There's nothing wrong with the piece of code you have provided (except that it does nothing and does not even compile because it's incomplete). To get an answer, you should have tried to obtain a minimal reproducting exemple, which would probably have shown you what was wrong in the process.

The issue probably comes from where you get your reference, if you have $ref = \SOMETHING where SOMETHING comes from a regex (the most obvious case being $ref = \$1;, running another match may change the value your reference points to. For exemple, the following code has a issue similar to yours, except it fails at the first run:

my $l_Name_ref; for (qw/HELLO PERL MONKS/) { /(.+)/; $l_Name_ref = \$1; if (defined($$l_Name_ref)) { print STDERR "1 $$l_Name_ref\n" if (defined($$l_Name_ref)); print STDERR "2 $l_Name_ref\n"; if ($$l_Name_ref =~ /^[a-zA-Z]\w+$/) { print STDERR "3 $l_Name_ref\n"; print STDERR "4 $$l_Name_ref\n"; } } }

Edit: I changed my example, in the previous version I used @ARGV and shifted from it because of my first attempt at reproducing the defect but it did not make much sense in this last version.


In reply to Re: Loosing variable content after regular expression by Eily
in thread Loosing variable content after regular expression by metty

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.