[Mastering-perl] Some things about the (Alpha) Dynamic Subroutines ; -)

brian d foy brian.d.foy at gmail.com
Tue Dec 5 18:17:46 EST 2006


On 11/4/06, Florian Merges <fmerges at cpan.org> wrote:

> Example looks like this:

>

> while( 1 )

> {

> my( $operator, @operand ) = get_line();


[snip]

> print $Operators{ $operator }->( @operand );

> }



> Why not doing here?

>

> print $some_sub->(@operand);


There's no good reason. I suspect the inconsistency was evolutionary
as I developed the example but didn't change all of the variable names
to match. :)






> 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 },

> ...

> );



That's a good idea, so I've done that. I also added a note about the
warning this produces. Exiting a subroutine with anything other than
return (implicit even) is a warnable offense.


> 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...


Perl checks UNIVERSAL before AUTOLOAD. It's as if UNIVERSAL is at the
end of @ISA. See perlobj for details.

In my version of Object-Oriented Perl, Damian gets it right (and the
reason it took me so long to actually respond to this message is from
waiting to get home, find my OOPerl book, and see what Damian actually
said). You might have been confused because UNIVERSAL's AUTOLOAD is
checked after all of the other AUTOLOADs, so in his chart UNIVERSAL
gets a last mention. It's also in the middle too, though. :)

Here's a little program to demonstrate it:


BEGIN{
package Foo;

sub bravo { "bravo from Foo" }

sub AUTOLOAD { "Foo AUTOLOAD called with $AUTOLOAD" }

package Bar;
use base qw( Foo );

sub alpha { "alpha from Bar" }

package UNIVERSAL;

sub alpha { "alpha from UNIVERSAL" }

sub bravo { "bravo from UNIVERSAL" }

sub charlie { "charlie from UNIVERSAL" }
}

my $obj = bless {}, 'Bar';

foreach my $method ( qw(alpha bravo charlie delta) )
{
print $obj->$method;
}

__END__

This gives the output that shows it looks in UNIVERSAL before it hits
Foo's AUTOLOAD to figure out what to do with "charlie":

alpha from Bar
bravo from Foo
charlie from UNIVERSAL
Foo AUTOLOAD called with Bar::delta


> 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?


I've thought about that quite a bit and I go back and forth on it. One
of the goals of Mastering Perl is to show the reader different ways of
doing things and to acknowledge that different people do things
differently. I'm still undecided about it, and its not high on my list
of things to fix right now. :)



> Last question, Brian, how do you want the contributions, is this way ok, or

> do you preffer to get patches?


I think I can deal with either, although patches are as simple as code
patches since a change may motivate me to update something elsewhere
in the book, and I usually don't take the changes literally. Most
suggestions make it into the text, but usually not exactly in the way
they were suggested. However, I can read patches just fine :)

--
brian d foy <brian.d.foy at gmail.com>
http://www.pair.com/~comdog/


More information about the Mastering-perl mailing list