in reply to Creating stub methods

update: Remember my vars::i, how about a subs::i, which would go something like
=head1 NAME subs - Perl pragma to declare sub names and initialize them? =head1 SYNOPSIS use subs::i start_html => \&AUTOLOAD; =cut package subs::i; sub import { my $callpack = caller; my $pack = shift; my %imports = @_; foreach $sym (keys %imports) { *{"${callpack}::$sym"} = $imports{$sym}; } };

MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
** The third rule of perl club is a statement of fact: pod is sexy.

Replies are listed 'Best First'.
Re: Re: Creating stub methods
by Ovid (Cardinal) on Dec 29, 2003 at 18:18 UTC

    Actually, I think the "use subs" declaration solves the problem I was looking at, though I kind of like your "subs::i" idea. And you're right that it would be nice if CGI.pm provided a "can" method, but so many people use UNIVERSAL::can() directly that I think simply predeclaring the subs would be easier.

    Update: What I think is really nifty about the subs pragma is the line:

    *{"${callpack}::$sym"} = \&{"${callpack}::$sym"};

    We create the slot in the typeglob, but assign an undefined value. $module->can($method) works because we have the slot, but AUTOLOAD still gets called because there is no actual subroutine or method there. Nifty!

    Cheers,
    Ovid

    New address of my CGI Course.

      In fact it's not undefined in the full sense of the word. If you take a reference to a named sub which isn't yet defined, that reference magically becomes defined and valid as soon as that sub is defined. Observe:
      $_ = \&x; *x = sub { print "Oh my\n" }; $_->();
      This does print "Oh my". Perl is devious like that.

      Makeshifts last the longest.