Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

__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?


s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Comment on __DATA__ on commandline does not work?
Select or Download Code
Re: __DATA__ on commandline does not work?
by afoken (Parson) 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.

    Alexander

    --
    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 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 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 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.


        s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
        +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
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>;
        __DATA__
        -e syntax OK
    
    And it seems perl tends to ignore the "Text HERE" part.
    Hope this helps a bit. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://834021]
Approved by toolic
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2014-07-24 23:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (167 votes), past polls