Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
laziness, impatience, and hubris
 
PerlMonks  

Re: eval inside an object

by sir.shz (Novice)
on Apr 27, 2005 at 14:00 UTC ( [id://452058]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to eval inside an object

Maybe you need to escape the "$self" as "\$self" since eval will first evaluate the $code, in which case $self will be subsititued by its value, something like "ClassName->HASH(...)". The following seemed to work:
package Foo; use strict; sub new{ return bless {}, shift; } sub foo{ my $self = shift; print "Foo called\n"; } sub bar{ my $self = shift; print "Bar called\n"; my $code = "\$self->foo()"; # it won't work withouth the "\" eval $code; } 1;
then:
use strict; use warnings; use Foo; my $c = Foo->new(); $c->foo(); $c->bar(); __OUTPUT__ Foo called Bar called Foo called

Replies are listed 'Best First'.
Re^2: eval inside an object
by ikegami (Patriarch) on Apr 27, 2005 at 15:52 UTC
    The OP used single-quotes, so the $ in $self (and in $result) doesn't need to be escaped.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://452058]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.