H
Henry Law
I think I've done my homework on this, including "How can I call
backticks without shell processing?" in perlfaq8, and a Groups search
on "perl shell escape characters backticks" but I'm none the wiser;
pointers would be welcome.
Here's a test program:
-----------------------
#! /usr/bin/perl
use strict;
use warnings;
print "Enter filename:";
my $filename = <STDIN>;
chomp $filename;
my $ret = `ls -m $filename`;
print "Returned value:$ret\n";
-----------------------
The problem comes when the file name that is entered contains the "$"
character, which in my case it often does. In the test directory
there is a file called "test" and another called "$test". If I run
the program above and enter "test" the result is as expected, thus:
$ ./test
Enter filename:test
Returned value:test
But if I enter the name of the "$test" file, the variable $filename is
interpolated a second time, and since there is no "$test" variable
within the program it comes out as null, thus:
$ ./test
Enter filename:$test
Returned value:bashref.html, c, d, (etc.. the whole directory)
Escaping the $ within the backticks won't work, since I do want Perl
to interpolate $filename, so what to do? My current work-round is a
sub called "shell_execute" which takes the string to be executed
(after one level of interpolation) and then escapes the dollar signs
before running backticks. It works, but ITNABWTDI?
Henry Law <>< Manchester, England
backticks without shell processing?" in perlfaq8, and a Groups search
on "perl shell escape characters backticks" but I'm none the wiser;
pointers would be welcome.
Here's a test program:
-----------------------
#! /usr/bin/perl
use strict;
use warnings;
print "Enter filename:";
my $filename = <STDIN>;
chomp $filename;
my $ret = `ls -m $filename`;
print "Returned value:$ret\n";
-----------------------
The problem comes when the file name that is entered contains the "$"
character, which in my case it often does. In the test directory
there is a file called "test" and another called "$test". If I run
the program above and enter "test" the result is as expected, thus:
$ ./test
Enter filename:test
Returned value:test
But if I enter the name of the "$test" file, the variable $filename is
interpolated a second time, and since there is no "$test" variable
within the program it comes out as null, thus:
$ ./test
Enter filename:$test
Returned value:bashref.html, c, d, (etc.. the whole directory)
Escaping the $ within the backticks won't work, since I do want Perl
to interpolate $filename, so what to do? My current work-round is a
sub called "shell_execute" which takes the string to be executed
(after one level of interpolation) and then escapes the dollar signs
before running backticks. It works, but ITNABWTDI?
Henry Law <>< Manchester, England