Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Another Q about piping Perl

by stefp (Vicar)
on Oct 12, 2001 at 03:18 UTC ( [id://118365]=note: print w/replies, xml ) Need Help??


in reply to Another Q about piping Perl

The idiom to execute perl statements on and print the result of each line of a file is
perl -pe 'perl_statements' < file
or
some_command | perl -pe 'perl_statements'

each line is stored in $_ on which perl_statements act.

Here you want to print all the files in the $HOME/Mail directory. An all perl solution is:
   map{ -f  $_  and print "$_ " } <$ENV{HOME}/MAIL/*>

Some people consider using map on empty context is bad form. I don't. They would propose instead:   for(<$ENV{HOME}/MAIL/*>) {  -f  $_  and print "$_ " }
or
  -f  $_  and print "$_ " for  <$ENV{HOME}/MAIL/*>

TMTOWTDI

-- stefp

Replies are listed 'Best First'.
(Ovid) Re(2): Another Q about piping Perl
by Ovid (Cardinal) on Oct 12, 2001 at 03:39 UTC

    stefp wrote:

    Some people consider using map on empty context is bad form. I don't.

    I think it depends on the context. If you're doing a short one-liner like that, it's not a big deal. In fact, if you're just writing a small "throw away" script that only gets used once or twice and you don't care about performance or memory, then it's not a big deal IMHO. However, if you're writing large applications, it pays to be rigorous. Creating a bunch of only to throw away the return value is a waste of resources and a for loop is preferable. Doing what you did to print all the files in the $HOME/MAIL/ directory:

    # print all files in the directory (an overblown and limited 'ls' +or 'dir' perl -e '-f $_ and print "$_ " for glob(shift)' ./*

    Side note: since I tested that on a Windows box and it doesn't like single quotes on the command line and I hate using \" all the time to embed quotes, I wrote this:

    perl -e "-f $_ and print $_.$,.$/ for glob(shift)" ./*

    No wonder people say Perl has too much punctuation :)

    Cheers,
    Ovid

    Update: Hmm... seems I wasn't paying attention to blakem's post.

    Vote for paco!

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Re: Another Q about piping Perl
by blakem (Monsignor) on Oct 12, 2001 at 03:25 UTC
    I like map as much as the next guy (see the "extended" Location field on my homenode) but why not write the above map as:
    -f $_ && print "$_ " for <$ENV{HOME}/MAIL/*>

    -Blake

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2025-05-21 02:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.