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

[FWP] Stupid uses of AUTOLOAD



Hi all,

inspired by Mark-Jason Dominus', ahem, ingenius uses of AUTOLOAD,
please laugh with, laugh at, dissect, ignore etc. the following:


#!/usr/bin/perl

sub AUTOLOAD {
  ($_ = $AUTOLOAD ) =~ s/^.*:://;
  s/(^|_)the_/ $1 eq '_' ? '_' : ''/gei;
  if (/^who_is_(\w+)_of_(\w+)$/) {
    eval "\$$1_by_key\{'$2'}"
  } elsif (/^who_is_(\w+)_(\w+)_of$/) {
    eval "\$$2_by_value\{'$1'}"
  } elsif (/^(\w+)_of_(\w+)_is_(\w+)$/) {
    eval "\$$1_by_key{'$2'} = '$3'; \$$1_by_value{'$3'} = '$2'"
  } elsif (/^(\w+)_is_(\w+)_of_(\w+)$/) {
    eval "\$$2_by_key{'$3'} = '$1'; \$$2_by_value{'$1'} = '$3'"
  }
}

The_father_of_luke_is_anakin();
shmi_is_the_mother_of_anakin();
print who_is_the_mother_of_anakin(), "\n";
print who_is_the_father_of_luke(), "\n";
print who_is_shmi_the_mother_of(), "\n";


This could be made a little bit more intelligent, for example to
recognize the reflective relationship between mother and son, brother
and sister, father and son, and to recognize descendants (fathers of
fathers etc.).

Not sure about all those evals either.



Marcel
-- 
sub AUTOLOAD{($_=$AUTOLOAD)=~s/^.*:://;y/_/ /;print} Just_Another_Perl_Hacker();

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