For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > let calling programm die?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author let calling programm die?
.rhavin grobert

2006-01-25, 7:55 am

hi there, i just want to know if the following is possible...


when i let my module die via
die 'something';
then it prints "something at line ##"
with ## = linenumber inside module... so far OK.
but i want to print the linenumber in the calling program, eg.

# calling prog:
..
..
31 # do something
32 &module::sub(bla);
33 # do something
..
..

# module
..
..
15 sub sub {
16 die 'your my killer';
17 }
..
..

.....should give me the line 32 (somehow).
is this possible?

.rhavin grobert

2006-01-25, 7:55 am

oh, and of course

&module::sub(bla) || die 'something';

is not the answer im looking for, because that would mean me to do it
every time i use &module::sub.

.rhavin grobert

2006-01-25, 7:55 am

> when i let my module die via
> die 'something';
> then it prints "something at line ##"
> with ## = linenumber inside module... so far OK.
> but i want to print the linenumber in the calling program...


OK, because this really was a RTFM, i answer it myself
(i've just read about 'caller' ;-)

sub todie {
my (undef, undef, $line) = caller;
die 'line of caller is '.$line;
}

so sorry for spamming...

Paul Lalli

2006-01-25, 7:55 am

..rhavin grobert wrote:
>
> OK, because this really was a RTFM, i answer it myself
> (i've just read about 'caller' ;-)
>
> sub todie {
> my (undef, undef, $line) = caller;
> die 'line of caller is '.$line;
> }
>
> so sorry for spamming...


Good that you found your own solution. Bad that you found the wrong
solution. :-) The caller() solution does not meet your original
specifications, as this will still print the original line numbers
after your message.

The correct answer is to use Carp's croak() instead of die()

package MyMod;
use Carp;

sub todie{
if (user_error()){
croak "You did something wrong";
}
# . . .
}

perldoc Carp
for more information.

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com