Can't use Perl RE (s///) within (?{})

Discussion in 'Perl Misc' started by Clint O, Apr 26, 2009.

  1. Clint O

    Clint O Guest

    Hi:

    Unfortunately, the program I have is way too complex to post here, but
    I wrote a lexical analyzer in Perl which makes heavy use of these code
    blocks because it more closely approximates the behavior of lex/flex
    than anything else I've found. I've tried both 5.8.7 and 5.10.0 with
    varying results, and I've noticed that when I attempt to call the
    regular expression engine inside I get two possible outcomes:

    1) Out of memory
    2) Segmentation fault

    my $macro_text = qr/^(.*?[^\\]\n)
    (?{
    $text = $^N;

    #$text_lines = $text =~ s|\\\n|\n|
    gs;
    @text_lines = split('\n', $text);

    @r = ('MACRO_TEXT', $text, scalar
    (@text_lines), 0);
    })
    /sx;

    The following attempts to snarf arbitrary replacement text in a
    textual macro up to a newline not preceded by a backslash. One of the
    things I need to do is a few transformations according the user guide
    like strip the backslashes out of the macro text. There are a few
    others.

    I've also noticed that declaring 'my' variables local to these blocks
    can cause Perl segfaults etc. as well. I do realize these code blocks
    are experimental, but this simplifies my life quite a bit by allowing
    the inline code to make pattern specific calculations before the match
    is complete.

    Any suggestions you could offer would be much appreciated.

    Thanks,

    -Clint
    Clint O, Apr 26, 2009
    #1
    1. Advertising

  2. Clint O

    Guest

    On Sun, 26 Apr 2009 14:11:54 -0700 (PDT), Clint O <> wrote:

    >Hi:
    >
    >Unfortunately, the program I have is way too complex to post here, but
    >I wrote a lexical analyzer in Perl which makes heavy use of these code
    >blocks because it more closely approximates the behavior of lex/flex
    >than anything else I've found. I've tried both 5.8.7 and 5.10.0 with
    >varying results, and I've noticed that when I attempt to call the
    >regular expression engine inside I get two possible outcomes:
    >
    >1) Out of memory
    >2) Segmentation fault
    >
    >my $macro_text = qr/^(.*?[^\\]\n)
    > (?{
    > $text = $^N;
    >
    > #$text_lines = $text =~ s|\\\n|\n|
    >gs;
    > @text_lines = split('\n', $text);
    >
    > @r = ('MACRO_TEXT', $text, scalar
    >(@text_lines), 0);
    > })
    > /sx;
    >
    >The following attempts to snarf arbitrary replacement text in a
    >textual macro up to a newline not preceded by a backslash. One of the
    >things I need to do is a few transformations according the user guide
    >like strip the backslashes out of the macro text. There are a few
    >others.
    >
    >I've also noticed that declaring 'my' variables local to these blocks
    >can cause Perl segfaults etc. as well. I do realize these code blocks
    >are experimental, but this simplifies my life quite a bit by allowing
    >the inline code to make pattern specific calculations before the match
    >is complete.
    >
    >Any suggestions you could offer would be much appreciated.
    >
    >Thanks,
    >
    >-Clint


    I don't think a regex in the code block is going to fly
    even if doing the regex from a called sub. I think its more a problem
    on the substitution side of s/// that is the problem. Some (re-eval 1) error.

    sub strip_stuff {
    my $text = shift;
    $text =~ s/\\\n/\n/g;
    return $text;
    }

    qr /...
    (? {
    $text = $^N;
    #$text_lines = $text =~ s|\\\n|\n|gs;
    $text = strip_stuff ($text);
    ...
    })
    /xs



    Within the code block things are very quirky and limited.
    For all but the simplest things, this is still part of the regular expression.
    So inside a regular expression it would seem doubtfull you can have another regular
    expression. Even the delemiters have to be different just to parse. Have you tried $text /= 5; ?

    In my test, in the code block, my() crashed and any regular expression crashes thats for sure.
    , Apr 27, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. suraj

    embedding perl within perl....

    suraj, Nov 6, 2003, in forum: Perl Misc
    Replies:
    4
    Views:
    113
    Al Tobey
    Nov 7, 2003
  2. Replies:
    14
    Views:
    285
    Tad McClellan
    Apr 26, 2006
  3. robertospara
    Replies:
    14
    Views:
    272
    robertospara
    Dec 10, 2006
  4. PerlFAQ Server
    Replies:
    0
    Views:
    88
    PerlFAQ Server
    Feb 9, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    162
    PerlFAQ Server
    Apr 24, 2011
Loading...

Share This Page