in reply to calling methods using a variable

Here's a quick replication of your problem:

use strict; use CGI; my $q = CGI->new; my $good_method = "param"; my $bad_method = "param('foo')"; print $q->$good_method( 'foo' ); print $q->$bad_method;

To fix it, make an array of array refs with the second item being your arguments:

@commands = ( ['add_node','W1'], ['add_node','W2'], ['add_edge,'W2' => 'W1'] ); use GraphViz; my $g = GraphViz->new(); foreach (@commands) { my ( $command, @args ) = @$_; $g->$command( @args ); }

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re: Re: calling methods using a variable
by Kanji (Parson) on Mar 22, 2002 at 00:48 UTC

    It would also prudent to throw a next unless $g->can($command); or something in there so that things don't go bewm if $command doesn't exist (ie, someone made a tipo [sic.]).

    You might even want to throw in some security checks for good measure...

    foreach (@commands) { my ( $command, @args ) = @$_; warn("Naughty, $command not allowed..."), next unless $permitted{$command}; warn("Ooops, I can't find $command..."), next unless $g->can($command); $g->$command( @args ); }

        --k.