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

one-liner yields unexpected result

by plendid (Acolyte)
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.

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

    -m-...?

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

        -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?

        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'.

      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 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 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 /.mnt.foo, 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://953503]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (12)
As of 2014-07-10 13:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (212 votes), past polls