Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Functions in substitutions (s///) and Perl 5.8.

by v_thunder (Scribe)
on Oct 25, 2002 at 17:13 UTC ( #208085=note: print w/replies, xml ) Need Help??

in reply to Functions in substitutions (s///) and Perl 5.8.

Hello again,

After some testing, I am even more baffled than before. After attempting to create some small test cases, I'm sure there is a perl bug lurking here. Here are our (teichman's and my) findings:

This code will break:

#!/usr/bin/perl -w $data = { key1 => 'value1', key2 => 'value2', }; sub macro_replace { $_ = shift; my $helper = sub { $_ = shift; return $data->{$_} if $data->{$_}; return undef; }; chomp (my $pwd = `pwd`); s/\[\[(pwd)\]\]/$pwd/g; s/\[\[(\w+)\]\]/@{[$helper->($1)]}/g; return $_; } print macro_replace ("blah [[key1]] [[key2]] [[pwd]] blah blah\n");

Note the use of $_ in the helper function. Changing this to use another variable makes it all work as expected, as teichman said.

Strangely, however, taking out the pwd substitution (i.e., commenting out lines 15 and 16) seems to stop it from crashing as well, but only in this test case. I haven't been able to reproduce the exact behavior we see in our program yet.

This is being tested on Red Hat Linux 8.0 and SuSE Linux 8.1, both of which come with perl 5.8.0 (+/- a few patches of their own--which I believe are all unrelated to this).

Thanks for the useful tips!


Replies are listed 'Best First'.
Re: Re: Functions in substitutions (s///) and Perl 5.8.
by jmarans (Novice) on Nov 01, 2002 at 17:43 UTC
    I've a similar problem on RH8. I'm trying to port a a running application, and the xml parser is returning the same panic error when my handler returns. I tried unsuccessfully to use the variable substitution you mentioned, so I was wondering if you're any futher along.
    s{ <\?nm( # opening angle bracket (?: # Non-backreffing grouping paren [^>'"] * # 0 or more things that are neither > nor ' nor " | # or else ".*?" | # a section between double quotes (stingy match) | # or else '.*?' | # a section between single quotes (stingy match) ) + # repetire ad libitum # hm.... are null tags <> legal? XXX )\?> # closing angle bracket }{my $var = $1; my_handler($r, $var)}geisx; # mutate

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://208085]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2017-09-24 17:51 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (274 votes). Check out past polls.