my $word = substr $str, $-[0] + 10, $+[0] - $-[0] + 10;

There seems to be a typo there:

my $str = 'Hulk hate classless system with means of manufacturing give +n to working class! Hulk crush puny Marxists!'; if ($str =~ /Hulk hate \w+/) { print "\$-[0] <$-[0]> \$+[0] <$+[0]>\n"; my $word = substr $str, $-[0] + 10, $+[0] - $-[0] + 10; print "word <$word>\n"; } __END__ $-[0] <0> $+[0] <19> word <classless system with means o>

That +10 should be -10.

Anyway, the reason I'm replying is not this, but to suggest some typeglob aliasing to make the code a tiny bit easier to read:

*starts = \@-; *ends = \@+; our (@starts, @ends); my $str = 'Hulk hate classless system with means of manufacturing give +n to working class! Hulk crush puny Marxists!'; if ($str =~ /with means of \w+/) { print "\$-[0] <$-[0]> \$+[0] <$+[0]>\n"; my $word = substr $str, $starts[0] + 14, $ends[0] - $starts[0] - 1 +4; print "word <$word>\n"; } __END__ $-[0] <27> $+[0] <54> word <manufacturing>

--
David Serrano


In reply to Re: An optimization of last resort: eliminate capturing from your regexps by Hue-Bond
in thread An optimization of last resort: eliminate capturing from your regexps by diotalevi

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.