• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Quick perl question

bigshooter

Platinum Member
here's some quick perl code that I wrote, and I'm wondering why it's giving me errors.
I want to exit the program and output a mesage if there are no command line arguments to this program so i just use

if (!@argv)
{
print "useage $0: command line args;
exit (1);
}

and yes i know i could use die instead, but i'm too used to c++. my problem is when I add this to my code, it gives me this

Global symbol "@argv" requires explicit package name at prog11.pl line 21.
Execution of prog11.pl aborted due to compilation errors (#1)
Uncaught exception from user code:
Global symbol "@argv" requires explicit package name at prog11.pl line 21.
Execution of prog11.pl aborted due to compilation errors.

i'm just curious why you can access elements of @argv, or do something like $temp = shift ...which uses @argv as default,
but you can't say if argv is empty kill the prog. Anyone that is a little more advanced with perl than I am know this answer? Also, any other better ways to check for arguments?

Thanks.


 
It's ARGV. You need to use the array in a scalar context to get the upper bound...

if (!scalar(@ARGV)) {
# usage...
}

if ($#ARGV == -1) {
# usage...
}

If you simply want to process arguments, there are easier ways:

use Getopt::Long;
my $filename= '';

GetOptions('filename=s' => \$filename);

Lookup Getopt and friends in the docs.
 
if you want simple command line argument checking (ie: pre-determined usage of arguments, where script.pl -ln != script.pl -n -l), you can simply use the contents of @ARGV, with the first argument in $ARGV[0];

if you want complicated command line argument processing (ie: script.pl -ln == script.pl -n -l), you will need to
use Getopt::Std;
die "\n" if !getopts("ln", \%opts);

more info on Getopt::Std
 
i was just going to use

if (!$argv[0])

but I was curious why you couldn't use @argv in an expression like that. If you declare an array, my @temp, then you can say if (!@temp). Is the !@argv line not working because its a default variable, or does it have something to do with global scope, or is it just something that doesn't work?
 
you almost got that right, but remember perl in case sensitive
@ARGV (capital letters) is the default list/array to store the command line arguments entered by user
@argv is something you can create and use later if you want to ... has nothing to do with CLA
 
HAHAHAHAHAHAHHAHAHHA.

God i'm an idiot. I rely on default variables too much, the extent of my perl coding has been just shifting elements of ARGV like

$temp = shift.

I completely forgot that its ARGV, not argv. The first guy posting tried to tell me that I think, I just didn't pay attention. I thought I was really getting the hang of this perl stuff, oh well.. I should have caught that.

Thanks.
 
Back
Top