Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Variable Name Mistery. Who calls?

by sauoq (Abbot)
on Oct 25, 2006 at 17:17 UTC ( [id://580626]=note: print w/replies, xml ) Need Help??


in reply to Variable Name Mistery. Who calls?

Maybe you can reliably do this with some B magic or something but there's no simple solution that I know of. For instance, consider the following code...

package P; sub new { bless {} } sub print_my_var { ... } package main; my $p = P->new(); my $q = $p; *r = $p; $p->print_my_var(); $q->print_my_var(); $r->print_my_var();
With $p, $q, and $r as three different names for the same object, how would you want that to behave?

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re^2: Variable Name Mistery. Who calls?
by porta (Sexton) on Oct 25, 2006 at 18:53 UTC
    What I want is: I'm working on a module to allow me create objects on the fly (I know there are a bunch of cpan modules that does exactly the same and better, but I was wondering how would that be, and also considered that a good learning experience) The point is that I'm using something like this (On my main class):
    sub AUTOLOAD { my $_call = our $AUTOLOAD; $_call =~ s/.*:://; my $self = shift; my $param = shift; ... $self->{$_call} = $_param || return $self->{$_call}; }
    So far, so good. But the problem is, that at some point, when I initialize a 'new' object, I define for it a list of available parameters, which will be then used as that object method. So, what I want is to raise a warning when I try to do something like
    my $o = Foo->new(); #create new object $o->Available_Param_List(["one", "two"]); #methods that should be avai +lable to $o $o->one('this will work ok'); $o->nope('this should print a warn on saying that nope is not availabl +e for $o');
    I want to avoid things like passing the var name to the constructor function or things like that... Thats why I been thinking that *maybe* there was a way to know the name of the variable that called to a package.

      The traditional message is

      Can't locate object method "nope" via package "Foo" (perhaps you forgo +t to load "Foo"?) at file.pl line 4.

      Why won't the following suffice?

      Can't locate object method "nope" (perhaps you forgot to specify "nope +" to Available_Param_List) at file.pl line 4.

      Chances are you won't be calling nope more than one time per line, so you'll know exactly which object is giving the error.

      Use Carp's croak to make the message appear to originate from the calling line.

      #!/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"); } }
        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?
      my $param = shift; ... $self->{$_call} = $_param || return $self->{$_call};
      I assume $param and $_param were supposed to be the same. That's a little obfuscated, and doesn't allow setting parameters to false values. A more typical way to do it is:
      # (after shifting everything but param) if (@_) { return $self->{$_call} = shift; } else { return $self->{$_call}; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://580626]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-19 02:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found