in reply to Problem using code ref's in AUTOLOAD

The POD perlsub says that you can't a reference to a built in function using the syntax \&CORE::die.
...
Even though it looks like a regular function call, it
isn't: you can't take a reference to it, 
such as the incorrect "\&CORE::open" might appear to produce.
...

To get around this you could use an anonymous subroutine as the value to $DEFAULT_HANDLER.

our $DEFAULT_HANDLER = sub { die @_ };

Making this change seems to work.

bruce:1:~/tmp $ cat p.pl #!/usr/bin/perl # vim: sw=4 use strict; use warnings; package fred; our $DEFAULT_HANDLER = sub { die @_ }; our $AUTOLOAD; sub AUTOLOAD { print STDERR "AUTOLOAD(@_) - $AUTOLOAD\n"; my $self = shift; $DEFAULT_HANDLER->('died via indirection'); } package main; my $fred = fred->raise_exception("some test"); bruce:1:~/tmp $ ./p.pl AUTOLOAD(fred some test) - fred::raise_exception died via indirection at ./p.pl line 9. bruce:1:~/tmp $

Replies are listed 'Best First'.
Re^2: Problem using code ref's in AUTOLOAD
by Bloodnok (Vicar) on Aug 28, 2007 at 09:03 UTC
    Mmm, OK, looks like I'm being way dense ... you're right WRT code ref's, however, it doesn't explain why a call of the form
    package fred; our $DEFAULT_HANDLER = 'die'; sub AUTOLOAD { $&ref("Some text"); }
    doesn't work ... or my ignorance exceeds even my normal standards, and it does ?

      You are being very harsh on yourself. It looks like when a sub is referenced that doesn't exist autoload will only ever look in the current package. So when you are making subroutine call (as opposed to a method call) AUTOLOAD will only ever search for fred::die, which doesn't exist, hence the repeated calls to AUTOLOAD. Interesting.

      If the call to the missing subroutine is made as a method call, AUTOLOAD will search the current package and then the packages listed in the @ISA array. To get this to work the handler has to be called as a method, e.g.

      bruce:1:~/tmp $ cat p.pl #!/usr/bin/perl # vim: sw=4 #use strict; #use warnings; package bert; sub die { die @_; } package fred; our @ISA = qw( bert ); our $DEFAULT_HANDLER = "die"; our $AUTOLOAD; sub AUTOLOAD { print STDERR "AUTOLOAD(@_) - $AUTOLOAD\n"; my $self = shift; $self->$DEFAULT_HANDLER('died via indirection'); } package main; my $fred = fred->raise_exception("some test"); bruce:1:~/tmp $ ./p.pl AUTOLOAD(fred some test) - fred::raise_exception fred died via indirection at ./p.pl line 10. bruce:1:~/tmp $

      So how to make a package inherit from "CORE" ?