generic declaration of variables

E

Ela

I am declaring the following variables at the beginning:

@usage='cg.pl afile gfile ffile outname gensf genqcf genpf';
die "Usage: @usage\n" if $#ARGV < @usage;

my ($afile, $gfile, $ffile, $outname, $gensf, $genqcf, $genpf);

I have 2 more questions here, the first, instead of copying the @usage to
the my(), how can I better write the codes to avoid typo and work? I will
use exactly the same name indeed and don't know how many more I will add. I
don't like config file because it usually makes program no longer work after
a period of maintenance (config file gets lost etc).
The second question is, how to allow the user input the arguments without
restricting them in a specific order? e.g. gfile ahead of afile...?
I find that some of the arguments are necessary, while some others, such as
flags, may be optional. How can I write it in a better way to check whether
all the necessary arguments are provided instead of fixing $#ARGV < @usage?
 
S

sandy_saydakov

I am declaring the following variables at the beginning:

@usage='cg.pl afile gfile ffile outname gensf genqcf genpf';
die "Usage: @usage\n" if $#ARGV < @usage;

my ($afile, $gfile, $ffile, $outname, $gensf, $genqcf, $genpf);

I have 2 more questions here, the first, instead of copying the @usage to
the my(), how can I better write the codes to avoid typo and work? I will
use exactly the same name indeed and don't know how many more I will add. I
don't like config file because it usually makes program no longer work after
a period of maintenance (config file gets lost etc).
The second question is, how to allow the user input the arguments without
restricting them in a specific order? e.g. gfile ahead of afile...?
I find that some of the arguments are necessary, while some others, such as
flags, may be optional. How can I write it in a better way to check whether
all the necessary arguments are provided instead of fixing $#ARGV < @usage?

I would suggest using Getopt::Long http://perldoc.perl.org/Getopt/Long.html

/sandy
http://myperlquiz.com/
 
B

benkasminbullock

I am declaring the following variables at the beginning:

@usage='cg.pl afile gfile ffile outname gensf genqcf genpf';
die "Usage: @usage\n" if $#ARGV < @usage;

my ($afile, $gfile, $ffile, $outname, $gensf, $genqcf, $genpf);

I have 2 more questions here, the first, instead of copying the @usage to
the my(), how can I better write the codes to avoid typo and work?

What I would do is to use a hash (here called %args) to store the
variables:

#! perl
use warnings;
use strict;
my @usage=qw/afile gfile ffile outname gensf genqcf genpf/;
die "Usage: cg.pl @usage\n" if @ARGV < @usage;
my %args;
@args{@usage} = @ARGV;
print join (" ", %args),"\n";

$ ./usage.pl a b c d e f
Usage: cg.pl afile gfile ffile outname gensf genqcf genpf

$ ./usage.pl a b c d e f g
genpf g genqcf f gensf e gfile b outname d ffile c afile a
I will
use exactly the same name indeed and don't know how many more I will add. I
don't like config file because it usually makes program no longer work after
a period of maintenance (config file gets lost etc).
The second question is, how to allow the user input the arguments without
restricting them in a specific order? e.g. gfile ahead of afile...?

It's not possible for me to answer that question without information
about how the program knows how to distinguish between gfile, afile,
etc.
I find that some of the arguments are necessary, while some others, such as
flags, may be optional. How can I write it in a better way to check whether
all the necessary arguments are provided instead of fixing $#ARGV < @usage?

I'll leave that as an exercise for you to attempt yourself.
 
C

ccc31807

In your script below, I have not seen the construction used on line 7.

On Mar 20, 7:48 pm, "(e-mail address removed)"
#! perl
use warnings;
use strict;
my @usage=qw/afile gfile ffile outname gensf genqcf genpf/;
die "Usage: cg.pl @usage\n" if @ARGV < @usage;
my %args;
@args{@usage} = @ARGV;
print join (" ", %args),"\n";

@ARVG is the array passed in from the CL.
@usage is the array created on line 4.
%args is the hash you use to link @ARGV and @usage.

I understand how you can iterate through the hash like this, where
each hash element has the (scalar) name $args{$key}:
foreach my $key (keys %args){print "\t$key => $args{$key}\n";}

What I DON'T understand is the use of the @ symbol to declare array
elements, as in @args{@usage}. I've searched for documentation of this
usage but have not found it. I can see how you would pass @ARGV to
@usage to copy one array to another, as in @second = @first.

I can't see how you can represent %args as @args, even though
accessing individual elements by $args{} is clear. I would appreciate
it greatly if you could explain the workings to me, or refer me to
some documentation of this feature.

Thanks, CC.
 
C

ccc31807

`perldoc perldata` offers....

If you're confused about why you use an '@' there on a hash slice
instead of a '%', think of it like this. The type of bracket (square or
curly) governs whether it's an array or a hash being looked at. On the
other hand, the leading symbol ('$' or '@') on the array or hash
indicates whether you are getting back a singular value (a scalar) or a
plural one (a list).

Thanks. I was afraid that I would get an answer like this, 'You do it
that way because Larry said to.' I understood the operation because I
had modified the code in question and had run it several times to
determine if it in fact worked as advertised. It did.

I didn't understand the concept, but now I see it. I'm not entirely
comfortable with it, but I at least have a head knowledge, and assume
that the experiential knowledge will come with use. Perl has been
accused of being willfully obscure, and at moments like this I
sympathize with this accusation.

Thanks again for your explanation. It did indeed do the trick.

CC
 
C

ccc31807

I don't understand that statement at all -- every language has it's
syntax rules ... When writing C we use parens to indicate function
calls, not because Dennis said to, but because that's what the
compiler will accept.

I didn't mean to be insulting. Maybe I should have said, 'You do it
this way because this is the way you do it.' I remember the first time
anyone really explained objects to me (in Java). I had been writing
Java the way it's taught, classes, objects, properties, methods, etc.,
just by rote. Then, one day, someone who really knew what he was doing
explained ADTs, the difference between primitive types and classes,
allocation of the heap, references, and so on. It made a lot more
sense with a mental picture of using 'new' to allocate storage for an
object based on a class definition.

It's one thing to follow the rules, it's another to understand the
rationale for the rules. I understand the necessity of conforming to
the syntax of a language, but I also attempt to understand the reason
for the syntax.

Thanks again, CC.
 
P

Peter J. Holzer

And of course the C compiler will accept it because Dennis said it
should ;-).

I didn't mean to be insulting. Maybe I should have said, 'You do it
this way because this is the way you do it.' [...]
It's one thing to follow the rules, it's another to understand the
rationale for the rules. I understand the necessity of conforming to
the syntax of a language, but I also attempt to understand the reason
for the syntax.

You'd have to ask Larry why he chose this particular syntax. One reason
why Perl is "strange" is that Larry - unlike most programming language
designers - is a linguist. So he borrowed lots of concepts from natural
languages. The sigils are articles (the English language has only one
specific article "the", which is used for singular and plural and all
genders, but most European languages distinguish between singular and
plural - so does Perl).

hp
 
B

Ben Bullock

I didn't understand the concept, but now I see it. I'm not entirely
comfortable with it, but I at least have a head knowledge, and assume
that the experiential knowledge will come with use. Perl has been
accused of being willfully obscure, and at moments like this I
sympathize with this accusation.

When we put a $ in front of a hash or array, we tell Perl to think of the
hashes and arrays as if they are scalars:

for (my $i=0;$i<@ARGV;$i++) {
$args{$usage[$i]} = $ARGV[$i];
}

When we put a @ there, we tell Perl to think of a hash or array as a
whole array:

@args{@usage} = @ARGV;

So Perl is very clever, it can understand that we want to bung a whole
list of stuff into a hash indexed by another list of stuff without typing
"<", "=", "0", "$" seven times, "+" twice, "[]" twice, "()", "{}", and ;
once, and also without having to type "for", or "ARGV" twice, or "$i"
five times.
 

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
474,209
Messages
2,571,088
Members
47,684
Latest member
sparada

Latest Threads

Top