Click here to get back home

somewhat unusual way to define a sub

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
somewhat unusual way to define a sub Ronny 04-07-2008
Posted by Ronny on April 7, 2008, 11:59 am
Please log in for more thread options
I found in a program a piece of code which basically looked like this:

my $n="myname";
*$n=sub { .... };

The whole think was of course guided by no strict 'refs'. My question:

Is this just a unusual way to write

sub myname { ... }

or did I miss something here?

Ronald

Posted by Uri Guttman on April 7, 2008, 4:55 pm
Please log in for more thread options

R> I found in a program a piece of code which basically looked like this:
R> my $n="myname";
R> *$n=sub { .... };

R> The whole think was of course guided by no strict 'refs'. My question:

R> Is this just a unusual way to write

R> sub myname { ... }

R> or did I miss something here?

it is just a wacko way to define a sub. it puts a code ref (the anon
sub) into the code slot in the *myname typeglob. it makes no sense to do
that unless the sub name was generated on the fly. and even that makes
little sense unless he has variant subs for the same name. and that is
better done with a dispatch table than munging the symbol table. i would
stay away from that code and coder if i were you!

strict has nothing to do with this as you can always define subs the
normal way under strict.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Posted by Ronny on April 8, 2008, 3:48 am
Please log in for more thread options
>
> R> I found in a program a piece of code which basically looked like this:
> R> my $n="myname";
> R> *$n=sub { .... };
>
> R> The whole think was of course guided by no strict 'refs'. My question:
>
> R> Is this just a unusual way to write
>
> R> sub myname { ... }
>
> R> or did I miss something here?
>
> it is just a wacko way to define a sub. it puts a code ref (the anon
> sub) into the code slot in the *myname typeglob. it makes no sense to do
> that unless the sub name was generated on the fly. and even that makes
> little sense unless he has variant subs for the same name. and that is
> better done with a dispatch table than munging the symbol table.

I guessed so. Thank you for confirming it.

> strict has nothing to do with this as you can always define subs the
> normal way under strict.

An expression such as *$x, where $x holds a string, would be illegal
under use strict 'refs' (that's why the coder had wrapped

no strict 'refs'

around this construct). Of course if the sub would have been defined
in a "normal" way, this would not be needed.

Ronald

Posted by Peter Scott on April 8, 2008, 9:07 am
Please log in for more thread options
On Mon, 07 Apr 2008 20:55:32 +0000, Uri Guttman wrote:

>
> R> I found in a program a piece of code which basically looked like this:
> R> my $n="myname";
> R> *$n=sub { .... };
>
> R> The whole think was of course guided by no strict 'refs'. My question:
>
> R> Is this just a unusual way to write
>
> R> sub myname { ... }
>
> R> or did I miss something here?
>
> it is just a wacko way to define a sub. it puts a code ref (the anon
> sub) into the code slot in the *myname typeglob. it makes no sense to do
> that unless the sub name was generated on the fly. and even that makes
> little sense unless he has variant subs for the same name. and that is
> better done with a dispatch table than munging the symbol table. i would
> stay away from that code and coder if i were you!

I beg to differ. I do this frequently to populate a class with a
number of similar methods, e.g.:

use Log::Log4Perl;
BEGIN {
for my $method ( keys %Log::Log4perl::Level::PRIORITY ) {
no strict 'refs';
*$method = sub {
my $self = shift;
my $logger = Log::Log4Perl->get_logger( ref $self );
$logger->$method( @_ );
}
};
}

Or there's just-in-time method creation (traditional objects):

sub AUTOLOAD {
(my $method = our $AUTOLOAD) =~ s/.*://;
return if $method eq 'DESTROY';
croak "No such attribute" unless $ALLOWED_ATTR;
no strict 'refs';
*$method = sub {
my $self = shift;
$self-> = shift if @_;
return $self->;
};
goto &$method;
}

I don't think a dispatch table is a better solution for those cases.

--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/


Posted by Ronny on April 8, 2008, 9:58 am
Please log in for more thread options
> On Mon, 07 Apr 2008 20:55:32 +0000, Uri Guttman wrote:
> > it is just a wacko way to define a sub. it puts a code ref (the anon
> > sub) into the code slot in the *myname typeglob. it makes no sense to do
> > that unless the sub name was generated on the fly.

> I beg to differ. I do this frequently to populate a class with a
> number of similar methods, e.g.:
>
> use Log::Log4Perl;
> BEGIN {
> for my $method ( keys %Log::Log4perl::Level::PRIORITY ) {
> no strict 'refs';
> *$method = sub {
> my $self = shift;
> my $logger = Log::Log4Perl->get_logger( ref $self );
> $logger->$method( @_ );
> }
> };
>
> }

Well, your case *does* make sense, because $method is a different
string on every execution. My original example, however, was that
the method name was "constant" (i.e. set to the same string on every
execution), so I agree with Uri that this could have done more
naturally
in the conventional way of defineing a sub.

Ronald


>
> Or there's just-in-time method creation (traditional objects):
>
> sub AUTOLOAD {
> (my $method = our $AUTOLOAD) =~ s/.*://;
> return if $method eq 'DESTROY';
> croak "No such attribute" unless $ALLOWED_ATTR;
> no strict 'refs';
> *$method = sub {
> my $self = shift;
> $self-> = shift if @_;
> return $self->;
> };
> goto &$method;
>
> }
>
> I don't think a dispatch table is a better solution for those cases.
>
> --
> Peter Scotthttp://www.perlmedic.com/http://www.perldebugged.com/


Similar ThreadsPosted
Unusual warning - what's it trying tell me? August 22, 2005, 9:40 am
unusual behaviour of perl script compiled with perlapp on hp-ux11i May 8, 2005, 7:52 am
where to define this array? May 15, 2005, 10:52 pm
Define my own signal. May 12, 2006, 4:26 am
#define-like feature in Perl September 29, 2004, 6:55 am
define an array in perl July 31, 2007, 6:43 am
When do I need to define a function with "use POSIX qw(.....)" and when not ? December 12, 2007, 6:45 am
FAQ 4.74 How do I define methods for every class/object? February 23, 2005, 6:03 pm
cgi.pm popup_menu define select width? March 27, 2005, 12:28 pm
FAQ 4.74 How do I define methods for every class/object? April 26, 2005, 11:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap