Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

'perl -e' and '__DATA__' What's wrong?

by Skeeve (Vicar)
on Dec 07, 2007 at 07:28 UTC ( #655597=perlquestion: print w/ replies, xml ) Need Help??
Skeeve has asked for the wisdom of the Perl Monks concerning the following question:

I discovered yesterday that it is impossible to read the __DATA__ section when your script comes via -e from the command line. Is this a fact or am I just missing something?

Example:

$ perl -e 'print <DATA>,; __DATA__ example data '

I'm calling the perl script from AppleScript so this is the reason why I use -e. When I use <<< instead of -e it works fine, but then I can't use STDIN in my script to read other input.


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

Comment on 'perl -e' and '__DATA__' What's wrong?
Select or Download Code
Re: 'perl -e' and '__DATA__' What's wrong?
by Anonymous Monk on Dec 07, 2007 at 08:01 UTC
    You're missing a file?
Re: 'perl -e' and '__DATA__' What's wrong?
by shmem (Canon) on Dec 07, 2007 at 08:05 UTC
    The -e switch tells perl to treat the next argument on the command line as a script.

    The __DATA__ token causes perl to re-open $0, seek to the position of the __DATA__ and leave the filehandle DATA open pointing to the contents after that line.

    $ perl -le 'print $0' -e
    I'm not surprised a file handle cannot be associated with text passed into perl as a command line argument. -e is no file proper.

    Maybe you could use

    $ perl -e 'print <<EOT; example data EOT '

    instead.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

      Unfortunately <<'EOT' has the same limitations as using <<<, meaning: STDIN is already used for the script and so I can't pipe other content in.

      My current solution is, that I use a variable like this:

      my $DATAvar = <<'EOT'; example data here EOT

      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
        STDIN is already used for the script and so I can't pipe other content in

        I'm probably misunderstanding something but

        $ cat cccc zzz 999 $ cat cccc | perl -e ' > print <<'EOT'; > abc > 123 > EOT > print while <>;' abc 123 zzz 999 $

        I can read from STDIN here. Have I got the wrong end of the stick?

        Cheers,

        JohnGG

Re: 'perl -e' and '__DATA__' What's wrong?
by ikegami (Pope) on Dec 07, 2007 at 08:39 UTC

    It is an undocumented limit resulting from the choice of code used to implement __DATA__. (Hey a bug! although probably only a documentation bug.)

    DATA is (effectively?) the file handle from which Perl reads the source, left pointing at the start of the line after the __DATA__ token.

    Since -e doesn't read from a source file, DATA isn't a file handle to the source file.

Re: 'perl -e' and '__DATA__' What's wrong?
by graff (Chancellor) on Dec 08, 2007 at 03:03 UTC
    Let's see if I have this right:
    1. you have an AppleScript application, which does some stuff and sets some "AppleScript variables"
    2. then the AppleScript thing invokes a perl command-line ("one-liner") script, which must:
      1. accept some stuff from stdin (some other process invoked by AppleScript is feeding the perl one-liner)
      2. accept some additional command line args? (or maybe not, or maybe this doesn't matter)
      3. accept some additional data which cannot be mixed with the stdin stream, and is not safe to provide as command-line args (for @ARGV), and this data content involves (some of) those "AppleScript variables".

    If AppleScript provides a way to open a file for output, write variables to that file, and close it, then the perl script could simply open a given file, and read from that instead of from __DATA__ -- or I suppose you could go to the trouble of writing the whole perl script to a file, including the runtime-determined __DATA__ content, then execute it.

    Or maybe you could turn things around: have a perl script that invokes AppleScript (via the "osascript" shell command), and reads the data it needs from that? (Just guessing)

      Okay then! Here is how my AppleScript-invoked perl scripts work. I often put them inside Applescript because this way I can use Drag & Drop or some other nice stuff AppleScript offers.

      Note! This example won't work, because I use -e and DATA

      property some_user_changeable_config_stuff : "this data can be changed by the user of the script usually this is some kind of template, containing placeholders the perl script will fill later. Note: The user usually knows nothing about perl but a bit about AppleScript. So he will happily be using stuff like @ Quotes % etc. " on run tell me to open { choose file } end run on open some_items repeat with this_item in some_items if info for this_item is not folder then do_stuff_with(this_item) end if end repeat en open on do_stuff_with(an_item) do shell script "perl -e " & (quoted form of " use strict; use warnings; # read DATA here e.g.: my $TEMPLATE; { local $/; $TEMPLATE= <DATA>; } # and now process while (<>) { # do something with the line and the # TEMPLATE } __DATA__" & (ASCII character 10) & some_user_changeable_config_stuff) +& " " & (quoted form of POSIX path of an_item) end do_stuff_with

      Of course I can check the "some_user_changeable_config_stuff" variable that a delimiter I choose isn't contained. I just wanted to avoid this, using __DATA__. So this post isn't a request for help, but just for clarification/explanation.


      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
        Then I'd rewrite that as follows
        on do_stuff_with(an_item) do shell script "perl -e " & (quoted form of " use strict; use warnings; use Getopt::Long; # get Template from the command line my $TEMPLATE; GetOptions("template=%s",\$TEMPLATE); # and now process while (<>) { # do something with the line and the # TEMPLATE } ") & " " & (quoted form of "--template=" & some_user_changeable_config +_stuff) & " " & (quoted form of POSIX path of an_item) end do_stuff_with

        yay, first AppleScript hacking I did did do! ;-)

        Dunno whether I got everything right, but the idea is to stuff into a command line argument that which you wanted to stuff into the DATA section.

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2014-08-27 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (237 votes), past polls