[Mastering-perl]
Some things about the (Alpha) Dynamic Subroutines ; -)
Florian Merges
fmerges at cpan.org
Sat Nov 4 13:32:32 EST 2006
Hi,
Subroutines as data
-----------------------------------------------
although we didn't tell you what they were anonymous subroutines. ->
s/what/that/ ?
Example looks like this:
while( 1 )
{
my( $operator, @operand ) = get_line();
my $some_sub = $Operators{ $operator };
unless( defined $some_sub )
{
print "Unknown operator [$operator]\n";
last;
}
print $Operators{ $operator }->( @operand );
}
Why not doing here?
print $some_sub->(@operand);
I mean if you have done the assignation why not using it, otherwise you
could have wrote
unless ( exists $Operators{ $operator } ) {
Could also be more readable calling $some_sub as $subroutine or something
like this
With the 'q' operator, wouldn't it be better to put a label to the while
loop, I mean could be less error prone if the reader use it to do something
more complex:
my %Operators = (
...
'q' => sub { last LOOP },
...
);
LOOP: while (1) {
...
unless (...) { last LOOP }
--------------------------------------------------------------------------------------
Simbolic references
--------------------------------------------------------------------------------------
I then dereference it a typeglob and assign my anonymous subroutine to
it. -> dereference it into a typeglob ?
The second example is wrong... this works:
use strict;
{
no strict 'refs';
my $evil_name = '<=>';
*{ $evil_name } = sub { print "How did you ever call me?\n" };
# <=>() yeah, that's not gonna happen
*{ $evil_name }{CODE}->(); # The long way
&{$evil_name}(); # Another way ;-)
}
---------------------------------------------------------------------------
Subroutines as arguments
---------------------------------------------------------------------------
because I really on it to have special -> rely
---------------------------------------------------------------------------
Autoloaded methods
---------------------------------------------------------------------------
First paragraph is not complete, the AUTOLOAD inside a class is not the
final catch-all, it will search also in the inheritance tree for AUTOLOAD
and then goes trough UNIVERSAL... there is a nice description in the book
Object Oriented Perl by D. Conway.
To define a method based on $AUTOLOAD, I first have to -> AUTOLOAD
without the $
On the example could be better to use () or && on the if condition. And I
think people are more used to the BLOCK LIST form of grep instead of the
EXPR, LIST.
Only as a matter of style, wouldn't it be cool to have the same code layout
used in the Learning Perl and Intermediate Perl?
Last question, Brian, how do you want the contributions, is this way ok, or
do you preffer to get patches?
Kind regards,
Florian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://five.pairlist.net/pipermail/mastering-perl/attachments/20061104/349df895/attachment.htm
More information about the Mastering-perl
mailing list