http://qs321.pair.com?node_id=957214


in reply to Memory Leaks and %+

Dang. I was hoping it was something wrong with my code, and therefore fixable. Unfortunately, for the intent & purpose of my particular script I'm stuck with 5.10. Network distribution and company policy and all that.

I will try it out on a newer version when I get the chance and report back, though.

In the meantime, I've found a quick n'dirty fix in case anyone else encounters this archaic headache. Just switch to @+ and @- instead of the more convenient %+:

#!/usr/bin/perl use strict; use warnings; open(my $fh, '<', 'EXAMPLE.TXT'); my $regexp = qr/(?<value1>\d+)\s+(?<value2>\d+)/; while(<$fh>) { next unless /$regexp/; my $value1 = substr($_, $-[1], $+[1] - $-[1]); my $value2 = substr($_, $-[2], $+[2] - $-[2]); print "GOT $value1 $value2\n"; }

And thanks for the responses!

-Maph

Replies are listed 'Best First'.
Re^2: Memory Leaks and %+
by ruzam (Curate) on Mar 01, 2012 at 14:07 UTC

    Isn't it easier to just switch over to $1 and $2 at this point? Your new substr workaround isn't very readable (to me anyway), so it appears that you've lost the one thing you were hoping to gain by using %+.

    my $regexp = qr/(\d+)\s+(\d+)/; while(<$fh>) { next unless /$regexp/; my $value1 = $1; my $value2 = $2; print "GOT $value1 $value2\n"; }

      I completely agree yours is more readable that the OPs alternate version. But even more readable is:

      my $regexp = qr/(\d+)\s+(\d+)/; while(<$fh>) { my( $value1, $value2 ) = /$regexp/ or next; print "GOT $value1 $value2\n"; }

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?