perl newbie - getting length of argv array

X

xhoster

Hi all,

Just starting to learn perl.

You should almost always use strict and use warnings.
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";

Perl is case sensitive. There is no @argv, unless you define one.
There is an @ARGV.


Xho
 
L

Lionel

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.
 
A

A. Sinan Unur

Lionel schreef:


#!/usr/bin/perl


Missing:
use strict;
use warnings;



my $length = @argv;

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 <[email protected]>
(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
 
L

Lionel

You should almost always use strict and use warnings.

What is strict? I was using warnings but didn't included it on this post.
Perl is case sensitive. There is no @argv, unless you define one.
There is an @ARGV.

Cheers, that got it. I also noticed that I had "@array" instead of @ARGV
above . . . bad copy and paste :).

Lionel.
 
L

Lionel

Thanks to both replies :).

Quick correction:

my $length = @ARGV;

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.
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
}

I was getting to that eventually :).

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

Lionel.
 
A

A. Sinan Unur

What is strict? I was using warnings but didn't included it on this
post.

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 <[email protected]>
(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
 
L

Lionel

A. Sinan Unur said:
perldoc strict

***
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.
While you are at it, you should read the posting guidelines as well

I've been posting in usenet for a fair while, what did I do wrong?

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.

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.
 
X

xhoster

***
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?

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
 
L

Lionel

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.

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top