[tpm] Subroutine arcana
Jim Graham
jim_graham at sympatico.ca
Mon May 21 09:38:28 PDT 2007
Hi
Thanks for the compliment :)
Just to follow up on the anonymous subroutine assigned to a scalar: I saw somewhere (perl.com?) that this is a way to have private
methods in a class. This is one of the complaints against Perl's OO model: all methods declared as subs are visible. This trick is
one way to make private methods:
my $private_method = sub { }
Imagine:
--------
package foo
sub new
{
....
bless $self, $class;
}
my $private_method = sub {};
sub public_method
{
#-- call private method
$private_method->( @args );
}
new and public_method are visible, $private_method is not.
I think this is discussed in TheDamien's Object Oriented Perl, and this thread
http://www.perlmonks.org/?node_id=200102
- jim
-----Original Message-----
From: toronto-pm-bounces+jim_graham=sympatico.ca at pm.org [mailto:toronto-pm-bounces+jim_graham=sympatico.ca at pm.org] On Behalf Of
arocker at vex.net
Sent: May 21, 2007 12:18 PM
To: tpm at to.pm.org
Subject: Re: [tpm] Subroutine arcana
Thanks to everyone for the help on this topic, especially Jim Graham for
the code examples. (In any discussion of programming topics, a functioning
example beats 4 aces AND a Smith & Wesson.)
My mistakes were:
1. Believing what my course notes said, without verifying them. How many
innocent minds have I corrupted?
2. Not replacing the 2nd Edition Camel with the 3rd, which did mention
nesting subroutines. (Confirmed from a CD version.)
3. Assuming that defining an anonymous subroutine would cause it to be
executed at definition time. (If you assign the result of a function to a
scalar, the function gets evaluated. An anonymous subroutine definition
looks like a function, but it just assigns the subroutine's reference to
the scalar.)
Oh well, egg is said to be good for the complexion.
_______________________________________________
toronto-pm mailing list
toronto-pm at pm.org
http://mail.pm.org/mailman/listinfo/toronto-pm
More information about the toronto-pm
mailing list