Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

__DATA__ on commandline does not work?

by Skeeve (Vicar)
on Apr 10, 2010 at 17:53 UTC ( #834021=perlquestion: print w/replies, xml ) Need Help??
Skeeve has asked for the wisdom of the Perl Monks concerning the following question:

Hi! I just noticed that this, entered on the commandline (bash / OS X 10.5) dos not work:

perl -e 'print <DATA>; __DATA__ line 1 line 2 '

Whereas this similar code works:

perl <<'XXX' print <DATA>; __DATA__ line 1 line 2 XXX

Can someone explain why the first version does not work?


Replies are listed 'Best First'.
Re: __DATA__ on commandline does not work?
by ikegami (Pope) on Apr 10, 2010 at 20:09 UTC
    DATA is the handle from which Perl reads the source. No handle is used for -e, so no DATA.
Re: __DATA__ on commandline does not work?
by The Perlman (Beadle) on Apr 10, 2010 at 19:17 UTC
    IIRC the -e switch means eval, and IMHO you cant use __DATA__ in eval.

    The second form you showed is completely different since you are using a shell (bash?) here-doc which is similar to execute a file.

    Anyway there is no big point in using __DATA__ within -e'...' since the shell offers plenty of possibilities to pipe the data into your snippet.

    for instance

    perl -e'print <>' <<XXX > 1 > 2 > 3 > XXX 1 2 3
Re: __DATA__ on commandline does not work?
by repellent (Priest) on Apr 10, 2010 at 20:11 UTC
    Here's a way that works without -e for Unix shells:
    $ perl print <DATA>; __DATA__ line 1 line 2 ^D line 1 line 2

    Hit Ctrl-D at ^D.
      Expanding that to use a here doc.:
      #!/bin/bash perl << HDEND print <DATA>; __DATA__ line 1 line 2 HDEND

        ...which s almost exactly my second codefragment in the question.

Re: __DATA__ on commandline does not work?
by afoken (Abbot) on Apr 10, 2010 at 18:19 UTC

    I know that perl reads its code from STDIN in the second case, STDIN is connected to a pipe filled by the shell. So you have a working file handle.

    I guess that in the first case, the -e argument is stuffed directly into the parser, no real file handle, thus no DATA file handle, so no output.


    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: __DATA__ on commandline does not work?
by pid (Monk) on Apr 12, 2010 at 09:05 UTC

    Hi Skeeve,
    I've tried this:

        $ perl -MO=Deparse -wle 'print <DATA>; __DATA__ Text HERE'
        BEGIN { $^W = 1; }
        BEGIN { $/ = "\n"; $\ = "\n"; }
        print <DATA>;
        -e syntax OK
    And it seems perl tends to ignore the "Text HERE" part.
    Hope this helps a bit. :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://834021]
Approved by toolic
Front-paged by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2018-03-24 02:46 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (297 votes). Check out past polls.