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

Hi i've a small problem about this instruction:

 push @tbl, [ @$_ ] for $table->rows

when i launch the my program, occasionally i receive this message:

Can't call method "rows" on an undefined value at download_and_parse_v3.pl line 81.

So, because i want the program goes on even on this error, i tried this in replace of the above instruction:

 push @tbl, [ @$_ ] for $table->rows or warn " ERRORE $hname $chin ";

where $hname and $chin are variables in the program that can help me understood the reason of the error.
At this point i thought that i had the above phrase instead the "Can't call method rows..."
but i alwais receive the same message. Where is my mistake? Thanks

Replies are listed 'Best First'.
Re: Handling errors on perl
by Corion (Patriarch) on Aug 19, 2018 at 12:45 UTC

    The error is upwards from the code you showed. $table did not receive a value.

    Maybe you have something like this in your code:

    $table = Spreadsheet::ParseExcel->parse( $filename );

    ... you should check there that you actually get a value back:

    my $parser = Spreadsheet::ParseExcel->new(); my $table = $parser->parse('Book1.xls'); if ( !defined $table ) { die $parser->error(), ".\n"; }

    But this is just a wild guess as you haven't shown how you intend to initialize $table.

Re: Handling errors on perl
by LanX (Saint) on Aug 19, 2018 at 12:41 UTC
    use a block eval:

    eval { push @tbl, [ @$_ ] for $table->rows } or warn " ERRORE $hname $chin ";

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Hi
      thank you for your answer.

      i was able to do the following, and worked very well:

      eval { push @tbl, [ @$_ ] for $table->rows };<br> if ($@) {<br> my $error = $@; <br> warn " Custom error type 1 = no problem ";<br> }


      this piece of code is inside a perl that is called from another perl so i use "my $error" cause the problem of global $@ and worked as i wanted.
      Now the small problem left is the following, the message i receive when the error raise is the following:

      Custom error type 1 = no problem  at perl_parse_v3.pl line 88.

      i simply would like to avoid "at perl_parse_v3.pl line 88." because i not necessary cause i perfecly know that only one cause can raise the error.
      how can that be done?
        Put a newline \n at the end of your warn text.

        It's described in the second phrase of the documentation.

        "If the last element of LIST does not end in a newline, it appends the same file/line number text as die does."

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: Handling errors on perl
by BillKSmith (Monsignor) on Aug 19, 2018 at 22:34 UTC
    You should consider using the module Try::Tiny rather than 'eval', especially if you are familiar with its concept from another language.
    Bill