in reply to Re:x2 'perl -p' ne 'cat'
in thread 'perl -p' ne 'cat'

Which only begs the question why does it work when -p is placed on the shebang line of a script, as opposed to needing -e 1 when used on a command line?

For the same reason you *never* need -e if you run the program from a file - either by using the shebang line, or by doing perl program.

Perl uses the following strategy to find the program:

  1. If there are one or more -e arguments, that's where the program was found.
  2. If the command was invoked as perl [options] file, the program text is found in the file.
  3. Otherwise, the program text is found on STDIN.

You might say, what about the shebang line? The shebang line is handled by the kernel - the interpreter found on the shebang line is started, and the content of the file (including the shebang line) is passed on STDIN. (This also explains why such interpreters always use # as the start of the comment).

Abigail

Replies are listed 'Best First'.
Re: Re:x2 'perl -p' ne 'cat'
by Juerd (Abbot) on Jun 04, 2002 at 09:55 UTC

    2. If the command was invoked as perl options file, the program text is found in the file. 3. Otherwise, the program text is found on STDIN.

    This is one step, iirc. The filename is the first non-option argument given on the command line, where "-" is STDIN, and it defaults to "-".

    The shebang line is handled by the kernel

    perl does its own interpretation too. This is why files with "#!/usr/bin/perl -w" still get warnings, even if called with "perl foo.pl". This is only if it's the first line.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.