in reply to 'perl -p' ne 'cat'

Suppose it wouldn't. Then you would no longer be able to start off a program with
#!/usr/bin/perl -p
and execute the file the program is in.

You do know how the kernel deals with #!, don't you?

Abigail

Replies are listed 'Best First'.
Re:x2 'perl -p' ne 'cat'
by grinder (Bishop) on Jun 03, 2002 at 20:00 UTC
    I pondered this on the way home. I know you can use -p on the shebang line, but in that case, the question to ask would be what happens when you run a script that contains

    #! /usr/bin/perl -p

    ... and nothing else. Curiously enough, this does do the same thing as cat(1). 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? The command line behaviour looks like a bug to me.


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
      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

        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.
        

Re: 'perl -p' ne 'cat'
by hacker (Priest) on Jun 04, 2002 at 16:13 UTC