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

Re: [FWP] Self Aware Data routines?



i woulda done it this-a way...

=====================================================

use strict;
use diagnostics;
use vars qw/$AUTOLOAD/;

while (<DATA>) {
  chomp;
  no strict qw/refs/;  # the next line is a symref
  &$_;
}

sub AUTOLOAD { 
  print "Sorry, I don't see a subroutine called '$AUTOLOAD'\n"; 
}

exit;

sub Sneex { 
  print "Hi from Sneex...\n";
}

sub TheCat {
  print "Hi from SnittyMibs!\n";
}

__DATA__
Sneex
TheCat
NoSubr

$ perl -w autoload-tester 
Hi from Sneex...
Hi from SnittyMibs!
Sorry, I don't see a subroutine called 'main::NoSubr'

=====================================================

for bonus points: create the non-existent subroutine
in &AUTOLOAD as a sub that prints that it didn't
exist a few cycles ago, but does now.  don't use 
eval().

Bill Jones wrote:
> 
> #!/usr/local/bin/perl -wT
> 
> # A script that shows how to
> # write data-aware routines...
> 
> use strict;
> use diagnostics; # -verbose;
> 
> my $vSub = '';
> 
> while (<DATA>) {
>     no strict q/refs/;
>     $vSub = \m/^(.+)$/io;
>     &$$vSub if defined &$$vSub;
>     print "Sorry, I don't see a Subroutine
>            $$vSub\n" unless defined &$$vSub;
> }
> 
> exit;
> 
> sub Sneex {
> print "Hi from Sneex...\n";
> }
> 
> sub TheCat {
> print "Hi from SnittyMibs!\n";
> }
> 
> __DATA__
> Sneex
> TheCat
> NoSubr
> 
> Comments?
--
Steve Lane <sml@zfx.com>

==== Want to unsubscribe from Fun With Perl?
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org