Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > generic declaration of variables

Reply
Thread Tools

generic declaration of variables

 
 
Ela
Guest
Posts: n/a
 
      03-21-2008
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?


 
Reply With Quote
 
 
 
 
sandy_saydakov@yahoo.com
Guest
Posts: n/a
 
      03-21-2008
On Mar 20, 5:14 pm, "Ela" <e...@yantai.org> wrote:
> 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/
 
Reply With Quote
 
 
 
 
benkasminbullock@gmail.com
Guest
Posts: n/a
 
      03-21-2008
On Mar 21, 9:14 am, "Ela" <e...@yantai.org> wrote:
> 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.
 
Reply With Quote
 
ccc31807
Guest
Posts: n/a
 
      03-21-2008
In your script below, I have not seen the construction used on line 7.

On Mar 20, 7:48 pm, "benkasminbull...@gmail.com"
> #! 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.
 
Reply With Quote
 
ccc31807
Guest
Posts: n/a
 
      03-21-2008
On Mar 21, 9:41 am, Lawrence Statton <yankeeinex...@gmail.com> wrote:
> `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
 
Reply With Quote
 
ccc31807
Guest
Posts: n/a
 
      03-21-2008
On Mar 21, 10:50 am, Lawrence Statton <yankeeinex...@gmail.com> wrote:
> ccc31807 <carte...@gmail.com> writes:
> > Thanks. I was afraid that I would get an answer like this, 'You do it
> > that way because Larry said to.'

>
> 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.
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      03-22-2008
On 2008-03-21 19:02, ccc31807 <> wrote:
> On Mar 21, 10:50 am, Lawrence Statton <yankeeinex...@gmail.com> wrote:
>> ccc31807 <carte...@gmail.com> writes:
>> > Thanks. I was afraid that I would get an answer like this, 'You do it
>> > that way because Larry said to.'

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


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

 
Reply With Quote
 
Ben Bullock
Guest
Posts: n/a
 
      03-22-2008
On Fri, 21 Mar 2008 08:13:44 -0700, ccc31807 wrote:

> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
not just generic type programming,but also parallism generic syntaxprogramming?? minlearn C++ 2 03-13-2009 05:17 PM
generic interfaces with generic methods Murat Tasan Java 1 02-03-2009 12:17 PM
Generic iterator declaration Hansel Stroem C++ 1 09-27-2008 02:06 AM
Generic class in a non generic class nramnath@gmail.com Java 2 07-04-2006 07:24 AM
maxplusII error: a deferred constant declaration without a full declaration is not supported Noah VHDL 5 04-07-2006 02:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57