rehann has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to build a path out of variables. I am reading lines from a file with a while loop. I then parse the information into variables. Here is my code
my ($var1) = 'stuff_to_cut_out_with_substr_FOO'; my ($var2) = 'stuff_to_cut_out_with_substr_BAR'; my ($var3) = 'stuff_to_cut_out_with_substr_LOST'; if (/foo/) { $var1 = substr($_, 8); } elsif (/bar/) { $var2 = substr($_, 7); } elsif (/lost/) { $var3 = substr($_, 5); } my ($path) = $var1 . '/' . $var2 . '/' / $var3 say "$path";
The problem is var1 is being replaced in the $path variable. The output looks like "obar/lost" instead of "/foo/bar/lost". When I print just the variables $var1, $var2, and $var3 they have the correct data in them. The problem happens when I try to build them into a string. I've also tried the following code
use File::Spec::Functions; my ($path) = catfile($var1, $var2, $var3);
This second code results in the same output. It seems like some sort of pattern matching is going on but I can't figure this out to save my life.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Building Paths without Regex
by johngg (Canon) on Sep 01, 2011 at 18:09 UTC | |
by rehann (Initiate) on Sep 01, 2011 at 19:18 UTC | |
|
Re: Building Paths without Regex
by Marshall (Canon) on Sep 03, 2011 at 03:10 UTC |