[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: function lookups, just like method lookups! (was Re: [FWP]alien method calls)



On Apr 26, Neko said:

>You can also use the 'can' method to look up a subroutine as well as a
>method:
>
>    (B->can($_) or next)->() for qw/alpha woof gamma/;
>    (C->can($_) or next)->() for qw/alpha bravo charlie/;

It tried that, but Perl got rather funny with me, doing all sorts of
DESTROY stuff at line 0!  Actually, that was when I did it as

  $obj->can($func)

instead of

  $pkg->can($func)

as you'll soon see.

  # new UNIVERSAL::AUTOLOAD
  package UNIVERSAL;
  $DEBUG = 1;
  sub AUTOLOAD {
    require Carp;
    no strict;
    local $\;
    my ($pkg,$func) = $AUTOLOAD =~ /(.*)::(.*)/;
    my ($file,$line) = (caller)[1,2];
    print STDERR "&$AUTOLOAD called at $file line $line\n" if $DEBUG;
    for (@{ "${pkg}::ISA" }) {
      # COMMENT NEXT FOUR LINES
      if (my $code = $_->can($func)) {
        print STDERR "&${_}::$func called\n" if $DEBUG;
        return $code->(@_);
      }
      # AND UNCOMMENT NEXT FIVE LINES
      # my $obj = bless [], $_;
      # if (my $code = $obj->can($func)) {
      #   print STDERR "&${_}::$func called\n" if $DEBUG;
      #   return $code->(@_);
      # }
    }
    Carp::croak("Undefined subroutine &$AUTOLOAD called");
  }

The reason I'd tried it first with an object was because of what I read at
the UNIVERSAL.pm man page, but then I realized "static method" meant
"class method".

I didn't have that \&$AUTOLOAD thing you talked about, Neko.  But I'm
using 5.005_02.

-- 
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve             japhy@pobox.com
http://www.pobox.com/~japhy/                  http://pinyaj.stu.rpi.edu/
PerlMonth - An Online Perl Magazine            http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc.    http://www.perlarchive.com/


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe