in reply to autoload and selfload

If you're adept, you can use them to install subroutines on the fly, the first time they're called (instead of compiling everything at the beginning). Not only does that save you a bit of typing if you have many similar methods (accessors -- get_variable, set_variable), but if your program only uses a few at a time, you get a bit of a speed benefit.

If you were really tricky-minded, you could add new subroutines on the fly, with some eval magic. Rough, untested code follows, as I'm not completely positive of the AUTOLOAD syntax without checking a book:

sub AUTOLOAD { next if $AUTOLOAD =~ /DESTROY/; # be safe my $sub_text = shift; $AUTOLOAD =~ s/.*:://; eval qq|*{$AUTOLOAD} = sub { $sub_text };| # the following goto may require # no strict 'refs'; goto &$AUTOLOAD; }
That's deep magic, and you'd better understand exactly what it does before you even think about using it. (There are only a couple of situations where I'd consider using it, and it would most definitely NOT be available to anyone off the street. Big security risk here.)

Update: I added the goto line because jlistf made me think of it.

Replies are listed 'Best First'.
RE: RE: autoload and selfload
by jlistf (Monk) on Jul 11, 2000 at 00:46 UTC
    so... you're using the AUTOLOAD subroutine to create a new function by sending it the code you want it to perform. (note: you can also use the goto to actually turn it into a function in the symbol table, case you didn't know.) interesting. i'm not sure that i'd ever use it, but i suppose it might come in handy somehow.

    y'know, you might be able to use this to add a subroutine to a running application without stopping it and restarting it... hmmm... any thoughts on that?