anitsahu has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to invoke a subroutine of a particular perl module and passing an argument as well ,where the subroutine ,the perl module and the argument are parsed from a xml file, but everytime the passed argument in the invoked subroutine gets the name of the perl module..pls help me out i am providing the code below

use NAS; use RRC; use Layer1; use Cwd; use XML::Simple; use Data::Dumper; $xml = new XML::Simple (KeyAttr=>[]);# read XML file $config =$xml->XMLin("Config_test.xml"); $top = $config->{ConfigRoot}; $start =$top->{debugDef}; $begin =$start->{general}; open report,">report.txt"; foreach my $var(@{$begin->{genCategory1}}){ print report $var->{sysTag},"\n\n\n"; $str=$var->{pm}; foreach my $var1(@{$var->{genCategory2}}){ print report $var1->{name},"\n\n"; $str1=$var1->{subroutine}; $regexp=$var1->{searchStr}; print $regexp,"\n"; $str->$str1($regexp);

where NAS,RRC and Layer1 are the name of the perl modules,the line $str->$str1($regexp); always passes $str as parameter to the subroutine..

Replies are listed 'Best First'.
Re: invoking subroutines
by syphilis (Archbishop) on Jul 01, 2011 at 01:04 UTC
    always passes $str as parameter to the subroutine..

    Yes - $str is the *second* argument that gets passed. The first argument will be the package name. Here's a demo:
    use warnings; use strict; package MY_FOO; my $str = 'this is the second argument'; MY_FOO->foo($str); sub foo { print "$_\n" for @_; } __END__ prints: MY_FOO this is the second argument
    (I haven't dug into your code - I'm merely guessing that this is what's happening.)

    Cheers,
    Rob
Re: invoking subroutines
by ig (Vicar) on Jul 01, 2011 at 07:19 UTC
    the line $str->$str1($regexp); always passes $str as parameter to the subroutine.

    $str is passed as first argument to the subroutine because you are using method invocation syntax. In particular, you are using the arrow operator: ->.

    If you don't want $str passed as the first argument, you could use a symbolic reference:&{"${str}::${str1}"}($regexp).

    use strict; use warnings; package XXX; sub YYY { my ($x) = @_; print "hi there $x\n"; } package main; my $str = 'XXX'; my $str1 = 'YYY'; { no strict "refs"; &{"${str}::${str1}"}(10); }