Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Ok I lied, there is a difference between the longhand perl while (<>) { } statement and -n: you can't use next or last to exit the loop - however you can use BEGIN{ } and END { } blocks to get pre and post loop code, run a text file containing some numbers through this baby:
perl -l -n -e 'BEGIN{$sum=0;} $sum=$sum+ $1 if /(\d+)/;print $1; END { +print "Total: ",$sum}'
You were right the first time. There really is no difference. Why don't next and last work for you?
echo -e "a\nb\nc" | perl -lne 'next if /b/; print' a c echo -e "a\nb\nc" | perl -lne 'last if /b/; print' a
In fact, it is equivalent to LINE: while (defined($_=<ARGV>)) { ... } which means that you can do next LINE if you have a loop within your one-liner.

Two other comments: I use the canonical

perl -lne 'print $s += $1 if /(\d+)/'
for summing numbers. For one-liners, it's much less typing, and gives you pretty much everything you need: the total sum, and a running sum that still allows you to catch errors.

And finally, the one big missing piece in this tutorial is that it is absolutely necessary, for reasons of tradition, to at least try to make your options spell out words. So never use -alne when -lane would work.


In reply to Re: Uncommon* but Useful Perl Command Line Options for One-liners by sfink
in thread Uncommon* but Useful Perl Command Line Options for One-liners by Sol-Invictus

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



  • 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:
    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
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (4)
    As of 2014-10-25 09:35 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (142 votes), past polls