>> Federico Lucifredi continues his quest to build a hardware-assisted
>> automagic hard-drive wiper, using perl in an embedded device. *Shiny
>> hardware! Demo! Code*!
Federico,
It was getting late, so I didn't want to throw in another
tangent for how to write your code. But I was thinking you
could replace your printline() function with a closure.
Instead of doing this over and over:
printline($var1,\$y,"text","font",$size);
You could take $y and put it inside a lexical block
then make a printline closure that uses $y
(basically, it's a global tothe subroutine, but the
lexical block means no one outside the block can
see $y or muck with it.
Then you don't have to keep passing in \$y every call.
The second, minor bit, is to reorder your parameters
a bit so that the things that change the most are first,
and the things that are usually one value are last.
Then you can take advantage of default values.
The code might look something like this:
# using dummy stubs, cause I don't remember how your code
# came up with these values
sub calculate_initial_value{return 4;}
sub offset_value{return 8;}
my $printline;
{ # lexical scope hides $y from everyone outside the scope.
my $y = calculate_initial_value();
$printline = sub{
my($text,$size,$font)=@_;
$size||=8; # default value
$font||="normal"; # default value;
$y=$y+offset_value();
print "text is '$text', size is '$size', font is '$font', y is '$y'\n";
};
} # end lexical scope
#Now call the closure, and you don't have ot pass in $y
#and you don't have to pass in default values
$printline->("hello");
$printline->("world", 6);
$printline->("Bye", 4, "bold");
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm