Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Common Perl Pitfalls

by Jenda (Abbot)
on Apr 09, 2012 at 23:55 UTC ( #964230=note: print w/ replies, xml ) Need Help??


in reply to Common Perl Pitfalls

Re: Regex in a loop The first while statement is perfectly fine ... if you intend to modify the variable. The first loop reads "while the variable still matches the regular expression do something with the variable" while the second reads "while there's still something more to match in the variable do something with the match". The rule is that in the first case you SHOULD modify the variable within the loop, while in the second case you SHOULD NOT modify it.

Re: Deleting some array elements You should use grep():  @filtered = grep {whatever('test', $you, want_with($_, 'aliased to an array element'))} @all;

Re: Slurping gone wrong my $data = do {local $/; <INPUTFILE>}; or use File::Slurp

Re: True is 1, false is...? Don't print just the value, print some more info to make sure you are looking at the result of the print statement you think you are and always put some kind of quotes around the variable: print "Computed the number of angels: >$angel_count<\n";

Jenda
Enoch was right!
Enjoy the last years of Rome.


Comment on Re: Common Perl Pitfalls
Select or Download Code
Re^2: Common Perl Pitfalls
by Joe_ (Beadle) on Apr 10, 2012 at 18:15 UTC

    I agree on almost all counts.
    I remember having used the option you talked about (modifying the regex and matching in the loop without 'g') before. I just don't like it, though. I feel that it's quite unstable and will turn into an infinite loop the second you're not looking...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://964230]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (16)
As of 2014-09-02 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (24 votes), past polls