Re: 'perl -e' and '__DATA__' What's wrong?
by shmem (Chancellor) 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}
| [reply] [d/l] [select] |
|
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
| [reply] [d/l] [select] |
|
$ 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 | [reply] [d/l] [select] |
|
|
|
Re: 'perl -e' and '__DATA__' What's wrong?
by ikegami (Patriarch) 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.
| [reply] [d/l] [select] |
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:
- you have an AppleScript application, which does some stuff and sets some "AppleScript variables"
- then the AppleScript thing invokes a perl command-line ("one-liner") script, which must:
- accept some stuff from stdin (some other process invoked by AppleScript is feeding the perl one-liner)
- accept some additional command line args? (or maybe not, or maybe this doesn't matter)
- 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) | [reply] |
|
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
| [reply] [d/l] [select] |
|
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}
| [reply] [d/l] |
|
Re: 'perl -e' and '__DATA__' What's wrong?
by Anonymous Monk on Dec 07, 2007 at 08:01 UTC
|
| [reply] |