perl scripts fail to read commandline arguments

Discussion in 'Perl Misc' started by reeddeer, Mar 11, 2005.

  1. reeddeer

    reeddeer Guest

    Using XP, SP2, all my previously working perl scripts now fail to
    notice any of the commandline arguments. I've never heard of this
    happening. I've rebooted the box and removed, then reinstalled
    ActivePerl-5.8.1.807-MSWin32-x86.msi, tested again and rebooted again.
    I've wondered if SYSIN could be mis-directed? Batch files accept
    arguments readily.

    Here is a testarg.pl program which fails to detect any arguments:

    use strict;
    use warnings;

    # -------------------------------------
    # Main Program
    # -------------------------------------

    if(@ARGV > 0 )
    {
    print "1st arg: ~~$ARGV[0]~~ \n";

    if ($#ARGV > 1 ) {
    print "2nd arg: ~~$ARGV[1]~~ \n";
    }
    if ($#ARGV > 2 ) {
    print "3rd arg: ~~$ARGV[2]~~ \n";
    }
    print "Done, read ~~$#ARGV~~ args.\n";

    }

    else
    {
    die "No arguments were seen! \n";
    }
    # -- end ------------------------------


    I try it with:
    testarg argue1 param2
    and get: No arguments were seen!
    testarg "argue1" "param2"
    and get: No arguments were seen!

    My co-workers have similar machines and they don't have the problem,
    the same test script prints the arguments given.

    Any suggestions? tips?
    Thanks,
    Reeddeer
    reeddeer, Mar 11, 2005
    #1
    1. Advertising

  2. reeddeer

    reeddeer Guest

    I found the problem: in "File Types" the .PL extension was opened by
    "\path\perl.exe" instead of by: "\path\perl.exe" "%1" %* so it did
    not open the perl script _with_ the arguments that were passed. Sorry
    for using the bandwidth, but perhaps this will help someone else in the
    future.

    Thanks,
    reeddeer
    reeddeer, Mar 12, 2005
    #2
    1. Advertising

  3. reeddeer wrote :
    > Using XP, SP2, all my previously working perl scripts now fail to
    > notice any of the commandline arguments. I've never heard of this
    > happening. I've rebooted the box and removed, then reinstalled
    > ActivePerl-5.8.1.807-MSWin32-x86.msi, tested again and rebooted again.
    > I've wondered if SYSIN could be mis-directed? Batch files accept
    > arguments readily.
    >
    > Here is a testarg.pl program which fails to detect any arguments:
    >
    > [snipped]


    look at this, I hope it helps:

    shell > perl -e 'if($#ARGV > -1) { print $#ARGV +1," arguments were passed\n"}
    else { print "no arguments were passed\n"}' hello world
    2 arguments were passed

    shell > perl -e 'if($#ARGV > -1) { print $#ARGV +1," arguments were passed\n"}
    else { print "no arguments were passed\n"}'
    no arguments were passed

    The index of the last argument is in $#ARGV, so $#ARGV +1 returns the
    number of arguments

    Best regards
    Martin

    --
    perl -e '$S=[[73,116,114,115,31,96],[108,109,114,102,99,112],
    [29,77,98,111,105,29],[100,93,95,103,97,110]];
    for(0..3){for$s(0..5){print(chr($S->[$_]->[$s]+$_+1))}}'
    Martin Kissner, Mar 12, 2005
    #3
  4. Martin Kissner <> wrote in
    news::

    >
    > The index of the last argument is in $#ARGV, so $#ARGV +1 returns the
    > number of arguments
    >


    And, of course, one could just use @ARGV in scalar context to get the
    number of arguments passed.

    Sinan
    A. Sinan Unur, Mar 12, 2005
    #4
  5. A. Sinan Unur wrote :
    > Martin Kissner <> wrote in
    > news::
    >
    >>
    >> The index of the last argument is in $#ARGV, so $#ARGV +1 returns the
    >> number of arguments
    >>

    >
    > And, of course, one could just use @ARGV in scalar context to get the
    > number of arguments passed.


    Yes, of course. Thanks for pointing out.
    This is actually what the OP does in the first if query 'if(@ARGV > 0 )'
    (not exactly. but kind of).

    But then he does

    if ($#ARGV > 1 ) {
    print "2nd arg: ~~$ARGV[1]~~ \n";
    }

    where the query should be

    if ($#ARGV > 0 )
    or
    if ($#ARGV => 1 )
    or even
    if (@ARGV > 1 )

    and so on.
    Finally

    print "Done, read ~~$#ARGV~~ args.\n";

    should be

    print "Done, read ~~",$#ARGV+1,"~~ args.\n";

    to return the number of arguments correctly (of course scalar @ARGV
    would return the same result as $#ARGV+1 does).
    The rest of the script seems all right to me.

    Best regards
    Martin

    --
    perl -e '$S=[[73,116,114,115,31,96],[108,109,114,102,99,112],
    [29,77,98,111,105,29],[100,93,95,103,97,110]];
    for(0..3){for$s(0..5){print(chr($S->[$_]->[$s]+$_+1))}}'
    Martin Kissner, Mar 12, 2005
    #5
  6. Abigail wrote :
    > Martin Kissner () wrote on MMMMCCXI September MCMXCIII
    > in <URL:news:>:
    > ??
    > ?? where the query should be
    > ??
    > ?? if ($#ARGV > 0 )
    > ?? or
    > ?? if ($#ARGV => 1 )
    >
    > I guess you mean
    >
    > if ($#ARGV >= 1 )


    Yes, I do; sorry for the typo.

    Best regards
    Martin

    --
    perl -e '$S=[[73,116,114,115,31,96],[108,109,114,102,99,112],
    [29,77,98,111,105,29],[100,93,95,103,97,110]];
    for(0..3){for$s(0..5){print(chr($S->[$_]->[$s]+$_+1))}}'
    Martin Kissner, Mar 13, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    how to write a program that takes arguments from commandline?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 11, 2006, in forum: C Programming
    Replies:
    26
    Views:
    636
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    Feb 14, 2006
  2. sapsi
    Replies:
    4
    Views:
    1,313
    Robert Kern
    Apr 21, 2009
  3. Iñaki Baz Castillo
    Replies:
    2
    Views:
    505
    Iñaki Baz Castillo
    Dec 23, 2009
  4. Ross
    Replies:
    1
    Views:
    133
    Brian Wakem
    Aug 19, 2005
  5. Santosh Kumar
    Replies:
    2
    Views:
    185
    Steven D'Aprano
    Sep 16, 2012
Loading...

Share This Page