Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Strange Global Array issue

by Anonymous Monk
on Apr 16, 2013 at 14:34 UTC ( #1028927=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all!

I've run into a strange issue with a global array that is being populated by values that (on the face of it) I can't figure out why.

I'm using strict/warnings, and have used Data::Dumper to find the point where the value of my global array is changing. Basically, I have a global array of hashes, with each key being the "type" of a regular expression (I have multiple regular expressions to run on a certain file type, with each regular expression looking for/at a specific part of the file) . The global array is declared at the top of the script:

my @regexes = ();

In a foreach loop within a subroutine, I make a call to another sub:

foreach (@regexes){ ($fileBodyMatch, $decode ) = checkFileBody ($file, $_->{bodyregex}); }

The array looks fine before this subroutine call. The value of "$decode" is what I'd expect it to be. In the subroutine checkFileBody, I do the following ($filename is the $file value being passed in) :

open ( DKT, "gunzip -c $filename | ") or die "Can't open pipe to $file +name\n";

At this point, everything still looks good in Dumper. I then do this:

while (<DKT>){ my $line =$_; #process data in here }

Even before I have the "my $line" declaration I have a Data Dumper call to print out the contents of @regexes and the @regexes array contains the last line of the file read and nothing else.

At the conclusion of the sub, the net result is that the @regexes array contains the last line of the file as the first element of the array, followed by the previous (valid) contents.

Thank you in advance!

Comment on Strange Global Array issue
Select or Download Code
Re: Strange Global Array issue
by hdb (Parson) on Apr 16, 2013 at 14:40 UTC

    Have you tried to remove the reliance on $_ in your sub?

    while (my $line = <DKT>){ # my $line =$_; # process data in here }

      First of all, that did the trick. Thanks!

      Second (and more importantly for my understanding of Perl), would you mind explaining why that would be the fix?

        There is only one $_ in your program and you did use it for two different purposes. One has to be careful sometimes with Perl's build-in variables.

        From "Modern Perl", page 7: http://onyxneon.com/books/modern_perl/modern_perl_a4.pdf

        As English gets confusing when you have too many pronouns and antecedents, you must take care mixing uses of $_ implicitly or explicitly. Uncautious simultaneous use of $_ may lead to one piece of code silently overwriting the value written by another. If you write a function which uses $_, you may clobber a caller function's use of $_.

        UPDATE: In order to make your script more robust overall, you might also want to remove $_ from your outer loop.

        foreach my $regex (@regexes){ ($fileBodyMatch, $decode ) = checkFileBody ($file, $regex->{bodyre +gex}); }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1028927]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2014-08-01 02:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls