metty has asked for the wisdom of the Perl Monks concerning the following question:
Hello,
I cannot figure out why my variable looses its content after applying a regular expression on it. The program is running in a mod_perl environment using Perl 5.14.2. The following code snippet is part of an object method. An interesting fact: sometimes its working, sometimes its not.
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"; ...
Here is the output from the apache log files
1 MMP_MODULES 2 SCALAR(0x7f80ab361968) 3 SCALAR(0x7f80ab361968) 4 MMP_MODULES 1 TMP_MODULE_HASH 2 SCALAR(0x7f80ad04ed68) 3 SCALAR(0x7f80ad04ed68)
Use of uninitialized value in concatenation (.) or string at myfile.pm + line 1061.
The error message indicates the line storing the print command with number 4.
During the first run, referencing the value 'MMP_MODULES', the program is working fine. The debug code send to STDERR, which is passed to the Apache log files, prints the expected result. During its second run, using the value 'TMP_MODULE_HASH', the regular expression is validated fine, but the referenced value gets undefined. I printed the pointer value. Interesting enough, the pointer to the memory block does not get modified. Just the value gets lost. Any idea why?
I can fix this by creating a local copy before applying the regular expression:
if (defined($$l_Name_ref)) { my $l_Name = $$l_Name_ref; if ($l_Name =~ /^[a-zA-Z]\w+$/) { ...
Using this code, it is always working. As the code is in a central part of my program, which is executed hundreds of times during a web page call, I would like to avoid copying a value without a need.
How can a regular expression check modify the content of a variable?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Loosing variable content after regular expression
by Eily (Monsignor) on Dec 18, 2014 at 16:49 UTC | |
by metty (Initiate) on Dec 19, 2014 at 08:27 UTC | |
|
Re: Loosing variable content after regular expression
by Corion (Patriarch) on Dec 19, 2014 at 08:21 UTC | |
by metty (Initiate) on Dec 19, 2014 at 08:39 UTC | |
by AnomalousMonk (Archbishop) on Dec 19, 2014 at 18:29 UTC | |
|
Re: Loosing variable content after regular expression
by Anonymous Monk on Dec 18, 2014 at 19:48 UTC |