[VPM] About that AUTOLOAD thing...
Peter Scott
Peter at PSDT.com
Sun Apr 13 11:12:55 CDT 2003
At 11:15 PM 4/12/2003 -0700, nkuipers wrote:
>...which I slammed. I stand corrected, apparently. The April issue of Linux
>Mag has an article on Eclipse, and one of the features mentioned is 'In this
>example, the method you're trying to call doesn't exist, so the "quick
>fix" is
>to create the method. The tip even provides a stub...that will be created
>for
>you...invaluable because it allows you to write code in a top-down fashion.'
>
>So there you have it.
There's a bit of apples and oranges here. From your description, this
article appears to be describing a venerated method of just-in-time method
creation; instead of writing a bunch of accessor methods, you put a list of
the allowed ones in a hash and then to avoid creating unnecessary ones,
create them on demand, something like (may be typos):
sub AUTOLOAD {
(my $method = our $AUTOLOAD) =~ s/.*://;
return if $method eq 'DESTROY';
croak "Invalid method $method" unless $METHOD{$method};
no strict 'refs';
*$method = sub { my $self = shift; $self->{$method} = shift if @_;
$self->{method} };
goto &$method;
}
Whereas the suggestion mooted here was to prompt the user to enter the
code... which gives me the willies. I guess something like this, perhaps:
sub AUTOLOAD {
(my $method = our $AUTOLOAD) =~ s/.*://;
return if $method eq 'DESTROY';
croak "Invalid method $method" unless $METHOD{$method};
print "Enter code for sub $method: ";
local $/;
my $code = <STDIN>;
no strict 'refs';
*$method = eval "sub { $code }";
goto &$method;
}
although can you read another method from STDIN after already seeing
end-of-file for the first one...?
--
Peter Scott
peter at psdt.com
http://www.perldebugged.com
More information about the Victoria-pm
mailing list