develooper Front page | perl.perl5.porters | Postings from April 2003

RE: [perl #21843] AutoReply: fail on substituting values into s/// construct

Thread Previous | Thread Next
From:
wlanahan
Date:
April 7, 2003 08:43
Subject:
RE: [perl #21843] AutoReply: fail on substituting values into s/// construct
Message ID:
7AFE40EF30EE754DA967D1B5968457CA169427@KCCLUST06EVS1.ugd.att.com
Further limitations with s/// reported in bug report 21843

# PROBLEM $1 and $2 in replacement are NOT correlated to pattern
#!/usr/bin/perl -w

my ($from, $to, $text, $line);

$text = "head
middle 1
middle 2
middle 3
tail";

print "\noriginal text\n$text\n";

$from = "(head).*(tail)";
$to = '${1}NEW_MIDDLE${2}';

$text =~ s/$from/$to/s;

print "\nfinal text\n$text\n";

########################################################################
########
###### Here try to substitute entire expression
###### used various quoting and backslashing of $str when defined
###### tried eval, and {}, () on $text =~ line

#!/usr/bin/perl -w

my ($from, $to, $text, $line);

$text = "head
middle 1
middle 2
middle 3
tail";

print "\noriginal text\n$text\n";

$str = quotemeta's|(head).*(tail)|${1}NEW_MIDDLE{2}|s';
#$str = 's|(head).*(tail)|\${1}NEW_MIDDLE\${2}|s';

$text =~ $str;   # <=== try to substitute entire expression
                 # replacement is always literal

print "\nfinal text\n$text\n";

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About