pythondude has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm looking into mking this code shorter if posible.

while(<STDIN>){print if m/$ARGV[0]/;}

Replies are listed 'Best First'.
Re: perl grep clone
by choroba (Cardinal) on Apr 01, 2016 at 21:45 UTC
    Why?

    # 1 2 3 # 1234567890123456789012345678901234567 while(<STDIN>){print if m/$ARGV[0]/;} /$ARGV[0]/&&print while<STDIN> # 123456789012345678901234567890 # 1 2 3

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Why?

      Because I'm trying to learn

      Also, thanks, I was not aware of the && operator

Re: perl grep clone
by RonW (Parson) on Apr 01, 2016 at 23:01 UTC
    # 5 0 5 0 5 7 $p=shift;print grep/$p/,<>;

    and works for any number of files as well as STDIN

    However, the content of all the files will be "slurped" into memory because <> is in list context.

    To read line by line:

    # 5 0 5 0 5 0 2 $p=shift;while(<>){print if/$p/}
      I like this answer because It still allows the default filenames on commandline arguments to stdin behavior with <>.
Re: perl grep clone (CLI & perlrun)
by LanX (Saint) on Apr 02, 2016 at 12:07 UTC
    I have the impression your motivation comes from the desire to emulate grep from command line.

    So maybe you want to have a look at -l -a -n -e -p ...(and so on) options in perlrun ?

    e.g.

    -n

    causes Perl to assume the following loop around your program, which makes it iterate over filename arguments somewhat like sed -n or awk:

    LINE: while (<>) { ... # your program goes here }

    Note that the lines are not printed by default. See -p to have lines printed. If a file named by an argument cannot be opened for some reason, Perl warns you about it and moves on to the next file.

    update

    As replacement for

    grep PATTERN *

    try

    perl -ne'/PATTERN/&&print' *

    for a start. :)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re: perl grep clone
by BrowserUk (Patriarch) on Apr 01, 2016 at 22:11 UTC

    23:

    # 5 0 5 0 3 print grep/$ARGV[0]/,<>;

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

      This dosen't work for me because perl tries to interpret the pattern as a file name, but I found it does not use this behavior when using <STDIN> instead of <>.

      29:

      # 5 0 5 0 5 9 print grep/$ARGV[0]/,<STDIN>;
        I cheat a little and assume only one argument on the command line:
        print grep/@ARGV/,<STDIN>;
Re: perl grep clone
by stevieb (Canon) on Apr 02, 2016 at 01:34 UTC

    Nevermind... it took a sec to figure out I had to pass an arg in ;)

    It doesn't look like your code does anything different than:

    # 5 0 4 print while<>;