Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^5: How to replace envs in path?

by BillKSmith (Monsignor)
on May 23, 2022 at 22:35 UTC ( [id://11144149]=note: print w/replies, xml ) Need Help??


in reply to Re^4: How to replace envs in path?
in thread How to replace envs in path?

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' #
Bill

Replies are listed 'Best First'.
Re^6: How to replace envs in path?
by hv (Prior) on May 24, 2022 at 00:08 UTC
    # look # behind |------ $1 ------| my $with_braces = qr/ (?<!\\) ( \$ \{ (\w+) \} ) /x; my $without_braces = qr/ (?<!\\) ( \$ (\w+) ) /x; # |$2 |

    Note that this use of lookbehind is generally not a safe way to look for an unescaped $, because generally when backslashes are used for escaping, they are also used for escaping backslashes themselves: \\$HOME would then be considered an escaped backslash followed by a replaceable variable reference.

    In this context, it's less obvious what is correct since backslashes are not generally being used for escaping. As such, I'm not sure that the spec is coherent as a whole - it looks like there would be no way to express a string that should have one or more actual backslashes followed by a variable.

    Hugo

      Good Catch. I now see that the ovedpol5's second set of examples includes one with a double backslash before the '$'. Unfortunately, he does not give us the results that he expects. The algorithm which he describes in words would treat that '$' as escaped. I cannot tell whether you have found an error common to both solutions, or if I accidentally did 'what he intended'.
      Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11144149]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-24 18:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found