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

I have a question about an one-liner that I saw on the net ( collection of Tom Christiansen one liners ).
Here it is:
# just lines 15 to 17 perl -ne 'print if 15 .. 17' *.pod
It reminds me on patern ranges in awk. Could anyone explain what's going under the hood of this code?

Thanks,
Emil

Replies are listed 'Best First'.
Re: Explanation of an one-liner
by sauoq (Abbot) on Oct 27, 2003 at 23:47 UTC
    It reminds me on patern ranges in awk. Could anyone explain what's going under the hood of this code?

    There's no need to; perldoc perlop explains it nicely already:

    In scalar context, ".." returns a boolean value. The operator is bistable, like a flip-flop, and emulates the line-range (comma) operator of sed, awk, and various editors.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Explanation of an one-liner
by PodMaster (Abbot) on Oct 28, 2003 at 00:18 UTC
    Let's not forget B::Deparse
    E:\>perl -MO=Deparse,-p -ne"print if 15 .. 17 " LINE: while (defined(($_ = <ARGV>))) { ((15 .. 17) and print($_)); } -e syntax OK E:\>perl -MO=Deparse -ne"print if 15 .. 17 " LINE: while (defined($_ = <ARGV>)) { print $_ if 15 .. 17; } -e syntax OK
    Now if still there is some confusion (like the range operator), perldoc my friend, perldoc ;)

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Explanation of an one-liner
by BrowserUk (Patriarch) on Oct 28, 2003 at 00:05 UTC

    If the POD explanation isn't enough, it wasn't for me having never used sed or awk to any great extent, then this might clarify things.

    perl -e"open ARGV,'<',$ARGV[0]or die $!; while(<ARGV>){ print if $. >= +15 and $. <=17 }" file

    This is (roughly) analogous to the one-liner you showed except it will only handle a single input file. The open and while parts are performed for you by the -n command line switch. See perlrun for details.

    The 15 .. 17 is (roughly) equivalent to the if condition above.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!

Re: Explanation of an one-liner
by QM (Parson) on Oct 27, 2003 at 23:55 UTC
    See perlman:perlop for Range Operators.

    If either operand of scalar .. is a constant, the if is true when $. == 15, 16, or 17.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of