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

I have a problem when using Parse::Lex. I get the error message: "Can't call method "name" on an undefined value at lextTest.pl line 17." when the code is executed. Does anyone know what is causing it?
use Parse::Lex; my $lexer = Parse::Lex->new (qw( SC ESC7 BOLD \[0;1m )); my $buffer = 'ESC7ESC[002;002HESC[0;1m . ESC[0;1mESC8ESC7ESC[002;01 +0HESC[0;1m . ESC[0;1mESC8ESC7ESC[003;002HESC[0;1;5m ESC[0;1mE +SC8'; $lexer->from($buffer); $lexer->every (sub { print $_[0]->name, "\t"; print $_[0]->text, "\n"; });

Replies are listed 'Best First'.
Re: Parse::Lex error message
by gothic_mallard (Pilgrim) on Jan 27, 2004 at 15:50 UTC

    I assume your problem is occuring at:

    $lexer->every (sub { print $_[0]->name, "\t"; print $_[0]->text, "\n"; });

    I'm not overly familiar with Parse::Lex - what's getting passed (or should be getting passed) to sub{} in $lexer->every()?

      Parse::Lex passes a Parse::Token object to the sub. You can print the name of the token that has been matched using the "name" method. However, the method can not be executed since a token object is not passed for some some reason. On shorter strings the lexer works, but with the long string in the example it fails.
Re: Parse::Lex error message
by Theo (Priest) on Jan 27, 2004 at 15:33 UTC
    Which is line 17, please?

    -Theo-
    (so many nodes and so little time ... )

      line 17 is print $_[0]->name, "\t"; $_[0] is a Parse::Token object.
        $_[0] is a Parse::Token object.
        The error message tells you that $_[0] is undefined, so it can't be a Parse::Token object.