I generally try to avoid operating on @_ and $_ wherever possible, to avoid confusion and pitfalls like what you may be experiencing.@_ and $_ are idiomatic of Perl. The former is much a Perl5 thing, but the latter will also be there in Perl6, more powerful than ever! Their use can be confusing only to non-perl programmers trying to hack at Perl. Also, while you can avoid "operating" on $_ (but only if you want to make your life more complicated than necessary!), @_ is the only means to pass parameters to subs, so you can't avoid to "operate" on it. Or do you prefer to clobber your main namespace of global variables?
Here's some code I whipped up that I think does the operation you desire but in a slightly different way.Good!#!/usr/bin/perl use strict; use warnings;
No need for the initializations.my $var = ''; my @array = ();
While this does work, it is generally advised against as an extremely bad habit as it will slurp in the whole file at a time. Sometimes it doesn't make much harm, sometimes it is necessary, but it is not here and it would be recommendable not to spread bad programming habits amongst newbies.foreach my $line (<DATA>) {
No need whatsoever for this!exit;
In reply to Re^2: Yet another array problem
by blazar
in thread Yet another array problem
by Angharad
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |