SPUG: Can't print from within AUTOLOAD
Dave O
cxreg at pobox.com
Tue Jun 6 13:56:23 PDT 2006
It does work, as evidenced by
$ perl -le 'sub AUTOLOAD { print "hi there"; } foo();'
Perhaps you closed STDOUT, or used select() to pick a different default
FH?
You probably also want to return the value that was set when creating the
set method, btw, as that's a common expectation and shouldn't hurt. Your
created sub does just that by not having an explicit return.
Dave
On Tue, 6 Jun 2006, Satish Gupta wrote:
> Does the "print" statement inside the AUTOLOAD function print somewhere else? Even though the following AUTOLOAD function is being called, I am not seeing the output of "print" statement on stdout!
>
> Thanks for your help.
>
> -------------
>
> sub AUTOLOAD
> {
> print "in AUTOLOAD $/";
> no strict "refs";
> my ($self, $newval) = @_;
> # Was it a get_... method?
> print "\$AUTOLOAD=$AUTOLOAD $/";
> if ($AUTOLOAD =~ /.*::get(_\w+)/ && $self->_accessible($1,'read'))
> {
> my $attr_name = $1;
> *{$AUTOLOAD} = sub { return $_[0]->{$attr_name} };
> return $self->{$attr_name}
> }
> # Was it a set_... method?
> if ($AUTOLOAD =~ /.*::set(_\w+)/ && $self->_accessible($1,'write'))
> {
> my $attr_name = $1;
> *{$AUTOLOAD} = sub { $_[0]->{$attr_name} = $_[1] };
> $self->{$1} = $newval;
> return
> }
>
> # Must have been a mistake then...
> croak "No such method: $AUTOLOAD";
> }
>
> --------------
>
>
> ---------------------------------
> New Yahoo! Messenger with Voice. Call regular phones from your PC and save big.
More information about the spug-list
mailing list