lvalue typeglobs

chromatic on 2003-04-17T23:52:21

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

Ovid on 2003-04-18T00:03:16

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!

Hrmm

belg4mit on 2003-04-18T00:46:51

What would you use this for that AUTOLOAD couldn't do?

Using AUTOLOAD to do the same trick.

Abigail on 2003-04-18T12:34:04

#!/usr/bin/perl

use strict;
use warnings;

sub AUTOLOAD : lvalue {
    no strict 'refs';
    *$::AUTOLOAD;
}

foo () = sub {print "Foo!\n"};
foo ();
__END__
Foo!

More about you

VSarkiss on 2003-04-18T15:31:49

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