gcthompson has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have the following function:
sub getSubjects($) { my $ref_sRecord = $_[0]; my @aSubNos = $$ref_sRecord =~ m/<subno>(.+?)<\/subno>/sig; .... }
If the contents of $$ref_sRecord contains :
.... <level>1</level> <subject> <subno>2.1.c</subno> <subtxt>Subject 1</subtxt> <subno>2.2</subno> <subtxt>Subject 2</subtxt> </subject> ....
This array aSubNos should contain 2.1.c and 2.2 (in this case).   This works fine for the majority of data but on occasion it fails and doesn't match anything. For some reason, replacing
my @aSubNos = $$ref_sRecord =~ m/<subno>(.+?)<\/subno>/sig;
with
my $sString = $$ref_sRecord; my @aSubNos = $sString =~ m/<subno>(.+?)<\/subno>/sig;
makes it work consistently across all of my data.   The perl script is running on Perl v5.8.4 (sun4-solaris).   The question is, why? This doesn't make any sense to me.

janitored by ybiC: formatting tweaks for legibility

Replies are listed 'Best First'.
Re: Weirdness with matching into an array
by ambrus (Abbot) on Jan 26, 2005 at 15:02 UTC

    This is just a wild guess, but is it possible that the pos of $$ref_sRecord is not zero when you do the g-matching? That would explain why copying the string helps.

Re: Weirdness with matching into an array
by dave_the_m (Monsignor) on Jan 26, 2005 at 11:01 UTC
    Can you produce a short (but complete) test case?

    Dave.

      Hi Dave, I stripped out a lot of the code to produce a test harness for you and magically the problem went away (i removed several DBI calls and several similar functions). Going back to my original source code; I have several similar bits of code and if i make the same changes as detailed in my original post it causes other regex to fail in peculiar ways. I don't know how to proceed with this, I think it's something to do with memory or the passing of references. Unfortunately I cannot provide you with my original source code as I suspect my employer would not be keen on this :) Thanks for your help Cheers Gordon