<?xml version="1.0" encoding="windows-1252"?>
<node id="62493" title="Re: Re (tilly) 1: Perl is psychic?!" created="2001-03-06 14:32:10" updated="2005-07-27 11:01:57">
<type id="11">
note</type>
<author id="59723">
pileswasp</author>
<data>
<field name="doctext">
Woo.  This one's got me interested.&lt;BR&gt;&lt;BR&gt;
I've tested this on perl 5.004_04 for sun-solaris, perls 5.004_05 and 5.6 for i686-linux (redhat) and even ActiveState's 5.6.0 for Win32 and _all_ of them show the same behaviour.&lt;BR&gt;
What causes the difference between two variations on this bit of code is whether or not the pattern is plain text (as it says above &lt;TT&gt;/blah/ may be optimized to an analogue of index()&lt;/TT&gt;).  If there's no regex compilation then $&amp; causes Segmentation faults.&lt;BR&gt;&lt;BR&gt;

Using&lt;BR&gt;&lt;CODE&gt;use re 'debug';&lt;/CODE&gt;&lt;BR&gt;shows that the regex isn't re-evaluated when the $&amp; is entered on STDIN, but it does state explicitly &lt;TT&gt;Omitting $` $&amp; $' support.&lt;/TT&gt;  Must say I'm at a bit of a loss as to where the value does come from.&lt;BR&gt;&lt;BR&gt;

If I were to go out on a limb a bit I would say that I'm thinking that maybe the penalty from using $&amp;, etc in your
code is because perl links it into plain text matches as well as compiled regexes.  ie  $&amp;, etc are &lt;EM&gt;always&lt;/EM&gt;
there for full compiled regex's, but &lt;CODE&gt;index()&lt;/CODE&gt; doesn't normally return the pre-match, match and post-match strings, so the "analogue of index()" requires a bit more work to produce them.&lt;BR&gt;&lt;BR&gt;

Where's japhy?  I get the feeling he'll know   :o)&lt;BR&gt;&lt;BR&gt;

There's a bunch of tests and &lt;TT&gt;re 'debug'&lt;/TT&gt; output below if you're interested:
&lt;CODE&gt;&lt;READMORE&gt;&lt;/CODE&gt;
&lt;CODE&gt;
use re 'debug';
'foo' =~ m/.*/;
print eval &lt;STDIN&gt;;
&lt;/CODE&gt;

This gives the following output:&lt;BR&gt;
&lt;PRE&gt;Compiling REx `.*'
size 3 first at 2
   1: STAR(3)
   2:   REG_ANY(0)
   3: END(0)
anchored(MBOL) implicit minlen 0
Omitting $` $&amp; $' support.

EXECUTING...

Matching REx `.*' against `foo'
  Setting an EVAL scope, savestack=3
   0 &amp;lt;&amp;gt; &amp;lt;foo&amp;gt;             |  1:  STAR
                           REG_ANY can match 3 times out of 32767...
  Setting an EVAL scope, savestack=3
   3 &amp;lt;foo&amp;gt; &amp;lt;&amp;gt;             |  3:    END
Match successful!
&lt;/PRE&gt;

Before waiting for the input.  It actually specifies that it's omitting $&amp;, etc support, yet when you do enter &lt;STRONG&gt;$&amp;&lt;/STRONG&gt; still gives the expected answer:

&lt;PRE&gt;Freeing REx: `.*'
foo
&lt;/PRE&gt;

If you use a plain text match (like tilly suggested with /ri/ in 'string', you don't get this result at all, as perl
 doesn't handle the match in the same way, it "guesses" the result, presumably using a more index() like way of making the match:&lt;BR&gt;

&lt;CODE&gt;use re 'debug';
'foo' =~ m/o/;
print eval &lt;STDIN&gt;;
&lt;/CODE&gt;

gives the output:

&lt;PRE&gt;$ &lt;STRONG&gt;perl reg&lt;/STRONG&gt;
Compiling REx `o'
size 3 first at 1
rarest char o at 0
   1: EXACT &amp;lt;o&amp;gt;(3)
   3: END(0) 
anchored `o' at 0 (checking anchored isall) minlen 1
Omitting $` $&amp; $' support.

EXECUTING...

Guessing start of match, REx `o' against `foo'...
Found anchored substr `o' at offset 1...
Guessed: match at offset 1
&lt;STRONG&gt;$&amp;&lt;/STRONG&gt;
Segmentation fault (core dumped)
&lt;/PRE&gt;

&lt;STRONG&gt;$`&lt;/STRONG&gt; and &lt;STRONG&gt;$'&lt;/STRONG&gt; don't have quite such drastic efects, they simply print blank.&lt;BR&gt;

The extra level of compilation that look(ahead|behind)s give the regex also
allow $&amp; to produce the required result:&lt;BR&gt;

&lt;CODE&gt;use re 'debug';
'foo' =~ m/(?&lt;=f)o(?=o)/;
print eval &lt;STDIN&gt;;
&lt;/CODE&gt;

Giving:

&lt;PRE&gt;$ &lt;STRONG&gt;perl reg&lt;/STRONG&gt;
Compiling REx `(?&amp;lt;=f)o(?=o)'
size 15 first at 1
rarest char o at 0
   1: IFMATCH&amp;#091;-1&amp;#093;(7)
   3:   EXACT &amp;lt;f&amp;gt;(5)
   5:   SUCCEED(0)
   6:   TAIL(7)
   7: EXACT &amp;lt;o&amp;gt;(9)
   9: IFMATCH&amp;#091;-0&amp;#093;(15)
  11:   EXACT &amp;lt;o&amp;gt;(13)
  13:   SUCCEED(0)
  14:   TAIL(15)
  15: END(0)
anchored `o' at 0 (checking anchored) minlen 1
Omitting $` $&amp; $' support.

EXECUTING...

Guessing start of match, REx `(?&amp;lt;=f)o(?=o)' against `foo'...
Found anchored substr `o' at offset 1...
Guessed: match at offset 1
Matching REx `(?&amp;lt;=f)o(?=o)' against `oo'
  Setting an EVAL scope, savestack=3
   1 &amp;lt;f&amp;gt; &amp;lt;oo&amp;gt;             |  1:  IFMATCH&amp;#091;-1&amp;#093;
   0 &amp;lt;&amp;gt; &amp;lt;foo&amp;gt;             |  3:    EXACT &amp;lt;f&amp;gt;
   1 &amp;lt;f&amp;gt; &amp;lt;oo&amp;gt;             |  5:    SUCCEED
                              could match...
   1 &amp;lt;f&amp;gt; &amp;lt;oo&amp;gt;             |  7:  EXACT &amp;lt;o&amp;gt;
   2 &amp;lt;fo&amp;gt; &amp;lt;o&amp;gt;             |  9:  IFMATCH&amp;#091;-0&amp;#093;
   2 &amp;lt;fo&amp;gt; &amp;lt;o&amp;gt;             | 11:    EXACT &amp;lt;o&amp;gt;
   3 &amp;lt;foo&amp;gt; &amp;lt;&amp;gt;             | 13:    SUCCEED
                              could match...
   2 &amp;lt;fo&amp;gt; &amp;lt;o&amp;gt;             | 15:  END
Match successful!
&lt;STRONG&gt;$&amp;&lt;/STRONG&gt;
Freeing REx: `(?&amp;lt;=f)o(?=o)'
o
&lt;/PRE&gt;



</field>
<field name="root_node">
62398</field>
<field name="parent_node">
62419</field>
</data>
</node>
