Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Why am I get a core dump when I use ??{}

by Plankton (Vicar)
on Apr 09, 2004 at 20:55 UTC ( [id://344027]=perlquestion: print w/replies, xml ) Need Help??

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

Friends,

I was trying to come with a regex to help another monk when I ended up having core dumps. Can some monk look at my code below and tell me where I went wrong?
$ cat dumps_core.pl #!/usr/bin/perl -w use strict; my $countSpaces = sub { my ($c) = ($1); my @spaces = ($c =~ /( )/g); return qr{(?!)} if $#spaces > 0; return qr{(?=)}; }; while (<DATA>) { #my @spaces = (/( )/g); print "$_ ", /(.*)(??{$countSpaces->()})/ ? "has 1 or no space +s\n" : "has more than one space\n"; } __DATA__ this line has more than one space that_is_5_spaces This isonespaceonly therearenospaceshere $ ./dumps_core.pl Segmentation fault (core dumped)

Plankton: 1% Evil, 99% Hot Gas.

Replies are listed 'Best First'.
Re: Why am I get a core dump when I use ??{}
by Zaxo (Archbishop) on Apr 09, 2004 at 21:09 UTC

    My first guess is abuse of $1.

    Why not just call tr/// to count characters?

    while (<DATA>) { print qq("$_" ), tr/ // <= 1 ? "has 1 or no spaces\n" : "has more than one space\n"; }

    After Compline,
    Zaxo

Re: Why am I get a core dump when I use ??{}
by ambrus (Abbot) on Apr 09, 2004 at 21:12 UTC

    You call the regexp engine twice inside each other.

    Older perls have problems if you use regexps from inside (?{ }), it seems that the regexp engine is not fully reentrant. If this is not an 5.8.x perl, try upgrading it.

      This core dumps on 5.8.3 - I dunno if upgrading can be classed as a solution :-)

      Update: Oh, and perlre (on 5.8.3) says:

      "(?{ code })"
      WARNING: This extended regular expression feature is considered highly experimental, and may be changed or deleted without notice.
      I think exploding satisfies the whole "highly experimental" bit.

      There is however a simple way of making the code work: you have too many question marks. Try:

      print "$_ ", /(.*)(?{$countSpaces->()})/ ? "has 1 or no spaces\n" : "h +as more than one space\n";
      ...in the right place. The output is:
      this line has more than one space has more than one space that_is_5_spaces has more than one space This isonespaceonly has more than one space therearenospaceshere has 1 or no spaces
      ...which looks like (almost) the right output to me.

      Update 2 Erm, ah. Doh. (??{ code }) is perfectly valid according to perlre too - put equally experiment. Nevermind. Using highly experimental features is probably a bad idea.


      If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
      That way everyone learns.

      It still dumps core on bleadperl.
Re: Why am I get a core dump when I use ??{}
by gmpassos (Priest) on Apr 09, 2004 at 22:48 UTC
    You can't use REGEXP in a CODE call from a main REGEXP, soo, your sub can't use REGEXP on it's code or it will mess with the main REGEXP, specially if the main REGEXP is from a while() loop.

    But you can replace the use of regexp for other code.

    Graciliano M. P.
    "Creativity is the expression of the liberty".

      Thanks gmpassos that was it! I changed my code to this ...
      ... use strict; my $countSpaces = sub { my $spaces = tr/ //; return qr{(?!)} if $spaces > 1; return qr{(?=)}; }; while (<DATA>) { chomp; print "$_ [", /(.*)(??{$countSpaces->()})/ ? "OK]\n" : "BAD]\n +"; } __DATA__ this line has more than one space that_is_5_spaces This isonespaceonly therearenospaceshere

      Plankton: 1% Evil, 99% Hot Gas.
Re: Why am I get a core dump when I use ??{}
by Anonymous Monk on Apr 09, 2004 at 21:09 UTC

    I probably would have used something simpler such as:

    while (<DATA>) { chomp; print "'$_' contains ", (@{[/ /g]}) > 1 ? "more than one space.\n" : "either one space or none.\n"; } __DATA__ foo bar baz foobar foobar baz foo barbaz

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 21:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found