For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2008 > Really literal string









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Really literal string
Keenlearner

2008-03-25, 7:09 pm

I like to store multiline perl code inside a scalar variable, but I
don't want perl to interpolate the variable inside the code.

my $t = "abc";

my $s = <<TEXT;
This is a test $t
TEXT

The above code still interpolate the string. I know that I can escape
the variable with backslash, but the code inside is much more
complicated than that as the would be subroutine as well. Any method
that I can use ? Thank you.

Chas. Owens

2008-03-25, 7:09 pm

On Tue, Mar 25, 2008 at 12:01 PM, Keenlearner <yingun@gmail.com> wrote:
> I like to store multiline perl code inside a scalar variable, but I
> don't want perl to interpolate the variable inside the code.
>
> my $t = "abc";
>
> my $s = <<TEXT;
> This is a test $t
> TEXT
>
> The above code still interpolate the string. I know that I can escape
> the variable with backslash, but the code inside is much more
> complicated than that as the would be subroutine as well. Any method
> that I can use ? Thank you.


#!/usr/bin/perl

use strict;
use warnings;

my $s = 'foo
$bar
baz';

print "$s\n";

$s = <<'END_OF_STRING';
$s used to hold just:
foo
$bar
baz
END_OF_STRING

print $s;


--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
Rob Dixon

2008-03-25, 7:09 pm

Keenlearner wrote:
> I like to store multiline perl code inside a scalar variable, but I
> don't want perl to interpolate the variable inside the code.
>
> my $t = "abc";
>
> my $s = <<TEXT;
> This is a test $t
> TEXT
>
> The above code still interpolate the string. I know that I can escape
> the variable with backslash, but the code inside is much more
> complicated than that as the would be subroutine as well. Any method
> that I can use ? Thank you.


If you put your terminating identifier in single-quotes, it behaves as
if the entire here-document is in single-quotes:

my $s = << 'TEXT';
This is a test $t
TEXT

HTH,

Rob
Gunnar Hjalmarsson

2008-03-25, 7:09 pm

Keenlearner wrote:
> I like to store multiline perl code inside a scalar variable, but I
> don't want perl to interpolate the variable inside the code.
>
> my $t = "abc";
>
> my $s = <<TEXT;
> This is a test $t
> TEXT
>
> The above code still interpolate the string. I know that I can escape
> the variable with backslash, but the code inside is much more
> complicated than that as the would be subroutine as well. Any method
> that I can use ?


Use single quotes.

my $s = <<'TEXT';

http://perldoc.perl.org/perlop.html#Single-Quotes

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jenda Krynicky

2008-03-25, 7:09 pm

From: Keenlearner <yingun@gmail.com>
> I like to store multiline perl code inside a scalar variable, but I
> don't want perl to interpolate the variable inside the code.
>
> my $t = "abc";
>
> my $s = <<TEXT;
> This is a test $t
> TEXT
>
> The above code still interpolate the string. I know that I can escape
> the variable with backslash, but the code inside is much more
> complicated than that as the would be subroutine as well. Any method
> that I can use ? Thank you.


Please compare

my $you = 'Keenlearner';
print "Hi $you\n";
print 'Hi $you\n';
print "\n\n";


print qq{Hi $you\n};
print q{Hi $you\n};
print "\n\n";


print <<TEXT;
Hi $you
What's up?
TEXT

print <<"TEXT";
Hi $you
What's up?
TEXT

print <<'TEXT';
Hi $you
What's up?
TEXT



HTH, Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

Jim Gibson

2008-03-25, 7:09 pm

In article
<a505a9d4-fba4-46b0-b913-56c98ac4c338@s19g2000prg.googlegroups.com>,
Keenlearner <yingun@gmail.com> wrote:

> I like to store multiline perl code inside a scalar variable, but I
> don't want perl to interpolate the variable inside the code.
>
> my $t = "abc";
>
> my $s = <<TEXT;
> This is a test $t
> TEXT
>
> The above code still interpolate the string. I know that I can escape
> the variable with backslash, but the code inside is much more
> complicated than that as the would be subroutine as well. Any method
> that I can use ? Thank you.
>


Enclose the terminating string in single quotes, and the enclosed
"here" document will be in single-quote context:

my $s = <<'TEXT';
This is a test $t
TEXT

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Gunnar Hjalmarsson

2008-03-25, 7:09 pm

Chas. Owens wrote:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $s = 'foo
> $bar
> baz';
>
> print "$s\n";
>
> $s = <<'END_OF_STRING';
> $s used to hold just:
> foo
> $bar
> baz
> END_OF_STRING
>
> print $s;


It should be noted that those methods are not quite equivalent.

C:\home>type test.pl
my $s = 'My programs are stored in
C:\\Program Files
';
print $s;

$s = <<'EOS';
My programs are stored in
C:\\Program Files
EOS
print $s;

C:\home>perl test.pl
My programs are stored in
C:\Program Files
My programs are stored in
C:\\Program Files

C:\home>

A single quotes here-document is the safest way to ensure that nothing
gets changed.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com