Please excuse me for not answering your latest question. Based on this new info, my understanding of you real requirement has changed. I now believe that you want to replace all UNIX variable references in a UNIX pathname with their value the same as UNIX does. You make an exception if the variable is not defined. You want to leave the reference in place. (UNIX would replace it with a null string.) This view of the requirements suggests a much different solution.
use strict;
use warnings;
use Test::More tests => 5;
my %ENV = ( # Overrides the global for this test
playground => 'myplay',
HOME => 'myhome',
le1 => 'fix_for_file1'
);
my @expected = (
q(set playground="myhome/playground")."\n",
q(set file1=myplay'/fifix_for_file1')."\n",
q(set file2=myplay'/file2$')."\n",
q(set file3=myplay'/f$i$l$e$3$')."\n",
q(set file4=myplay'/fi\$le4')."\n",
);
# look
# behind |------ $1 ------|
my $with_braces = qr/ (?<!\\) ( \$ \{ (\w+) \} ) /x;
my $without_braces = qr/ (?<!\\) ( \$ (\w+) ) /x;
# |$2 |
while (<DATA>) {
s!$with_braces !$ENV{$2}//$1!xge;
s!$without_braces!$ENV{$2}//$1!xge;
is( $_ , shift @expected, $_ );
}
__DATA__
set playground="${HOME}/playground"
set file1=${playground}'/fi$le1'
set file2=${playground}'/file2$'
set file3=${playground}'/f$i$l$e$3$'
set file4=${playground}'/fi\$le4'
OUTPUT:
1..5
ok 1 - set playground="myhome/playground"
#
ok 2 - set file1=myplay'/fifix_for_file1'
#
ok 3 - set file2=myplay'/file2$'
#
ok 4 - set file3=myplay'/f$i$l$e$3$'
#
ok 5 - set file4=myplay'/fi\$le4'
#
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.