Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

one-liner yields unexpected result

by plendid (Sexton)
on Feb 13, 2012 at 17:39 UTC ( #953503=perlquestion: print w/replies, xml ) Need Help??
plendid has asked for the wisdom of the Perl Monks concerning the following question:

I'm attempting to list all the filenames found in 94 volumes . This is easy to do within a Perl program,

use File::Find::Rule; foreach (0..93) {print File::Find::Rule->new->file->in("/.mnt.$_")}

which yields lots of file-names. But when i attempt to execute this code as a one-liner,

perl -m-File::Find::Rule -e "foreach (0..93) {print File::Find::Rule->new->file->in(qq(/.mnt.$_))}"

i get nothing.

I looked at perlrun but didn't get a clue. And this particular problem seems hard to look up. I suspect the fault is mine, not understanding the nuances of Perl one-liners. The code itself is a greatly simplified version of my real program. The reason for preferring one-lining is that the code is executed via ssh.

Thanks for whatever help you can provide.

Replies are listed 'Best First'.
Re: one-liner yields unexpected result
by JavaFan (Canon) on Feb 13, 2012 at 17:53 UTC
    If you're on Unix, use single quotes. The $_ is interpreted by your shell.

      Single quotes did it. Thank you.

Re: one-liner yields unexpected result
by BrowserUk (Pope) on Feb 13, 2012 at 17:53 UTC


      -m- tells Perl to load a specified module before running the program. In my case File::Find::Rule.

        Try to use -m instead of -m-. From the documentation (perldoc perlrun)
        If the first character after the -M or -m is a dash (-) then the 'use' is replaced with 'no'.
        -m- tells Perl to load a specified module before running the program.

        No, it should be -mFile::Find::Rule, not -m-Fi...

      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".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

      Its odd but it works :)

      $ perl -m-File::Find::Rule -e "foreach (0..93) {print File::Find::Rule +->new->file->in(qq(/.mnt.$_))}" $ perl -MO=Deparse -m-File::Find::Rule -e "foreach (0..93) {print File +::Find::Rule->new->file->in(qq(/.mnt.$_))}" use File::Find::Rule (); foreach $_ (0 .. 93) { print 'File::Find::Rule'->new->file->in("/.mnt.$_"); } -e syntax OK
Re: one-liner yields unexpected result
by educated_foo (Vicar) on Feb 13, 2012 at 18:14 UTC
    This is even easier to do with the Unix find(1) command:
    find /.mnt.* -type f -print
    There's more than one way to do it, and often the easiest way isn't Perl.
      That would list the content of /.mnt.94, /.mnt.01 and /, none of them will be listed by the OPs snippet.
        Sure -- you can always use /.mnt.{1..93} (in Bash and Zsh, at least) if that is an actual problem.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://953503]
Approved by Corion
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2017-09-23 21:55 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (273 votes). Check out past polls.