in reply to Re^2: Variable Name Mistery. Who calls?
in thread Variable Name Mistery. Who calls?

#!/usr/bin/perl my $o = Foo->new(); $o->available_methods([ qw/one two/]); $o->one("This will work\n"); $o->two("This will work as well\n"); $o->three("This won't work\n"); package Foo; use Carp; my %Objs; sub new { my $self = {}; $Objs{$self} = {}; bless $self, shift; } sub available_methods { my($self,$methods_ref) = @_; $Objs{$self}{methods} = $methods_ref; } sub DESTROY { my $self = shift; delete $Objs{$self}; } sub AUTOLOAD { my $_call = our $AUTOLOAD; $_call =~ s/.*:://; my $self = shift; my $param = shift; if(grep /$_call/,@{ $Objs{$self}{methods} }) { print $param; } else { carp("Couldn't execute $_call\n"); } }

Replies are listed 'Best First'.
Re^4: Variable Name Mistery. Who calls?
by porta (Sexton) on Oct 25, 2006 at 22:17 UTC
    It's almost there. this is the output I've got: ~$ perl p.pl This will work This will work as well Couldn't execute three at p.pl line 9 And, what I want to achieve is to print (for example) which variable raised that warning. Something like this: Couldn't execute three on $o at p.pl line 9 Perhaps am I asking to much?

      Well, you could use PadWalker's var_name. var_name won't always work, though.

      Before using PadWalker, ask yourself if you really think the tiny bit of extra usefulness to help debug the rare situation where a non-existant function is called more than once in a given statement is really worth the extra work, complexity and maintenance issues of displaying the variable's name (when there might not even be one).