When should I use Perl's AUTOLOAD?

前端 未结 3 1522
我在风中等你
我在风中等你 2021-01-17 17:13

In \"Perl Best Practices\" the very first line in the section on AUTOLOAD is:

Don\'t use AUTOLOAD

However all t

3条回答
  •  心在旅途
    2021-01-17 17:42

    I think for a stand alone script this approach is OK. You can create subroutines on the fly to speed up subsequent calls, e.g.:

    sub AUTOLOAD {
        (my $name = our $AUTOLOAD) =~ s/.*:://;
        no strict 'refs';  # allow symbolic references
    
        *$AUTOLOAD = sub { print "$name subroutine called\n" };    
        goto &$AUTOLOAD;   # jump to the new sub
    }
    

    Autoloading is tricky when producing inheritance trees.

提交回复
热议问题