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.


In reply to Building Paths without Regex by rehann

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.