in reply to Bitten by the lazy execution bug?

Wtf? If I comment out the print, it once again errors right off the bat saying the third column is null, and if I uncomment it again, it runs fine?

the $1..$3 aren't localized on entering a sub, therefore any sub that uses a regular expression somewhere *might* invalidate the references to the special regex capture variables, like:

... $_ = 'the small print'; if( /^\s*(\S+)\s+(\S+)\s+(.+\S)\s*$/ ) { sub1( 'replace into reference_code values (?,?,?)', $1, $2, $3 ) } sub sub1 { my $p0 = shift; # try to comment this line out and get your lesson print "looks like a statement somewhere\n" if $p0 =~ /\w+/; my ($p1, $p2, $p3) = @_; print "statement parameters: $p1, $p2, $p3\n"; } ...

Are you sure your program really did run without errors - only by inserting the "print" line?

BTW, interpolation of $1..$3 in the sub call (as has been suggested) also does the trick (by instantiating anonymous copies of $1..$3):

... sub1( 'replace into reference_code values (?,?,?)', "$1", "$2", "$3" +) ...

Regards

mwa

Replies are listed 'Best First'.
Re^2: Bitten by the lazy execution bug?
by mccready (Initiate) on Dec 08, 2007 at 20:38 UTC
    That is bizarre. I would have thought the values would have been passed in "by value".

      To quote from perldoc perlsub:

      The array @_ is a local array, but its elements are aliases for the actual scalar parameters. In particular, if an element $_[0] is updated, the corresponding argument is updated (or an error occurs if it is not updatable). If an argument is an array or hash element which did not exist when the function was called, that element is created only when (and if) it is modified or a reference to it is taken.

      Thus the behavior of this code:

      #!/usr/bin/perl -l my @a = 1..3; sub inc { $_++ for @_; } inc(@a); print for @a; inc('hello'); __DATA__ 2 3 4 Modification of a read-only value attempted at - line 6.