[comp.lang.perl] Question on variable substitution in strings

russell@ccu1.aukuni.ac.nz (Russell J Fulton;ccc032u) (06/18/91)

I have been writing a perl program to implement a generic print command
for our UNIX system. On of the ways we print files is by doing a DEC NET
copy of the file to a vax where a process picks up the file and puts it
in a VMS queue. (You *really* *don't* want to know why we do it this way!)

Anyway I cobble to getter a long file name with various information 
coded into it and then invoke the decnet copy program with system.
here is the fragment of code.


       $ddev='ccvcom::sys\$sysdevice:[decnet.uspool]';


       print "/usr/bin/dn/dncp $file $ddev$$__$printer"."__$USER__$vmsfn\n"; 

       system("/usr/bin/dn/dncp $file $ddev$$__$printer"."__$USER"."__$vmsfn"); 

There are two things that puzzle me. One is why do I need to escape the
$ in the first statement. If I don't then the print statement prints
the string correctly, in the call to system the $sysdevice is
interpreted as a  variable and substituted with a null string.

The other thing is: why do I need to break the string with a "." after
$printer? I could have understood it if I had to break up "$$__" because
perl got confused between $$ and $_.

BTW all the variables in these exprssions contain /\w/.

I am running perl 4.03.

I have this application running now but I would like to understand
why I need to do this and hopefully say myself some time when I next
fall over some thing similar.

Thanks, Russell.
-- 
Russell Fulton, Computer Center, University of Auckland, New Zealand.
<rj_fulton@aukuni.ac.nz>

Andrew.Vignaux@comp.vuw.ac.nz (Andrew Vignaux) (06/18/91)

In article <1991Jun18.051149.25411@ccu1.aukuni.ac.nz>, russell@ccu1.aukuni.ac.nz (Russell J Fulton;ccc032u) writes:
|>   $ddev='ccvcom::sys\$sysdevice:[decnet.uspool]';
|>   system("/usr/bin/dn/dncp $file $ddev$$__$printer"."__$USER"."__$vmsfn"); 
|> 
|> There are two things that puzzle me. One is why do I need to escape the
|> $ in the first statement.

$sysdevice is being interpreted by /bin/sh.

|> The other thing is: why do I need to break the string with a "." after
|> $printer? I could have understood it if I had to break up "$$__" because
|> perl got confused between $$ and $_.

Because '_' is a valid character in an identifier.  An easier
workaround is to use {}s:

    "...__${printer}__..."

To avoid /bin/sh getting its grubbly little hands on your '$'s (or
even worse someone invoking your utility with "USER='; echo gotcha'")
you could invoke:

    system('/usr/bin/dn/dncp', $file, "$ddev$$__${printer}__${USER}__$vmsfn");

i.e. pass the arguments to dncp directly.

Andrew
-- 
Domain address: Andrew.Vignaux@comp.vuw.ac.nz