in reply to Re: OO Control loops
in thread OO Control loops

Well, obviously it is slower and uglier than using normal control loops, and I wouldn't suggest using it always. But if you have to write repeated control structures, and don't want to write the same code many times, it may be useful.

As for subrefs, this code is essentially a pretty interface to them, though you can also use strings to eval. I assume what you are thinking of is:

my $subref = sub {print shift() . "\n";}; my $subref2 = sub {print "Length is " . length shift() . "\n";}; foo($subref); foo($subref2); sub foo { my $code = shift; foreach (@some_array) { &$code($_); } }

which is the same but less flexible. But maybe you have something else in mind.

dave hj~

Replies are listed 'Best First'.
Re: Re: Re: OO Control loops
by Juerd (Abbot) on Jan 04, 2002 at 16:28 UTC
    Repeated control structures can be a sign of bad programming. But still, I'd rather use the same structure over and over than an OO way to obscurely do the same thing less efficiently.

    And about the subrefs: Sorry, my bad. I only saw the 'print ...' example and somehow didn't notice the other one. The example of what you thought I was thinking about, was not what I meant, but I still like that better than the OO thingy :)

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$