in reply to Weird behaviour with match-time code evaluation and backtracking

Use our instead of my and your code will work as you expect.

C:\test\DBMNested>p1 [0] Perl> sub getChecksum($) { my $in = shift; our $check = 0; $in =~ m/(?:(.)(?{$check += ord($1);}))*^/; return $check; };; print getChecksum( 'foo' );; 324 print getChecksum( 'foo' );; 324 print getChecksum( 'foobar' );; 633

Though its a very slow way to achieve your goal. Corion's unpack tip is the way to go.


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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."
  • Comment on Re: Weird behaviour with match-time code evaluation and backtracking
  • Download Code

Replies are listed 'Best First'.
Re^2: Weird behaviour with match-time code evaluation and backtracking
by ikegami (Patriarch) on Mar 05, 2008 at 09:42 UTC

    That should be local our $check;. Let's not clobber our caller's variables.

    sub getChecksum($) { my $in = shift; local our $check = 0; $in =~ m/(?:(.)(?{$check += ord($1);}))*^/; return $check;