perl newbie - getting length of argv array

Discussion in 'Perl Misc' started by xhoster, May 4, 2006.

  1. xhoster

    xhoster Guest

    You should almost always use strict and use warnings.
    Perl is case sensitive. There is no @argv, unless you define one.
    There is an @ARGV.


    Xho
     
    xhoster, May 4, 2006
    #1
    1. Advertisements

  2. xhoster

    Lionel Guest

    Hi all,

    Just starting to learn perl. I don't seem to be able to get the length
    of the argv array correctly. Here's my script called perlScript.pl:

    #! /usr/bin/perl
    #
    #

    $length = @array;
    print "Length = @argv\n";
    $length = $#array + 1;
    print "Length = $length \n";



    Here's commandline output:

    ../perlScript.pl somearg secondarg third
    Length =
    Length = 0

    Any help appreciated.

    Lionel.
     
    Lionel, May 4, 2006
    #2
    1. Advertisements

  3. xhoster

    Dr.Ruud Guest

    Lionel schreef:
    #!/usr/bin/perl


    Missing:
    use strict;
    use warnings;

    my $length = @argv;
     
    Dr.Ruud, May 4, 2006
    #3
  4. Quick correction:

    my $length = @ARGV;

    In most cases, however, there is no need to save the length. Using @ARGV
    in scalar context works fine, and is more transparent:

    unless ( @ARGV == 2 ) {
    die <<EO_USAGE;
    bzzt: Please provide an input and output filename

    EO_USAGE
    }

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, May 4, 2006
    #4
  5. xhoster

    Lionel Guest

    What is strict? I was using warnings but didn't included it on this post.
    Cheers, that got it. I also noticed that I had "@array" instead of @ARGV
    above . . . bad copy and paste :).

    Lionel.
     
    Lionel, May 4, 2006
    #5
  6. xhoster

    Lionel Guest

    Thanks to both replies :).

    Yes. Although the code I posted here was one of the wrong versions of
    what I was trying, the problem I was having is that I didn't know that
    argv was supposed to be uppercase. Thanks for that.
    I was getting to that eventually :).

    On track now . . . thanks. What a stupid question I asked :).

    Lionel.
     
    Lionel, May 4, 2006
    #6
  7. perldoc strict

    While you are at it, you should read the posting guidelines as well

    How should I put this? You are not telling the truth. If you had run
    this script with warnings enabled, you would have gotten:

    D:\Home\asu1\UseNet\clpmisc\cafe> perl -Mwarnings t.pl
    Possible unintended interpolation of @argv in string at t.pl line 6.
    Name "main::argv" used only once: possible typo at t.pl line 6.
    Length =
    Length = 0


    At which point, you would have checked

    perldoc perlvar

    to see if there really was a variable called @argv.

    Sinan
    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, May 4, 2006
    #7
  8. xhoster

    Lionel Guest

    ***
    DESCRIPTION
    If no import list is supplied, all possible restrictions are
    assumed.
    (This is the safest mode to operate in, but is sometimes too
    strict for
    casual programming.)
    ***

    So leaving it out means it is included anyway? I understand though that
    it is best to include it implicitly.
    I've been posting in usenet for a fair while, what did I do wrong?

    Ok, my bad again. I was using warnings but I didn't use them
    specifically on that script. That was a mistake, I had trimmed down
    script and run it without warnings to just check it did the same thing
    in terms of output and it did. As I said, that is a mistake on my behalf
    for not running it again with warnings.

    I'm just learning of tutorials I find online. I suppose my downfall is
    not taking all the time to go through every detail because I'm fluent in
    a number of programming languages, perl however is a little different.

    Thanks for your help

    Lionel.
     
    Lionel, May 4, 2006
    #8
  9. xhoster

    xhoster Guest

    No, it means leaving out the *import list* out is the same as including
    the full import list.

    use strict;
    is the same as
    use strict "vars", "refs", "subs";

    Both of them are quit different from not having a "use strict" at all.

    If you used strict, it would have told you that @argv wasn't a
    declared variable or a special variable.

    Xho
     
    xhoster, May 4, 2006
    #9
  10. xhoster

    Lionel Guest

    Cheers. I've been using "use strict" for about the last hour now and it
    has been very useful. Things are starting to flow :).

    Lionel.
     
    Lionel, May 4, 2006
    #10
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.