#!perl use warnings; use strict; my $test = Test->new(); $test->print("hello\n"); $test->print("hello2\n"); $test->print("hello3", "hello4\n"); # ============================================================ package Test; # ------------------------------------------------------------ sub new { # any constructor my ($proto) = shift; my $class = ref($proto) || $proto; my $parent = ref($proto) && $proto; my $self; $self = {}; # create a new object bless($self, $class); } # new # ------------------------------------------------------------ sub AUTOLOAD { use vars qw($AUTOLOAD); my ($self, @params) = @_; my ($method) = (split(/::/, $AUTOLOAD))[-1]; print "AUTOLOAD: $AUTOLOAD\nSELF: $self\n"; print "METHOD: $method\nPARAMS: @params\n"; # create code my $code = sub { my ($self, @params) = @_; print @params, "\n"; }; $self->$code(@params); # execute for the first time # and add it to symbol table no strict 'refs'; *{$method} = $code; use strict 'refs'; } # AUTOLOAD # ------------------------------------------------------------ sub DESTROY { # don't autoload DESTROY :-) # ... } # DESTROY # ------------------------------------------------------------