Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

recursive s///g core dumps

by I0 (Priest)
on May 17, 2002 at 03:02 UTC ( #167187=perlquestion: print w/ replies, xml ) Need Help??
I0 has asked for the wisdom of the Perl Monks concerning the following question:

At least it does in perl, v5.6.1 built for IP32-irix and perl, v5.6.0 built for IP22-irix Anyone know if it's fixed in v5.8?
#!/usr/bin/perl -w use strict; print doFunc( "AABBCC",0 ); print doFunc( "AAAABBBBCCCC",0 ); sub doFunc { my( $theParam,$depth) = @_; print STDERR "dofunc($theParam)\n"; my $re; $re = ${['(.+)\1']}[$depth]||'(.+)(\1)'; print STDERR join"\n", $theParam =~ /$re/g,''; $theParam =~ s/$re/&doFunc($1,$depth+1)/ge; ++$theParam; print STDERR "=$theParam\n"; return $theParam; }

Comment on recursive s///g core dumps
Download Code
Replies are listed 'Best First'.
Re: recursive s///g core dumps
by boo_radley (Parson) on May 17, 2002 at 04:18 UTC
    I think perl's clobbering or removing backreferences somewhere, and you're trying to access \1 after perl's done something else with it.
    If $1 is assigned to a temp var within dofunc, the code no longer dumps core. Below is my test, and output...

    #!/usr/bin/perl -w use strict; my $weeping; print doFunc( "AABBCC",0 ); print doFunc( "AAAABBBBCCCC",0 ); sub doFunc { my( $theParam,$depth) = @_; print STDERR "\ndofunc($theParam)\n"; my $re; $re = ${['(.+)$weeping']}[$depth]||'(.+)($weeping)'; print STDERR join"\n", $theParam =~ /$re/g,undef; $theParam =~ s/$re/&doFunc($weeping,$depth+1)/ge; $weeping=$1; ++$theParam; print STDERR "=$theParam\n"; return $theParam; }


    and this gives us

    dofunc(AABBCC) =AABBCD AABBCD dofunc(AAAABBBBCCCC) =AAAABBBBCCCD AAAABBBBCCCD


    Now, if you'll excuse me, I need to go wash my hands compulsively :-)
    update so what output do you expect, out of curiosity?
Re: recursive s///g core dumps
by educated_foo (Vicar) on May 17, 2002 at 03:40 UTC
    Works for 5.6.1 on linux-ppc. At least, it doesn't segfault. For those of us following along at home, what in the world is this:
    $re = ${['(.+)\1']}[$depth]||'(.+)(\1)';
    supposed to do? It looks like you're indexing into an array consisting of the single element '(.+)\1', which will be undef for any depth > 0, meaning $re will be '(.+)(\1)'. But I'm probably missing something.

    /s
    Update: Since my output doesn't even pretend to match boo_radley's, maybe I should go ahead and post it:

    dofunc(AABBCC)
    A
    B
    C
    dofunc(A)
    =B
    dofunc(B)
    =C
    dofunc(C)
    =D
    =BCE
    dofunc(AAAABBBBCCCC)
    AA
    BB
    CC
    dofunc(AA)
    A
    A
    dofunc(A)
    =B
    =C
    dofunc(BB)
    B
    B
    dofunc(B)
    =C
    =D
    dofunc(CC)
    C
    C
    dofunc(C)
    =D
    =E
    =CDF
    
      ${['(.+)\1']}[$depth]||'(.+)(\1)'; just changes $re within the recursive call, which seems to be necessary to cause the core dump

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://167187]
Approved by boo_radley
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2015-07-28 03:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (252 votes), past polls