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

Dear wise Monks,
I would like to ask you the following:
I have a PHP page that calls a Perl script to execute locally, and passes a number of arguments to it.
The arguments passed are:
1) input file
2) number of sequences in the input file
3) a text string
4) an indication if a textbox is selected or not by the user
5) the same as #4

The call of Perl script is made by PHP exec function:
exec ("perl Whole_Program.pl $input_file $seqs_number $prior_info $run +_mode $txtbox1 $txtbox2", $res_total, $ret);
My problem has to do with #4 and #5. If the user has selected both text boxes, then the argument for $txtbox1 and $txtbox2 is 'on'.
However, if the user selects, say, only the second txtbox, then there is no value for $txtbox1 variable, because the @ARGV array contains 5 elements instead of 6.Is there a way to correct this? Because, if the user does not select one of the textboxes, then, the argument is not passed to the perl script.

Replies are listed 'Best First'.
Re: Problem with getting the correct arguments for my local Perl script
by ww (Archbishop) on Jul 05, 2014 at 12:24 UTC

    For your problem example: give each text box a default value; code your (PHP) script so that items "4)" and "5)" can be (a) dealt with or (b) ignored when they contain the default.


    check Ln42!

      Ouf, sometimes I realise that I am posting stupid questions :)
      Thanks a lot, that did the trick!
Re: Problem with getting the correct arguments for my local Perl script
by NetWallah (Canon) on Jul 05, 2014 at 15:39 UTC
    If you put single-quotes around your 'text strings' in your call to the perl script, it will solve the problem you mentioned(empty strings), plus allow spaces in the text.

            What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                  -Larry Wall, 1992

      * Fine print: May not work on your OS.

      OP: This is really more of a PHP question than a Perl question, so you might want to ask there. Some quick Googling seems to show that PHP doesn't support the more fine-grained control of arguments passed to other commands that Perl supports, but I may have missed something.

Re: Problem with getting the correct arguments for my local Perl script
by Laurent_R (Canon) on Jul 05, 2014 at 17:13 UTC
    When you don't have a $txtbox1 argument, presumably you could still pass it to the Perl script and after having given it a dummy value (possibly an empty string, a zero (0), a negative value, whatever normally impossible value, which you Perl script can then identify as a really empty parameter.
Re: Problem with getting the correct arguments for my local Perl script
by boftx (Deacon) on Jul 05, 2014 at 22:36 UTC

    ww and Laurent_R have already given effective answers. I would only add that the use of Getopt::Long in your Perl scripts can further reduce possible errors on the command line by requiring named parameters thereby allowing you to take corrective action if they are not present.

    You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.