For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2005 > calling an invocation argument from @ARGV









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author calling an invocation argument from @ARGV
Christopher Spears

2005-02-25, 3:57 pm

I'm trying to automate g++ through a Perl script.
Here is what I have written so far:

#!/bin/perl -w
use strict;

my $counter;
$counter = 0;

for (my $i = 0; $i < @ARGV; $i++) {
$counter++;
}

if ($counter == 0) {
print "Not enough arguments!";
print "Usage: ./cedit somefile.C++";
print "Usage (optional): ./cedit somefile.C++
outputfile";
} elsif ($counter == 1) {
system "g++", (first invocation argument);

How do I call individual arguments from the @ARGV
array? Basically, the usage of the script (called
cedit) is:

$./cedit somefile.C++

or

$./cedit somefile.C++ outputfile

The first argument is the .C++ file to be compiled,
and the second argument is the name of the .exe file.
Errin M HMMA/IT Larsen

2005-02-25, 8:55 pm


<<SNIP>>

>=20
> ## you should:
> use warnings;
> ## it will help you in the future
>=20


<<SNIP>>

Oops! I just realized your using that '-w' switch to perl up there.
That IS using the warnings pragma, I believe. Sorry about that!

--Errin
Errin M HMMA/IT Larsen

2005-02-25, 8:55 pm


Hi! And, welcome to Perl!

>=20
> I'm trying to automate g++ through a Perl script.=20
> Here is what I have written so far:
>=20
> #!/bin/perl -w
> use strict;


## you should:
use warnings;
## it will help you in the future

>=20
> my $counter;
> $counter =3D 0;


## With Perl, it's not really necessary to initialize this variable.
It doesn't hurt, but it's just a line you didn't need to type.

>=20
> for (my $i =3D 0; $i < @ARGV; $i++) {
> $counter++;
> }


## This whole loop is not needed. If you use the @ARGV variable in a
Scalar context, it will report how many elements it has
my $number_of_args =3D @ARGV;

>=20
> if ($counter =3D=3D 0) {
> print "Not enough arguments!";
> print "Usage: ./cedit somefile.C++";
> print "Usage (optional): ./cedit somefile.C++
> outputfile";
> } elsif ($counter =3D=3D 1) {
> system "g++", (first invocation argument);=20
>=20


## This can be easily re-written to simplify and make it more
understandable:
if( @ARGV ) {
## Do stuff here
} else {
die "Not enough arguments!\nUsage: $0 somefile.C++\nUsage
(optional): $0 somefile.C++ outputfile";
}

> How do I call individual arguments from the @ARGV
> array? Basically, the usage of the script (called
> cedit) is:
>=20

=20
The answer to this question is two-ways.
1) you can access the specific element in the array:
my $first_arg =3D $ARGV[0];
my $second_arg =3D $ARGV[1];

2) you can shift the args off the top of the array:
my $first_arg =3D shift;
my $seconf_arg =3D shift;

There is a lot going on there. There are also numerous other ways to
get at that info. You should read up on some Perl!


> $./cedit somefile.C++
>=20
> or
>=20
> $./cedit somefile.C++ outputfile
>=20
> The first argument is the .C++ file to be compiled,
> and the second argument is the name of the .exe file.
>=20


Check out the Perl Documentation:

perldoc perldoc
perldoc -f shift

To be honest, I'm not sure where to send you to find out about Perl
arrays in general, or the special @ARGV array specifcally.

You can check out the perldoc FAQ for it:

perldoc -q array

--Errin
John W. Krahn

2005-02-25, 8:55 pm

Christopher Spears wrote:
> I'm trying to automate g++ through a Perl script.
> Here is what I have written so far:
>
> #!/bin/perl -w
> use strict;
>
> my $counter;
> $counter = 0;
>
> for (my $i = 0; $i < @ARGV; $i++) {
> $counter++;
> }


You don't need a loop for that as an array in scalar context returns the
number of elements in the array:

my $counter = @ARGV;


> if ($counter == 0) {


And you don't really need $counter either.

if ( @ARGV == 0 ) {


> print "Not enough arguments!";
> print "Usage: ./cedit somefile.C++";
> print "Usage (optional): ./cedit somefile.C++
> outputfile";


Why not just die() or exit()?

@ARGV or do {
print <<USAGE;
Not enough arguments!
Usage: ./cedit somefile.C++
Usage (optional): ./cedit somefile.C++ outputfile
USAGE
exit 1;
};


> } elsif ($counter == 1) {
> system "g++", (first invocation argument);


my $compiler = 'g++';

system( $compiler, @ARGV ) == 0 or die "system $compiler @ARGV failed: $?";


> How do I call individual arguments from the @ARGV
> array? Basically, the usage of the script (called
> cedit) is:
>
> $./cedit somefile.C++
>
> or
>
> $./cedit somefile.C++ outputfile
>
> The first argument is the .C++ file to be compiled,
> and the second argument is the name of the .exe file.


The string 'somefile.C++' will be in $ARGV[0] and the string 'outputfile' will
be in $ARGV[1]



John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com