JStrom has asked for the wisdom of the Perl Monks concerning the following question:

A strange problem here. Perl is telling me
"Use of uninitialized value $_[2] in pattern match (m//) at Plugin/Syn +tax.pm line 39."
But there is no reference to $_[2] in the code. I stripped out most of the module to hit the function: (Line 39 is the first line of parse_rvalue())
package Plugin::Syntax; use strict; use warnings; use Data::Dump; my $code; my $kw = qr/[A-Za-z_][A-Za-z0-9_-]*/; my %directives = map { $_ => 1 } (qw| !some !data !snipped| ); my %commands = map { $_ => 1 } (qw| some data snipped| ); sub parse_command { return unless $_[0] =~ /\G\s*($kw)\s*=\s*/cg; # not 'ident =' my $id = $1; my ($s,$e) = ($-[1],$+[1]); if( $id =~ /^(?:ok|e[0-9A-Fa-f]{2})$/ ) { $code->tagAdd( 'special', "$_[1] +${s}c", "$_[1] +${e}c" ); } if( $_[0] =~ /\G($kw)\s*\(/cg ) { # parse that command pos( $_[0] )--; # give back the paren if( exists $commands{lc $1} ) { # Check if command exi +sts $code->tagAdd( 'command', "$_[1] +$-[1]c", "$_[1] +$+[1]c" + ); } return lc $1; } # This must be an immediate value &parse_rvalue; return 'none'; } sub parse_rvalue { if( $_[0] =~ /\G[CS]?\d\d-\d\d(?:\s+var\s+[0-9A-Fa-f]{1,2})?/cg ) +{ $code->tagAdd('constant', "$_[1] +$-[0]c", "$_[1] +$+[0]c"); } elsif( $_[0] =~ /\G[CS]?[#][0-9A-Fa-f]{3}/cg ) { $code->tagAdd('constant', "$_[1] +$-[0]c", "$_[1] +$+[0]c"); } elsif( $_[0] =~ /\G\$?[0-9A-Fa-f][0-9A-Fa-f ]*(?!\w)/cg ) { $code->tagAdd('constant', "$_[1] +$-[0]c", "$_[1] +$+[0]c"); } elsif( $_[0] =~ /\G\$?$kw/cg ) { # indentifier, no highlight } elsif( $_[0] =~ /\G".*?"/cg ) { $code->tagAdd('constant', "$_[1] +$-[0]c +1c", "$_[1] +$+[0]c +-1c"); } elsif( $_[0] =~ /\G\@[^\s,)]*/cg ) { $code->tagAdd('constant', "$_[1] +$-[0]c +1c", "$_[1] +$+[0]c" +); } elsif( $_[2] =~ /^(?: hsm | cat | cat8 )$/x and $_[0] =~ /\G[<>;]/ +cg ) { } else { return; } return 1; } 1;
And the test script:
use strict; use warnings; use Plugin::Syntax; Plugin::Syntax::parse_command( "xx = ", "" );

Replies are listed 'Best First'.
Re: Weird undefined variable warning
by brian_d_foy (Abbot) on Jan 18, 2008 at 23:37 UTC

    You use $_[2] in parse_rvalue in the last elsif. However, you only ever pass it two arguments, so $_[2] will never have anything in it.

    The argument list for parse_rvalue is the same as that for parse_command since you call it as &parse_command. In the last line of the sample script, you only pass two arguments to parse_command, so that's all that will show up in parse_rvalue.

    How you fix that depends on what everything is supposed to be doing. :)

    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review
Re: Weird undefined variable warning
by ysth (Canon) on Jan 18, 2008 at 23:27 UTC
Re: Weird undefined variable warning
by hipowls (Curate) on Jan 18, 2008 at 23:12 UTC

    It's because pos is pointing to the end of the string to be matched and you are asking the match to start at pos (the /c qualifier). I have no idea why the error refers to $_[2], perhaps you'd like to file a bug report?

      I have no idea why the error refers to $_[2], perhaps you'd like to file a bug report
      It is a bug, but not relating to reporting an index of 2 rather 0. The undef error is actually occuring at the last branch of the if/else tree (hence the 2, which features on that line - probably a mistake in the OP's code), but the wrong line number is being reported (a well-known and longstanding class of bug, will probably be fixed for 5.12.)

      Dave.

        Do'h, didn't spot that last elsif;( I'd got the error to disappear by resetting pos to 0 and it went away. We live and learn;)