in reply to -p option with __DATA__

#! /usr/bin/perl -p __DATA__ hello goodbye

Is the same as:

#! /usr/bin/perl while ( <> ) { } continue { print } __DATA__ hello goodbye

You are not doing anything with the DATA filehandle.

Replies are listed 'Best First'.
Re^2: -p option with __DATA__
by toolic (Bishop) on Nov 19, 2009 at 16:10 UTC
      I guess what confuses me is that
      perl -MO=Deparse script.pl
      and
      perl -MO=Deparse < script.pl
      give (almost) the same output, but
      perl script.pl
      hangs as described, whereas
      perl < script.pl
      prints what I expected.

        You shouldn't be surprised that something that reads from STDIN behaves differently when you redirect STDIN than when you don't.

        "<" means "pretend what's in the file is being typed". When you don't specify a file to execute, perl reads the program from the STDIN. It stops reading when it receives and EOF signal or when it sees __END__ or __DATA__.

        If your program proceeds to read from STDIN, so be it. What it reads has nothing to do with whether the program has used __DATA__ or not.

        To show it's got nothing to do with __DATA__:

        BEGIN { print "Enter a line:\n"; print "You entered: ", scalar(<>) } print("a\n"); print("b\n");
        >perl script.pl Enter a line: foo You entered: foo a b >perl < script.pl Enter a line: You entered: print("a\n"); b
Re^2: -p option with __DATA__
by 7stud (Deacon) on Nov 19, 2009 at 16:17 UTC

    ...which uses the 'diamond operator'. Normally, the diamond operator takes all the file names specified on the command line as arguments, combines the corresponding files into one big file, and then assigns a line from the big file to $_ each time through the loop. However, if you do not specify any file names as arguments on the command line, then the diamond operator reads from STDIN. Presumably, that is what you did, so your program is waiting for you to type in a line of text for the diamond operator to process (you would hit ctrl+D to signal end-of-file after typing in all your lines).

    Also, when you specify filenames as arguments on the command line, if you specify a file name as: -, then the diamond operator will read from STDIN for that portion of the big file.

      Normally, the diamond operator takes all the file names specified on the command line as arguments,

      Only when ARGV is provided as the handle or not handle is provided (since the diamond operator uses ARGV by default). That's because the diamond operator does none of that. It's all ARGV's doing.

      combines the corresponding files into one big file

      Yes, without actually combining the files on disk or in mem. In fact, using eof (with no argument or parens), you can detect when it's going to move to another file.

      and then assigns a line from the big file to $_ each time through the loop

      No, the diamond operator neither creates a loop nor assigns to $_. while provides the looping.

      However, if you do not specify any file names as arguments on the command line, then the diamond operator reads from STDIN.

      Again, only if ARGV is used as the handle since it's all ARGV's doing.

      if you specify a file name as: -, then the diamond operator will read from STDIN for that portion of the big file.

      ARGV uses the two arg version of open. It also allows arguments such as 'zcat compressed.gz |'. In other words, without using a shell, you can achieve the same using bash's <( zcat compressed.gz )