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

Hi, I am writing a perl script in Win32. If the user enters wrong number of parameters to the script, it has to return a error. My code for this:

if (!@ARGV) { print "Usage: perl $0 <XML filename> <equations file name>\nType $0 h +for Help!\n"; exit(0); }

In the similar way, if the user types h along with the program name, it has to display help. The code for this is:

if(@ARGV="h") { print "Help contents"; exit(0); }

When executing the code, whether I type h or any other character, I get the "Help contents" displayed. Can anyone tell me what's wrong in my code?

Replies are listed 'Best First'.
Re: Help display when executing the script
by GrandFather (Saint) on Aug 08, 2006 at 10:18 UTC

    I strongly recommend that you use strictures: use strict; use warnings;. They would have told you that the assignment in the conditional expression was likely a bad thing to do. You probably actually want something like:

    if ($ARGV[0] eq 'h') {

    Note the use of eq rather than == because you want a string compare rather than a numeric compare.

    However a much better way to manage all the command line processing is to use Getopt::Long.


    DWIM is Perl's answer to Gödel
Re: Help display when executing the script
by davorg (Chancellor) on Aug 08, 2006 at 10:21 UTC
    if(@ARGV="h")

    A single equals sign is an assignment operator. You're overwriting the value of @ARGV with "h". As "h" is true, that expression will always be true.

    You need two equals signs to check equality. But that checks _numeric_ equality, so you actually need "eq"

    if(@ARGV eq "h")

    But that's still not right as that compares "h" to the number of elements in @ARGV. If the "h" will always be the first option on the command line, then you could do this:

    if ($ARGV[0] eq 'h')

    Or to cover more options, you could use this:

    if (grep { $_ eq 'h' } @ARGV)

    But really, you should look at one of the command line option processing modules like Getopt::Std or Getopt::Long.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      As "h" is true that expression will always be true.

      To be pedantic, it is not because "h" is true that the expression is true, otherwise

      if ( @ARGV = 0 )

      would evaulate to false, which it doesn't.

      Rather, if my reading of perlop is correct, it is because the assignment operator returns true (or more strictly, the number of elements assigned to @ARGV):

      my $return = @ARGV = ( 1, 2, 3 ); print $return;

      prints 3.

      Update: fixed typos in text.

Re: Help display when executing the script
by Corion (Patriarch) on Aug 08, 2006 at 10:20 UTC
Re: Help display when executing the script
by dorward (Curate) on Aug 08, 2006 at 10:18 UTC

    The two obvious issues are:

    1. @ARGV is an array, not a scalar. You probably want to be comparing the first item in it, not the whole thing.
    2. '=' is the assignment operator, you want to string equality operator 'eq'

    (And you're also failing to use strict and warnings)

Re: Help display when executing the script
by Samy_rio (Vicar) on Aug 08, 2006 at 10:22 UTC

    Hi, If I understood your question correctly then try like this,

    use strict; use warnings; use Win32; use Getopt::Std; my %opts; getopts("h", \%opts); help() if $opts{h}; sub help { Win32::MsgBox("Help Content", 32, "Help"); exit; }

    Updated : Thanks davorg.

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

      for a long time, Getopt::Std has supported an interface which doesn't require the use of package variables. You can pass in a reference to a hash and that hash will be populated with the options.

      use Getopt::Std; my %opts; getopts('h', \%opts); help() if $opts{h};

      I think that's a much nicer interface.

      Also, I think your call to getopts uses the Getopt::Long interface, not the Getopt::Std one. Getopt::Std only supports single letter options.

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

Re: Help display when executing the script
by cdarke (Prior) on Aug 08, 2006 at 10:23 UTC
    if (@ARGV = "h" ) is actually an assignment. You are overwriting @ARGV and making it a single element array containing 'h'. Using two equal signs == is an arithmetic comparison, so I think you mean:
    if ($ARGV[0] eq 'h')
    However you will get a warning from this (you are setting warnings, right?) so:
    if (@ARGV && $ARGV[0] eq 'h')
    This checks that @ARGV actually has some elements.