Re: Here, doccy...*woof*
by footpad (Abbot) on Jun 21, 2001 at 06:28 UTC
|
It is written in the POD of perldata (version 5, release 6):
A line-oriented form of quoting is based on the shell "here-document" syntax. Following a << you specify a string to terminate the quoted material, and all lines following the current line down to the terminating string are the value of the item. The terminating string may be either an identifier (a word), or some quoted text. If quoted, the type of quotes you use determines the treatment of the text, just as in regular quoting. An unquoted identifier works like double quotes. There must be no space between the << and the identifier. (If you put a space it will be treated as a null identifier, which is valid, and matches the first empty line.) The terminating string must appear by itself (unquoted and with no surrounding whitespace) on the terminating line.
Granted it's a little oblique, but it is there--as someone once reminded me. Since then, I have seen it mentioned in a couple of other places, including (if memory serves) Rat, Camel (currently 50% off, via that link), and the Spikey Ball.
--f
| [reply] [d/l] [select] |
|
yes, and from perlop,
Quote and Quote-like Operators
While we usually think of quotes as literal values, in Perl they function as operators, providing various kinds of interpolating and pattern matching capabilities. Perl provides customary quote characters for these behaviors, but also provides a way for you to choose your quote character for any of them.
nowhere does it imply (to me) that there are situations where you can use " but not qq(), so i would expect to be able to use
print <<qq~DOUBLE~;
$foo
DOUBLE
but i can't.
and that doesn't explain or demonstrate other weird uses of heredoc syntax, for example,
my @list = (<<HERE, <<THERE, <<EVERYWHERE);
cluck, cluck
HERE
baa, baa
THERE
e i e i o
EVERYWHERE
print "with a ", shift @list, "and a ", shift @list, "singing ", shift
+ @list;
so i'd still like a tutorial. but maybe i'll have to write it myself.
| [reply] [d/l] [select] |
|
Since then, I have seen it mentioned in a couple of other places, including (if memory serves) Rat, Camel (currently 50% off, via that link), and the Spikey Ball.
nit: It's not a rat, it's a mouse! Quick quote from the Colophon:
The animal featured on the cover of CGI Programming for the World Wide Web is a mouse, a rodent of the family Muridae.
This ends today's extremely pointless fact, g'day!
Dr. Michael K. Neylon - mneylon-pm@masemware.com
||
"You've left the lens cap of your mind on again, Pinky" - The Brain
| [reply] |
Re: Here, doccy doccy. nice doccy. heredoc, treat.
by CheeseLord (Deacon) on Jun 21, 2001 at 06:35 UTC
|
A small question, perhaps, but why would you need to use here docs with qw? The following would do what (I think) you're trying to do with your final section of code:
my @list = qw(
Foo Bar Baz
);
print "@list\n";
Will print out Foo Bar Baz unless you have a different list item separator...
I'm more than willing to concede that I likely am just not thinking hard enough (or I don't know enough) to envision a case where this wouldn't be optimal, but... I just don't see why here-docing things like qw would ever be necessary (or really, even desirable).
Although I can't deny it is interesting.
Update: (re: jeffa) Point taken. :)
Update #2: (re: Vynce) True, there could (should?) be some uses for this sort of thing in the future. I guess I was just thinking in "Why?" instead of "Why not?" mode.
His Royal Cheeziness | [reply] [d/l] [select] |
|
| [reply] |
Re: Here, doccy doccy. nice doccy. heredoc, treat.
by Abigail (Deacon) on Jun 22, 2001 at 04:40 UTC
|
Anything that allows for interpolated context (which means
qq, qx and qr) can
fairly easily be adapted to use here docs:
print `${\<<HERE}`;
date
HERE
or
print qx {${\<<HERE}};
date
HERE
And for qx:
print qr /${\<<HERE}/;
x*foo
HERE
For q and qq, we already know what to do,
single or double quote the delimeter. Which leaves us with
qw, which is fairly simple too:
my @words = split ' ' => <<'HERE';
some words go here
HERE
-- Abigail
| [reply] [d/l] [select] |
Re: Here, doccy doccy. nice doccy. heredoc, treat.
by chipmunk (Parson) on Jun 21, 2001 at 19:24 UTC
|
Next you'll be wanting to quote the q() delimiters with qq():
q qq(DELIMITER)Here is my happy string!qq(DELIMITER);
Sorry, you just can't do that.
On the other hand, you could get the effect of qw() with a heredoc like this:
my @list = split(' ', <<HERE);
some whitespace
separated words
HERE
| [reply] [d/l] [select] |
|
my @list = split(' ', <<'HERE');
some whitespace
separated words
and possibly $a
varaiable or $2
HERE
note 'HERE' rather than HERE. qw does not interpolate.
otherise, yes, that solution does work -- for qw(). and i'm sure a similar solution could be made to work for qr//. and of course q(), qq() and qx() are all trivial (though the special case qx'' is not so trivial, as far as i can tell). but perl6 introduces new quoting mechanisms (including, i think, customizable quoting mechanisms), and i might like to be able to use heredocs with those.
although, i guess i do take your point -- the quotes around the HEREDOC delimiter are not actually being used as those quotes in that case. so maybe heredoc could have an optional second argument (don't ask me how, i'm jsut thinking out loud here) that says what quoting mechanism to use. (ok, nevermind, i'll suggest how after all) like
print <<(HEREDOC, qx);
date
df
HEREDOC
(currently, that gives this warning:
Use of bare << to mean <<"" is deprecated at everywhere.pl line 23.
so we'd not be breaking good existing uses of heredocs.)
.
| [reply] [d/l] [select] |
Re: Here, doccy doccy. nice doccy. heredoc, treat.
by particle (Vicar) on Jun 22, 2001 at 00:24 UTC
|
just one nit to pick...
in your example above,
print <<IMPLIED;
$foo
IMPLIED;
you have a semi-colon after the string terminator. this won't compile on my ActiveState Perl build 623, with or without strict and warnings. i've only been using Perl since 5.005_03, so i'm not sure if this is a holdover from earlier versions, or a bug. otherwise, your example is accurate, for *NIX. on Win32 of course, the `date` call waits for user input, and outputs something like
The current date is: Thu 06/21/2001
Enter the new date: (mm-dd-yy) date
when enter is pressed. yuck.
~Particle | [reply] [d/l] [select] |
|
hey, i just noticed something in my output from running 'date' in a heredoc on Win32. look closely at the output:
The current date is: Thu 06/21/2001
Enter the new date: (mm-dd-yy) date
when date is run from the command line, the output is:
The current date is: Thu 06/21/2001
Enter the new date: (mm-dd-yy)
so why does date (the contents of $foo) appear when run from a heredoc with backtick interpolation? this seems to be more than just a fancy way of quoting. can someone verify this behaviour on *nix?
curiouser and curiouser...
=Particle | [reply] [d/l] [select] |
|
well, if you've copy-pasted my version, you'll note that after printing `date`, it prints "date" -- which would make it appear to be right after the backticked version. move the IMPLIED heredoc to before the BACK heredoc aand you should see that change.
also, my version of date and yours are different; mine doesn't ask for the new date if called with no arguments. sorry for the inconvenience; i hope i didn't make your machine forget the date.
.
| [reply] |
|