The module has changed slightly. This new form appears to be more solid in terms of definition and behavior.

NAME

re::ampersand - Perl pragma to alter $& support in regular expressions


SYNOPSIS

    # see examples


DESCRIPTION

When Perl sees you using $`, $&, or $', it has to prepare these variable after every successful pattern match. This can slow a program down because these variables are "prepared" by copying the string you matched against to an internal location. This copying is also how $DIGIT variables are made accessible, but that only occurs on a per-regex basis: if a regex has capturing parentheses, the string will be copied, otherwise it will not be.

This pragma allow you to turn on and off the support for $& and friends in blocks of your code.


USAGE

Not using this pragma

Your program will run the same way it did before if you do not use this pragma. Default behavior has not been changed.

Enforcing $& support

You can enforce support for $& and friends with use re::ampersand 'capture', which makes all regexes set $& if they are successful. You can leave off the 'capture' argument.

Turning off $& support

You can turn off support for $& with use re::ampersand 'no_capture', which stops all regexes (that don't have capturing parentheses) from setting $&. Any regex with capturing parentheses will always have support for $& because of the mechanism that provides $DIGIT variables.


EXAMPLES

Support for $& in a block of a program

  #!/usr/bin/perl -w
  "Perl" =~ /Pe/;
  {
    use re::ampersand;
    "Perl" =~ /rl/ and print "<$&>\n";  # <rl>
  }
  # $& has not been seen yet!
  "Perl" =~ /rl/ and print "<$&>\n";  # <>

Turning off support for $& in a block

  #!/usr/bin/perl -w
  # regexes set $&
  "Perl" =~ /(?<=.)./ and print "<$&>\n";  # <e>
  {
    use re::ampersand 'no_capture';
    # matching on a string you'd rather not have copied!
    $huge_string =~ /a+bc+/ and print "<$&>\n";  # <>
  }
  # regexes set $&
  "Perl" =~ /.(?!..)/ and print "<$&>\n";  # <r>


CAVEAT

Do not try no re::ampersand unless you are aware of the consequences! If you do this, any $& found in the no re::ampersand block of code will cause the ``seen $&'' flag to go on for the normal program. That means the following code produces the output denoted by the comments:

  "abc" =~ /a/ and eval q{ print "<$&>" };    # <a>
  {
    no re::ampersand;
    "abc" =~ /b/ and print "<$&>";            # <b>
  }

When your program is being compiled, the $& in the no re::ampersand block sets the ``seen $&'' flag in your program, which means that when the "abc" =~ /a/ regex is executed, it will set $&.

Efforts are currently underway to fix this behavior.


AUTHOR

Jeff japhy Pinyan, japhy@pobox.com.

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;


In reply to New $& Approach, thanks to Hugo, Jarkko, Tye, and others by japhy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.