J
Just in
I'm trying to write regexp search and replace strings on the command line
and have the program I'm calling use them in a substitution. Problem is that
the $[0-9] variables are not being interpreted the way I want them to be.
Heres what I am doing on my Linux box:-
<OUTPUT>
%> try.pl -f to.txt -s 'add (\w+) minus' -r 'FOO$1BAR'
FOO$1BAR
FOO$1BAR
%>
</OUTPUT>
<CODE>
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
# Variable declaration
my $Search = my $Replace = my $File = '';
# Process @ARGV
getopts('f:hr:s:');
our($opt_f, $opt_r, $opt_s);
$opt_f ? $File = $opt_f : &DisplayHelp();
$opt_r ? $Replace = $opt_r : &DisplayHelp();
$opt_s ? $Search = $opt_s : &DisplayHelp();
open(FILE, $File) or die "Can't open $File:- $!";
while(<FILE>)
{
s/$Search/$Replace/;
print;
}
close FILE;
sub DisplayHelp
{
print "To be defined\n";
exit 0;
}
</CODE>
How can I achieve the desired interpretation of $1? Single quotes and double
quotes don't help, nor does qr//
Cheers
Just in
and have the program I'm calling use them in a substitution. Problem is that
the $[0-9] variables are not being interpreted the way I want them to be.
Heres what I am doing on my Linux box:-
<OUTPUT>
%> try.pl -f to.txt -s 'add (\w+) minus' -r 'FOO$1BAR'
FOO$1BAR
FOO$1BAR
%>
</OUTPUT>
<CODE>
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
# Variable declaration
my $Search = my $Replace = my $File = '';
# Process @ARGV
getopts('f:hr:s:');
our($opt_f, $opt_r, $opt_s);
$opt_f ? $File = $opt_f : &DisplayHelp();
$opt_r ? $Replace = $opt_r : &DisplayHelp();
$opt_s ? $Search = $opt_s : &DisplayHelp();
open(FILE, $File) or die "Can't open $File:- $!";
while(<FILE>)
{
s/$Search/$Replace/;
print;
}
close FILE;
sub DisplayHelp
{
print "To be defined\n";
exit 0;
}
</CODE>
How can I achieve the desired interpretation of $1? Single quotes and double
quotes don't help, nor does qr//
Cheers
Just in