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

Hi,

I have a script that reads from an excel workbook. I have pointed it to a worksheet, which I guess will be present in the excel workbook.
Next I try to access the value of a particular cell. If that worksheet was not present, the call which I make to get the cell value, my script exits throwing the below error.

Can't call method "Cells" on an undefined value at Perl_XYZ.pl line XX.

My question here is that: where is that error message stored before it prints it out to the screen ? Is it in some variable which I can manually use and print the contents ?

btw, I am using ActivePerl 5.10 on windows xp.

Thanks,
J

Replies are listed 'Best First'.
Re: to get the un-forseen errors
by moritz (Cardinal) on Feb 17, 2010 at 11:56 UTC
    You can catch such errors by putting the failing block in a BLOCK eval. The error message is then stored in $@.
    Perl 6 - links to (nearly) everything that is Perl 6.
Re: to get the un-forseen errors
by cdarke (Prior) on Feb 17, 2010 at 11:57 UTC
    If you wish to trap the error message before it gets displayed to STDERR then use an eval block. The error message (if any) will be in $@:
    use warnings; use strict; my $thing; eval {$thing->Cells(1234)}; print "Error <$@>" if $@;
      Correct. Of course, in this case, it would be better to check
      defined($thing)
      and avoid the error in the first place.
Re: to get the un-forseen errors
by biohisham (Priest) on Feb 17, 2010 at 11:51 UTC
    Generally, Depending on the type of error, there are variables that are Error Indicators, each of these correspond to errors detected by:
    1. The Perl Interpreter.
    2. C Library.
    3. Operating System.
    4. External Programs.

    These variables can be accessed many ways (they have more than one form), for example you can use $! or $OS_ERROR or $ERRNO to read the current value of the C error number in numeric context also you can use $! in string context to yield the corresponding system error string..

    perlvar has valuable information about these different variables.


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
Re: to get the un-forseen errors
by rovf (Priest) on Feb 17, 2010 at 11:58 UTC
    Can't call method "Cells" on an undefined value at Perl_XYZ.pl line XX. My question here is that: where is that error message stored
    Technically, this question can't be answered without seeing all the code which is used here (i.e. including the modules you use for accessing your worksheet), but in this case, we can be nearly sure that the text of the template for this message is stored inside the Perl runtime system.

    Just wondering why you are concerned about this....

    -- 
    Ronald Fischer <ynnor@mm.st>
      I am looking for a way to capture the last thrown error in case the perl script aborts due to some error, coz I have to capture and return that error to QTP from where my perl script was invoked.

      That error line was just an example. I am handling the errors almost every where, but I just need a way to be sure, if an error that I have not handled occurs, at least I have the right error text being returned.

      Thanks,
      J
        I am looking for a way to capture the last thrown error in case the perl script aborts due to some error

        If the error is printed by a die statement, you can of course catch the exception using eval (and the message will be stored in $@). In the example you gave, this will likely work (unless some of the intervening functions in the call stack do some nasty countermeasures, of course). If the error is just printed without throwing an exception, there is no way to handle it, unless you are willing to modify the source code which causes the error.

        -- 
        Ronald Fischer <ynnor@mm.st>
Re: to get the un-forseen errors
by Corion (Patriarch) on Feb 18, 2010 at 10:40 UTC

    In my home-grown logger class I use the following hooks:

    $SIG{__WARN__} = \&log_error; $SIG{__DIE__} = sub { log_fatal_error( @_ ); exit 1 };

    ... which makes my log routines get called whenever a warning is issued or the die command is used. This does not work universally, because $SIG{__DIE__} will also get triggered in the following situation:

    $SIG{__DIE__} = sub { print "good-by\n" }; eval { die "adios"; }

    Which usually is not what you want. But in my case it works out fine because I don't have stacks of eval{} layers and any exception is fatal in my situation.