Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

next and it's friends are clever little calls in Perl. Did you know you can do this evil thing?

foreach my $thing (@array) { process($thing); add_to_processed($thing); } sub process { my $thing = shift; next if boring($thing); .... } sub add_to_processed { my $thing = shift; last if too_many($thing); .... }

Now just imagine that process and add_to_processed are hidden away in another module, or something like that. Your foreach loop looks like it processes each thing in the array and then adds it to the list of processed things. But it doesn't. Some things are skipped, silently, others are left behind when add_to_processed decides it's had enough.

Call one of these subroutines outside of a loop and watch as sometimes they break, and sometimes they don't! Gotta love it. (Yes, I was given some code a little like this to maintain once....)

At least eval was telling you about your mislaid next. ;)

Personally I try to keep eval statements around the smallest scope possible. So I'd be writing:

for my $case ( @cases ) { my $is_interesting = eval { interesting($case) }; print "$case had errors:$@" if $@; next unless $is_interesting; eval { process($case) }; if ($@) { print "$case had errors:$@"; } }

or some functional equivalent.

Your advice is good. Making warnings fatal and other such sweeping changes definately require a code review or at least careful thought.

Hope you're having fun.

jarich

In reply to Re: Unexpected action at a distance with use warnings FATAL=>'all'; by jarich
in thread Unexpected action at a distance with use warnings FATAL=>'all'; by demerphq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-19 20:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found