in reply to Interpolating variables for use in method calls

You're using double quotes where you should be using single:
my $ref = 'Allsys::Resource::ManMan->' . $task . 'Document($refname)';

The problem is that $refname gets interpolated in double quotes, which will result in a function call with a bareword in it. Single quotes don't interpolate, so...

Note that only the last string needs single quotes. I changed them both for consistency's sake :)

ar0n ]

Replies are listed 'Best First'.
Re: (ar0n) Re: Interpolating variables for use in method calls
by dpatrick (Scribe) on Jul 13, 2001 at 23:52 UTC
    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

      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