Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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

In reply to Example... ( Re: Interpolating variables for use in method calls ) by lestrrat
in thread Interpolating variables for use in method calls by dpatrick

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2023-05-30 00:37 GMT
Find Nodes?
    Voting Booth?

    No recent polls found