in reply to Replacing environment variables
if (/(\$[^\/\n\t]*)/) {
What if the presumed variable is $f.`rm -rf $ENV{HOME}`? Oops. Lotsa space now..
my $var = $&; my $key = $var; substr($key, 0, 1) = "";
You capture your match with () and then use $&? And what do you have $var for? Why not $key = $1?
Leave the the $ out of your capturing parens. That way you haven't got to strip it with substr.
I'd write your loop as
while(<FILE>) { if (my ($key) = /\$(\w+)/) { defined($ENV{$key}) and s/\$$key/$ENV{$key}/ or $undefinedVars++; } print; }
but it's usefulness depends on the input file...
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Replacing environment variables
by loris (Hermit) on Oct 18, 2006 at 10:32 UTC | |
by holli (Abbot) on Oct 18, 2006 at 12:54 UTC | |
by loris (Hermit) on Oct 18, 2006 at 14:03 UTC |