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

Re: (ar0n) Re: Interpolating variables for use in method calls

by dpatrick (Scribe)
on Jul 13, 2001 at 23:52 UTC ( #96562=note: print w/replies, xml ) Need Help??


in reply to (ar0n) Re: Interpolating variables for use in method calls
in thread Interpolating variables for use in method calls

Thank you. That worked, but I don't quite understand why.
I thought that I needed $refname to be interpolated before
the eval in order for the parameter to be passed to viewDocument()
...

dap
  • Comment on Re: (ar0n) Re: Interpolating variables for use in method calls

Replies are listed 'Best First'.
Example... ( Re: Interpolating variables for use in method calls )
by lestrrat (Deacon) on Jul 14, 2001 at 02:31 UTC

    Let me expand on what arOn was saying

    Suppose you ultimately want to call foo->bar( $baz ). And the way you want to achieve it is by dynamically deciding what the subroutine you're going to use, like so:

    my $subname = 'baz'; my $param = { abc => 'xyz' }; # or an object. whatever. eval 'foo->' . "$subname( \$param )";

    Okay, now observe what happens when you call the eval.

    # first, the string that is to be eval'ed is created 'foo->' . "$subname( \$param )" => 'foo->baz( $param )' # then the eval kicks in eval 'foo->baz( $param )' # which is effectively means foo->baz( $param ); # where $param is the ref to a hash

    Now observe what happens if I change the quotations and such...

    # suppose we use "$subname( $param )" 'foo->' . "$subname( $param )" => "foo->baz( 'HASHx(....)' )" # where HASHx(...) is the string representation of the # hash ref. this happens because you're interpolating # the hashref within the string end result => foo->baz( 'HASHx( .... )' ); ---- # what if we use "baz( $param )" ? 'foo->' . "baz( $param )" => "foo->baz( 'HASHx(....)' )" # ah, same as the example above end result => foo->baz( 'HASHx( .... )' ); ---- # suppose we use '$subname( $param )' 'foo->' . '$subname( $param )' => 'foo->$subname( $param )' # so no interpolation is done in the first step... foo->$subname( $param ); # ah, but this is perfectly valid!

    Hope that clears things up. Personally, I like suaveant's way of calling method better, though

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2023-03-23 14:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (60 votes). Check out past polls.

    Notices?