Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

perl one-liner doesn't autochomp input

by bronto (Priest)
on Sep 07, 2004 at 09:56 UTC ( [id://388975]=perlquestion: print w/replies, xml ) Need Help??

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

Hello

I read this great article about Perl Command-Line Options, printed it and had it on my desk. Today I needed to accomplish a simple task: having the output of an ls -1 (that's "minus one") and output a single line with all the filenames separated by ":". In other days I would have written it this way:

ls -1 | perl -e '@_=<>;chomp@_;print join":",@_'

and in fact it just works. But after reading the article, and in particular this:

Using -l and giving it no value has two effects. Firstly, it automatically chomps the input record, and secondly, it sets $\ equal to $/. If you give -l an octal number (and unlike -0 it doesn't accept hex numbers) it sets $\ to the character represented by that number and also turns on auto-chomping.

I expected I could write my one-liner this way:

ls -1 | perl -le 'print join ":",<>'

but it actually doesn't what I expected. Could anybody explain where I am doing wrong?

Thanks a lot

Ciao!
--bronto


The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
--John M. Dlugosz

Replies are listed 'Best First'.
Re: perl one-liner doesn't autochomp input
by broquaint (Abbot) on Sep 07, 2004 at 10:05 UTC
    The first effect of using -l is better documented in perlrun
    It has two separate effects. First, it automatically chomps $/ (the input record separator) when used with -n or -p.
    Not taking full advantage of perl's you could always write your one-liner like this
    ls -1 | perl -le 'print join ":", map /(.*)$/, <>'
    HTH

    _________
    broquaint

Re: perl one-liner doesn't autochomp input
by Anonymous Monk on Sep 07, 2004 at 10:13 UTC

      Oh, damn! How I would like to front-page this! So GREAT!

      Update: apart that it adds a trailing ":", which I don't like to be there :-(

      Update: so do also tachyon's and keszler's solutions: the trailing ":" should not be there

      Ciao!
      --bronto


      The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
      --John M. Dlugosz
        ls|xargs|perl -pe 'y/ /:/' fixes the trailing ':' issue as xargs converts the ls output into a nice space separated stream.
      $ ls | perl -l72pe1

      This works and seems equivalent to

      $ ls | perl -l72pe "{}"

      But I don't understand the role of "1" (or digits in general) at the end of the command line, and I can't find any explanation in perlrun. I thought that they might affect the way that arguments are processed, but that doesn't seem right. Can someone clarify?

      Thanks,
      --John

        The 1 is there because you have to supply some Perl code to -e, but in this case you don't care to do anything, you just want the side effects of the other switches. Using a 1 by itself as your "script" is just like having 1; on a line by itself in a script — it does nothing.

        This can be used for such funny command lines as

        perl -please textfile # print the contents of the textfile perl -deal # fire up the debugger for interactive fiddling

        These are equivalent to

        perl -p -l -e '"ase"' textfile perl -d -e '"al"'

        where you simply have a string on a line by itself as the only statement in your "code", resulting in a no-op.

        Makeshifts last the longest.

Re: perl one-liner doesn't autochomp input
by tachyon (Chancellor) on Sep 07, 2004 at 10:42 UTC
    ls | perl -pe 's/\n/:/'

    Removed redundant -n from perl Sidhekin notes that -np == -p ; also removed -1 from ls because you don't need it either.

Re: perl one-liner doesn't autochomp input
by keszler (Priest) on Sep 07, 2004 at 11:06 UTC
    ls -1 | perl -l0 -ne 'print $_, ":"'

    works. In the 5.6.1 and 5.8.4 versions of perlrun, the -l switch is documented to auto-chomp only when used with the -n or -p switches. You need both the chomp and a null $\.

Re: perl one-liner doesn't autochomp input
by Aristotle (Chancellor) on Sep 07, 2004 at 18:04 UTC

    Offtopic, but a solution to the ultimate problem:

    perl -le'print join ":", glob "*"'

    Or if you let the shell do the work for you

    perl -le'print join ":", @ARGV' *

    Or maybe

    perl -le'$,=":";print<*>' perl -le'$,=":";print@ARGV' *

    Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-18 07:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found