I've always wondered if this would work, but never tried it until now. It worked on the first try. Does that say more about me or Perl?
#!/usr/bin/perl -w
use strict;
sub installsub: lvalue
{
my $glob = shift;
no strict 'refs';
*{ $glob };
}
installsub( 'foo' ) = sub { print "Foo!\n" };
foo();
Of course, that's not bad enough:
for my $sub (qw( bar baz ))
{
installsub( $sub ) = sub { print "$sub\n" };
__PACKAGE__->can( $sub )->();
}
Worse yet, I have an actual factual practical use in mind.
Uh oh ... (Score:3, Funny)
Um, er, ah ... I hope I'm not somewhat responsible for that. I seem to recall having a beer in my hand at the time. It was the beer -- THE BEER, I TELL YOU!
Reply to This
Hrmm (Score:3, Interesting)
Were that I say, pancakes?
Reply to This
Using AUTOLOAD to do the same trick. (Score:4, Insightful)
use strict;
use warnings;
sub AUTOLOAD : lvalue {
no strict 'refs';
*$::AUTOLOAD;
}
foo () = sub {print "Foo!\n"};
foo ();
__END__
Foo!
Reply to This
More about you (Score:3, Funny)
It says you are one sick puppy. ;-)
Of course, Abigail isn't very far. But we already knew that.
I should really stop replying to journals on Friday....
Reply to This