in reply to Re^4: Multiple uses of (?{ code }) do not appear to be called
in thread Multiple uses of (?{ code }) do not appear to be called

Your code will break if execution ever proceeds past the my(@o) line. That is, if you put a foo() call at the end of your script. It is always bad style to use my() variables with (?{...}) and (??{...}). Don't do it.

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

  • Comment on Re^5: Multiple uses of (?{ code }) do not appear to be called

Replies are listed 'Best First'.
Re^6: Multiple uses of (?{ code }) do not appear to be called
by rhesa (Vicar) on Dec 29, 2006 at 20:08 UTC
    I'll gladly accept your advice about it being bad style, but I don't understand why you say my code would break. Given that the following works, I must be overlooking something:
    #!/usr/bin/perl -l use strict; bar(); bar(); { # limit scope my @o; sub bar { my $window = "a b X20 c X5 d e X17 X12"; @o = (); my @m = ( $window =~ m/(X\d+(?{push @o, pos()}))/g ); print "Matches: @m"; print "Offsets: @o"; print " "; } } print "One last time"; bar(); __END__ Matches: X20 X5 X17 X12 Offsets: 7 12 20 24 Matches: X20 X5 X17 X12 Offsets: 7 12 20 24 One last time Matches: X20 X5 X17 X12 Offsets: 7 12 20 24
    Even if I put the bar() call inside the bare block, right after the sub definition, it still works. I'd appreciate it if you could enlighten me.