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

Hello monks,

I am new to perl and trying to write a script which is having a subroutine and it takes as an input 2 parameters and gives one prints output the code is as follows

#!/usr/bin/perl BEGIN { unshift (@INC,"/view/sawans1_api_test/vobs/pp2dev/src/testsuite/user/t +inc"); unshift (@INC,"/view/sawans1_api_test/vobs/pp2dev/src/testsuite/user") +; } use LWP::Simple; use DHPL::System; use nightly::common; use Logs::LogReader; sub get_user_scenario_desc{ my($web_link, $us_name) = @_; if($web_link =~ m/nightly_results(.*?)(.html)/) { $web_link= "/nightly_db".$1; } `rcp lcla238.lss.emc.com:$web_link`; my $file = "/tmp/nightly_db.html"; my $sul = SuiteLog->new( { FILE => $file } ); # $logfile is the log f +ile of test run $sul->parse_suite( ); # get_scheduled will return the list of all the test_suites present in + the respective test_run foreach my $sname ( @{$sul->get_scheduled( )} ) { my $scl = $sul->get_log( $sname ); my $name = $scl->name( ); my $result = $scl->result( ); my $errors = $scl->log_errors_short( ); my $description = $scl->get_decription( ); if($name eq $us_name){ print"Test Scenario Name = $name\n"; print"Result = $result\n"; print"Errors = $errors\n"; print"Description = $description\n"; } } }

Here I wanted to pass the two inputs to the subroutine defined above..I tried like this

perl UserScenarioDesc.pl &get_user_scenario_desc("http://some_link/XYZ +.html","testdare/UserScenarioDAREtestCkmadm")

And I got error like this

-bash: syntax error near unexpected token `"http://some_link/XYZ.html","testdare/UserScenarioDAREtestCkmadm"'

As I am prttt new to perl can anybody tell me how to call such subroutine with parameters from command line???

Thanks in advance !!!

Replies are listed 'Best First'.
Re: calling paramaterized subroutine
by moritz (Cardinal) on Mar 22, 2010 at 22:23 UTC
    You are mixing up perl code and shell code.

    What you can do is to use @ARGV in perl, so write something like this at end of your perl code:

    get_user_scenario_desc get_user_scenario_desc(@ARGV[0, 1])

    This called the sub get_user_scenario_desc with the first two command line parameters, so then you can write on the shell:

    perl UserScenarioDesc.pl http://some_link/XYZ.html testdare/UserScenar +ioDAREtestCkmadm
    Perl 6 - links to (nearly) everything that is Perl 6.
Re: calling paramaterized subroutine
by Corion (Patriarch) on Mar 22, 2010 at 22:22 UTC

    This has little to do with Perl and everything to do with that & is a special character in your shell. You will need to use quotes to prevent your shell from interpreting the code:

    perl UserScenarioDesc.pl '&get_user_scenario_desc("http://some_link/XY +Z.html","testdare/UserScenarioDAREtestCkmadm")'

    But your program makes little sense and it does not use the passed parameters. Read perlvar to find about @ARGV as where the shell passes parameters to Perl. Most likely you just want to use the following in your program:

    get_user_scenario_desc(@ARGV);
Re: calling paramaterized subroutine
by almut (Canon) on Mar 22, 2010 at 23:13 UTC
    how to call such subroutine with parameters from command line?

    In theory, you could write

    $ perl -e 'do "UserScenarioDesc.pl"; get_user_scenario_desc("http://so +me_linkXYZ.html","testdare/UserScenarioDAREtestCkmadm");'

    Or, illustrated with a shorter, self-contained example:

    #!/usr/bin/perl # 830179.pl sub foo { print "foo(): @_\n"; } print "bar\n";
    $ perl -e 'do "./830179.pl"; foo("arg1", "arg2");' bar foo(): arg1 arg2

    The argument to the option -e (the part between single quotes) is a mini Perl script in which the do loads/compiles/runs the script. After that, the foo() function within the script is being called.

    However, as others have said, what you most likely really want is to pass arguments to the script, and not invoke a routine in it from the command line.