Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Documentation for shifting elements of @ARGV

Reply
Thread Tools

Documentation for shifting elements of @ARGV

 
 
Neil Montgomery
Guest
Posts: n/a
 
      03-09-2005
I have solved my actual programming problem, but was frustrated in my
attempt to determine the behaviour of @ARGV in the online docs.

I want to combine data from two files. This combined data will be sorted
and modified in various ways which aren't of any importance except that
I need to keep track of which file each datum came from.

I expected the following to do what I needed:

#!/usr/bin/perl
use warnings;
use strict;

my @data;
while (<>) {
chomp;
# print "$ARGV, $ARGV[0]\n"; # For debugging
my $file = ($ARGV eq $ARGV[0]) ? "First" : "Second";
push @data, [ $file , split(/,/) ];
}

foreach (@data) { print "@$_\n" };
__END__

I tested this program using two suitable files of three lines each
(appended at the end of this message) and was surprised by the following
output:

Use of uninitialized value in string eq at try line 8, <> line 4.
Use of uninitialized value in string eq at try line 8, <> line 5.
Use of uninitialized value in string eq at try line 8, <> line 6.
Second a b c
Second 1 2 3
Second 2 3 4
Second d f t r
Second 2 3 4 5
Second 5 6 8 8

I inserted the (commented out) print statement and concluded that the
elements of @ARGV are being 'shift'ed, but I wasn't able to find this
behaviour documented the online docs (perlvar, perlop, perlsyn, perldoc
-q, probably others). I had no success with Google either.

I fixed the program by saving a copy of @ARGV and using that copy
instead. Nevertheless I would like help in finding the documentation for
the way @ARGV is handled in this way.

Regards,
Neil

p.s. Here are the contents of the test files I used:

a,b,c
1,2,3
2,3,4

and

d,f,t,r
2,3,4,5
5,6,8,8





 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      03-10-2005
Neil Montgomery <> wrote:

> I have solved my actual programming problem, but was frustrated in my
> attempt to determine the behaviour of @ARGV in the online docs.



@ARGV is an array of _data_.

Data does not _have_ behaviour, code does.

So it must be something else...


> I need to keep track of which file each datum came from.



> while (<>) {



Now we're getting somewhere.

There is an _op_erator that we know makes use of the data in @ARGV.


> and concluded that the
> elements of @ARGV are being 'shift'ed, but I wasn't able to find this

^^^^ ^^^^^
> behaviour documented the online docs (perlvar, perlop, perlsyn, perldoc
> -q, probably others).



It ought to be in perlop.pod somewhere, but it took me a while to
find it too. The FAQ calls it the "diamond operator" but perlop
calls it the "null filehandle".

But you had the search terms that led me to find it:

grep ARGV *.pod | grep shift


See the "I/O Operators" section:

The null filehandle <> is special
...
It really does shift the @ARGV array


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
Fabian Pilkowski
Guest
Posts: n/a
 
      03-10-2005
* Neil Montgomery wrote:
>
> I have solved my actual programming problem, but was frustrated in my
> attempt to determine the behaviour of @ARGV in the online docs.
>
> I want to combine data from two files. This combined data will be sorted
> and modified in various ways which aren't of any importance except that
> I need to keep track of which file each datum came from.
>
> I expected the following to do what I needed:
>
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> my @data;
> while (<>) {
> chomp;
> # print "$ARGV, $ARGV[0]\n"; # For debugging
> my $file = ($ARGV eq $ARGV[0]) ? "First" : "Second";
> push @data, [ $file , split(/,/) ];
> }
>
> foreach (@data) { print "@$_\n" };
> __END__


I think this is a good example to use "eof" without its parenthesis.
Please study `perldoc -f eof` to learn more about eof's behavior.


#!/usr/bin/perl -w
use warnings;
use strict;

my @data;
my $filecnt = 1;

while ( <> ) {
chomp;
push @data, [ $filecnt, split /,/ ];
$filecnt++ if eof;
}

print "@$_\n" for @data;
__END__


regards,
fabian
 
Reply With Quote
 
Arndt Jonasson
Guest
Posts: n/a
 
      03-10-2005

Tad McClellan <> writes:
> Neil Montgomery <> wrote:
>
> > I have solved my actual programming problem, but was frustrated in my
> > attempt to determine the behaviour of @ARGV in the online docs.

>
>
> @ARGV is an array of _data_.
>
> Data does not _have_ behaviour, code does.


The data does not have behaviour, but the variable referring to it may
well have. I don't think there is anything wrong with the expression
"the behaviour of <predefined variable name>", generally. It may be
misleading or misinformed in particular cases, of course.

And code, after all, is just data interpreted by a processor.
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      03-10-2005
Arndt Jonasson <do-not-> wrote:
>
> Tad McClellan <> writes:
>> Neil Montgomery <> wrote:
>>
>> > I have solved my actual programming problem, but was frustrated in my
>> > attempt to determine the behaviour of @ARGV in the online docs.

>>
>>
>> @ARGV is an array of _data_.
>>
>> Data does not _have_ behaviour, code does.

>
> The data does not have behaviour, but the variable referring to it may
> well have.



Do you have an example of that?

(There isn't such an example in this thread, as it is the null
filehandle (input operator) that has the behaviour of
using/changing the data stored in @ARGV.
)

> I don't think there is anything wrong with the expression
> "the behaviour of <predefined variable name>", generally.



We will have to agree to disagree on that then.


> It may be
> misleading or misinformed in particular cases, of course.



I would classify "misleading" as "anything wrong", which is
why I said what I said in the first place.

It misled the OP to looking at @ARGV instead of at <>.


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Arndt Jonasson
Guest
Posts: n/a
 
      03-11-2005

Tad McClellan <> writes:
> Arndt Jonasson <do-not-> wrote:
> >
> > Tad McClellan <> writes:
> >> Neil Montgomery <> wrote:
> >>
> >> > I have solved my actual programming problem, but was frustrated in my
> >> > attempt to determine the behaviour of @ARGV in the online docs.
> >>
> >>
> >> @ARGV is an array of _data_.
> >>
> >> Data does not _have_ behaviour, code does.

> >
> > The data does not have behaviour, but the variable referring to it may
> > well have.

>
>
> Do you have an example of that?
>
> (There isn't such an example in this thread, as it is the null
> filehandle (input operator) that has the behaviour of
> using/changing the data stored in @ARGV.
> )


In Perl, the %ENV variable, for example. Assigning into it causes more
things to happen than just storage of a value (maybe the process
environment is only updated just before a `` or a system(), but that
can't be determined from within Perl code). Also, when using the value
of $!, it returns a string or a number, depending on context, which
most scalar don't. I find it useful to say that the variable "behaves"
in a particular way.

> > I don't think there is anything wrong with the expression
> > "the behaviour of <predefined variable name>", generally.

>
>
> We will have to agree to disagree on that then.


Fine.

> > It may be
> > misleading or misinformed in particular cases, of course.

>
>
> I would classify "misleading" as "anything wrong", which is
> why I said what I said in the first place.
>
> It misled the OP to looking at @ARGV instead of at <>.


That's true, but I took your comment to be relevant for all variables.
(And in all languages, but maybe you didn't mean that. We often do
give advice in this news group about general programming practices.)

I do seem to find at least one use in the documentation. perlretut.pod
has:
"The result C<$^R> is automatically localized, so that it will behave
properly in the presence of backtracking."
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      03-13-2005
Tad McClellan wrote:
> Arndt Jonasson <do-not-> wrote:


>>The data does not have behaviour, but the variable referring to it may
>>well have.

>
> Do you have an example of that?


I would say that

perl -le 'print "$_ ".$|-- for (0..9)'

exhibits a variable that has peculiar behavior.

-Joe
 
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
converting XSD documentation to HTML documentation kev.sully@gmail.com XML 1 09-16-2006 12:09 PM
documentation for web.config elements keithb ASP .Net 2 06-02-2006 03:50 PM
Python documentation: How about structured documentation? Looking for comments/suggestions Kenneth McDonald Python 2 05-06-2004 04:11 AM
Principles of documentation (was: Python Documentation Blows!) Cameron Laird Python 1 04-03-2004 06:54 PM
accessing documentation elements/unhandled attributes via the som wooks XML 1 01-18-2004 11:02 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