> Greg London wrote:
>> ...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...
>
> To me this looks like an example of where we suffer by not having OO
> truly baked into the language. It's a pretty natural fit for this
> problem, but not the first thing people shout out, because we tend to
> want to keep small scripts simple and procedural, and not bother with
> the overhead of OO.

Meh. I don't blame that on perl's lack of "builtin" OO.

I've pasted an OO version and the closure version of my script below.
They're nearly identical. they both take about the same amount of
lines of code. Perl's "bolt-on" version of classes can fix this
about as easily as perl's closure stuff can fix it.

The problem is I'm not thinking of this script as an OO script.
I suppose one could imagine a class call "HD_Label" and every
object is a specific label, but I'm not entirely convinced that
that would actually be an improvement.

Given how short the script is, I wasn't even really worried about
$y being passed in by reference every call to printline.
Federico pointed it out as something that annoyed him.
I solved that particular problem with minimal modification to the
rest of his code.

If this were a much larger script, I might suggest rearchitecthing
it from the ground up, converting it from procedural to object
oriented, but it was short enough that I shrugged it off.

What I was left with at the end of the evening was that wiping
a drive this way isn't really secure for data you care about.
It would seem that a drill press with a large bit to drill
a couple holes in the platter at different radii, and
then drill a hole right through the bearing so it can't spin
without grinding, would be the better solution.

So, the printline( \$y  thingy was more a minor afterthought in my mind.

Greg

====================================
Here is the OO version:
====================================

{

package printline;

sub calculate_initial_value{return 4;}

sub offset_value{return 8;}

sub new{
        my $obj={
                yval=>calculate_initial_value(),
        };

        return bless($obj,'printline');
}

sub print{
        my $obj=shift(@_);
        my($text,$size,$font)=@_;
        $size||=8;
        $font||="normal"; # default value;

        $obj->{yval}+=offset_value();

        print "text is '$text', size is '$size', font is '$font', y is '".
($obj->{yval}). "'\n";

};

}

my $pl = printline->new();

$pl->print("hello");
$pl->print("world", 6);
$pl->print("world", 4, "bold");



====================================
Here is the closure version:
====================================


sub calculate_initial_value{return 4;}

sub offset_value{return 8;}

my $printline;

{

my $y = calculate_initial_value();

$printline = sub{

        my($text,$size,$font)=@_;
        $size||=8;
        $font||="normal"; # default value;

        $y=$y+offset_value();

        print "text is '$text', size is '$size', font is '$font', y is '$y'\n";

};

}

$printline->("hello");
$printline->("world", 6);
$printline->("world", 4, "bold");









_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to