a problem with variable interpolation..

K

Kevin

#!/opt/perl_5.8.5/bin

# heres a peice of script which is troublin me since a while. Might as
well be # a newbee question!! The problem is,
# upon single quoting the $sentence ( see below) , and forcing
evaluation
# by putting it in double quotes while printing, the string simply
does
# not get evaluated as no evaluation is specified during its
definition

# upon forcing evaluation by defining $sentence in double quotes, the
# compiler evaluates it as a system path, and does not interpolate the
variables.

# Is there a workaround which would enable evaluation of variables,
without
# evaluating it as a path?
# and thus hopefully print '\dhdu\foo\sdjadf\bar\edjd\huh' ?


$var1 = 'foo';
$var2 = 'bar';
$var3 = 'huh';

$sentence = "\dhdu\$var1\sdjadf\$var2\edjd\$var3";

print "$sentence";
 
T

Tore Aursand

#!/opt/perl_5.8.5/bin

Remember these:

use strict;
use warnings;
$var1 = 'foo';
$var2 = 'bar';
$var3 = 'huh';

$sentence = "\dhdu\$var1\sdjadf\$var2\edjd\$var3";

The '\' will escape anything that comes right after it. You probably want
to escape the '\' character itself, or use the '/' character instead
(which works just fine on multiple platforms).
 
J

Jürgen Exner

Kevin said:
$var1 = 'foo';
$var2 = 'bar';
$var3 = 'huh';

$sentence = "\dhdu\$var1\sdjadf\$var2\edjd\$var3";
print "$sentence";

See "perldoc -q DOS":
Why can't I use "C:\temp\foo" in DOS paths? What doesn't
`C:\temp\foo.exe` work?

On top of that you are escaping the dollar signs, thereby explicitely
telling perl to _not expand the variables $var1, ...

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top