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

Hello,

"#!/usr/bin/perl -l"
Did you anybody use the -l switch in your program?
How to use it?Is there any real example?
Thank you in advance.

Replies are listed 'Best First'.
Re: How do I use the switch -l?
by rob_au (Abbot) on May 14, 2002 at 05:58 UTC
    See perlrun (or perldoc perlrun) - This switch can be used to modify the input and output line separators, $/ and $\ respectively. The main use for this type of assignment is in the command line for one-liner scripts used for file processing.

    An example modified from perlrun, to trim lines in *.txt files to a maximum width of 80 characters ...

    perl -lpe 'substr($_, (length $_ > 79) ? 80 : length $_) = ""' *.txt

    This code will step through the file, delete all characters beyond the 80 character length of the string and then write the file back out, retaining the original line separator.

     

    Update : Corrected code example to handle lines less than 80 characters in length - Thanks ariels++

     

Re: How do I use the switch -l?
by ariels (Curate) on May 14, 2002 at 08:39 UTC

    I often use it for one-liners. See perlrun for an example involving (GNU) find. The other example is somewhat broken, unfortunately.

    Another example: print second column of file if first starts with an `X'...

    cgn_perl -lane 'print $F[1] if $F[0] =~ /^X/'

    Well, typing an extra \n isn't that bad, but...

Re: How do I use the switch -l?
by strat (Canon) on May 14, 2002 at 10:49 UTC
    just have a look at my signature :-) There, -l stands for adding a newline at the end. I especially use -l for oneliners. If I use it in real programs, I have to to write some comments about why using it, so I hardly ever use it there.

    Best regards,
    perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"