PetaMem has asked for the wisdom of the Perl Monks concerning the following question:
everyone knows the basic principles of closures:
Unfortunatedly this behaviour doesn't suit the needs I actually encounter. Much more suitable would be a subroutine, that is able to remember its state whenever called. Without the additional#!/usr/bin/perl -w use strict; sub compare { my @mem; return sub { my $x = shift; my $y = shift; push @mem,$y if($x eq $y); return @mem; }; }; my $code = &compare; print "IT1: ",&$code('a','b'),"\n"; print "IT2: ",&$code('b','b'),"\n"; print "IT3: ",&$code('a','c'),"\n"; print "IT4: ",&$code('c','c'),"\n";
I tried to implement that with a closure inside a sub, but failed miserably. Anyone who could help me out here?
Thank you and
Bye
PetaMem
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: A metaclosure? (use static vars)
by demerphq (Chancellor) on May 13, 2002 at 11:10 UTC | |
by tye (Sage) on May 13, 2002 at 16:59 UTC | |
|
Re: A metaclosure? Howto?
by Biker (Priest) on May 13, 2002 at 10:25 UTC | |
|
Re: A metaclosure? Howto?
by broquaint (Abbot) on May 13, 2002 at 10:33 UTC | |
|
Re: A metaclosure? Howto?
by educated_foo (Vicar) on May 13, 2002 at 13:38 UTC | |
by demerphq (Chancellor) on May 13, 2002 at 13:56 UTC | |
by educated_foo (Vicar) on May 13, 2002 at 14:15 UTC | |
by demerphq (Chancellor) on May 13, 2002 at 14:18 UTC | |
by educated_foo (Vicar) on May 13, 2002 at 14:21 UTC |