Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

here-document without interpolation (<<DELIMITER;)

by gmpassos (Priest)
on Jan 15, 2004 at 23:27 UTC ( #321707=perlquestion: print w/replies, xml ) Need Help??

gmpassos has asked for the wisdom of the Perl Monks concerning the following question:

I was looking for a way to use the here-document (<<DELIMITER;) to set some data section in the class.

But this sections need to be exactly as they where declared, soo, if some variable exists there can't be parsed.

Soo, this data:

$data = <<DATAFOO; bla bla bla $globalvar DATAFOO print "$data\n" ;
prints:
bla bla bla
But I need that:
bla bla bla $globalvar

Looking at perldoc I found

## From perldiag: =item Use of bare << to mean <<"" is deprecated (D deprecated) You are now encouraged to use the explicitly quoted form if you wish to use an empty line as the terminator of the here-document. ## From perlop Customary Generic Meaning Interpolates '' q{} Literal no "" qq{} Literal yes `` qx{} Command yes* qw{} Word list no // m{} Pattern match yes* qr{} Pattern yes* s{}{} Substitution yes* tr{}{} Transliteration no (but see below) <<EOF here-doc yes* * unless the delimiter is ''.

Soo, testing some codes I saw that is possible to write that:

$data = <<'DATAFOO'; bla bla bla $globalvar DATAFOO print "$data\n" ;
And the output is:
bla bla bla $globalvar

OK, I found what I want. But the use of <<'DELIMITER'; is right/maintained? It works, but I never saw that and is not well documented, or I'm the only one that doesn't know that?

Graciliano M. P.
"Creativity is the expression of the liberty".

Replies are listed 'Best First'.
Re: here-document without interpolation
by thelenm (Vicar) on Jan 15, 2004 at 23:38 UTC
    Yes, you found the correct way to do what you were trying to do. The documentation in perlop may not be particularly clear, but the "*" next to "yes" in the "Interpolates" column is explained as "unless the delimiter is ''". So you can use single quotes to prevent interpolation with qx, m//, qr, s///, and here-docs.

    -- Mike

    --
    XML::Simpler does not require XML::Parser or a SAX parser. It does require File::Slurp.
    -- grantm, perldoc XML::Simpler

Re: here-document without interpolation (<<DELIMITER;)
by Roger (Parson) on Jan 15, 2004 at 23:38 UTC
    It is well documented in the perlop documentation. Search for '<<EOF'.

Re: here-document without interpolation (<<DELIMITER;)
by Abigail-II (Bishop) on Jan 15, 2004 at 23:42 UTC
    The use of single quotes around the delimiter is the correct way of doing this.

    Abigail

Re: here-document without interpolation (<<DELIMITER;)
by Coruscate (Sexton) on Jan 16, 2004 at 05:35 UTC

    Heredocs allow you to use a single blank line as your end delimeter if you wish. This means that rather than writing

    print <<"Done"; foo bar baz Done print "aha!\n";

    , you can write

    print <<""; foo bar baz print "aha!\n";

    (note the lack of anything but a blank line between "baz" and the following print statement). It was previously allowed to use

    print <<; foo bar baz print "aha!\n";

    (note the plain and simple "print <<;" line). You can still use that simple version, but it's best not to as there is no guarantee that such functionality will be reserved in future perl versions (and that is why the warnings pragma will cough up that deprecated message if you use "print <<;").

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2020-08-11 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (58 votes). Check out past polls.

    Notices?