In the example given, setting the flag is redundant because the &someaction can be called within the loop itself before 'last' is called.
However there are situations where setting the flag is both useful and perhaps even necessary.
Say you've just opened a file for both reading and writing. You've a loop to read each line in the file and do something to it if certain conditions are met or print the line back otherwise. You can't exit the loop with 'last' because that would cause the file to be corrupted (some entries at the end may not have been written back). In this case, it may be useful to set a flag to remember the state and do something about it when the loop is finished...
Did I make sense?
Continuing from the above example where you're opening a file for both reading and writing. Say you want to print (on the screen via the browser) an error message because one of the lines contains an error or something. You can't just exit the loop and print the error message because that will interrupt the writing of the file and corrupt it. You need to set a flag to remember the error and display that error when the loop is finished.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
Outside of code tags, you may need to use entities for some characters:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||