Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: How to write a program to read stdin or command line?

by ikegami (Pope)
on Oct 02, 2007 at 16:28 UTC ( #642158=note: print w/ replies, xml ) Need Help??


in reply to Re: How to write a program to read stdin or command line?
in thread How to write a program to read stdin or command line?

if that's what the OP means by "stdin or command line", you could also use -p instead of -n:

perl -ple"$_ .= 'world'" file


Comment on Re^2: How to write a program to read stdin or command line?
Select or Download Code
Re^3: How to write a program to read stdin or command line?
by BrowserUk (Pope) on Oct 02, 2007 at 17:35 UTC
    if that's what the OP means by "stdin or command line",

    Seems it was.

    you could also use -p instead of -n:

    You could, but I wouldn't as it is less flexible than -n.

    If you want to omit some lines from the output, you can't do that if you are also using -l


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Assuming I was talking about the general case, how is -p less flexible? And how does -l matter?

      perl -nle "print bar($_) unless foo($_)"
      perl -ple "next if foo($_); $_=bar($_)"

        perl -ple "next" still prints every line, so, yes, -p is less flexible based on your fine examples.

        If you want to omit some lines from the output, you can't do that if you are also using -l

        My first reaction to that is that the final -l was supposed to be a -p. That would certainly make sense to me, but I can't say for sure that it is what was intended.

        - tye        

        With -p, whilst you can conditionally undef $_ to avoid it being printed; if you are also using -l, you will get unwanted blank lines:

        C:\test>type junk.txt 1 2 3 4 5 C:\test>perl -nle"$_ & 1 and print" junk.txt 1 3 5 C:\test>perl -ple"$_ & 1 or undef $_ and next" junk.txt 1 3 5

        And in fact the next in your example serves no purpose:

        C:\test>perl -ple"$_ & 1 or undef $_" junk.txt 1 3 5

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2014-10-24 20:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (137 votes), past polls