Ask a Question related to PERL Beginners, Design and Development.
-
Dan Muey #1
Getting croak or carp into variable ?
Hello List,
I am using a module that does use Carp; and you
can specify whther you want croak or carp on an error
Which is cool. But there is no way to specify anythign
else besides those two.
So the way it is you just do:
my $res = funktion('foobarmonkey',err_doer => 'carp');
And it will do
carp('what the heck is foobarmonkey');
What I'd like to do is get any errors into a variable:
my $err = '';
my $res = funktion('foobarmonkey',err_doer => 'puterrorinvar');
if($err) {
print "Error: $err\n";
print "Logging error..."'
if(logerror($err) { print "Ok\n"; } else { print "Failed!\n"; }
print "Emailing Admin...";
if(emailadmin($err)) { print "Ok\n"; } else { print "Failed!\n"; }
# and now that we did that we can go ahead and carp, craok die, whatever
}
Except I can't simply specify a new function for errors because it checks for what you enter to see if its in a hash and if not defaults to one or the other. (I could modify the module but then it won't work for all)
SO I guess the question is, is there a way to get carp or croak into a variable?
Something like this perhaps: ?
my $err = '';
putcarpinvar_on(\$err); # this is an example to illustrate what I'm shooting for it is not real
my $res = funktion('foobarmonkey',err_doer => 'puterrorinvar');
putcarpinvar_off(\$err); # this is an example to illustrate what I'm shooting for it is not real
if($err) { ...
Any ideas?
TIA
Dan
Dan Muey Guest
-
Carp -- shortmess and longmess
perldoc Carp says: carp - warn of errors (from perspective of caller) cluck - warn of errors with stack backtrace (not exported by default)... -
ANN: Win32::EventLog::Carp 1.30 released
The latest version of Win32::EventLog::Carp has been uploaded to PAUSE and should begin showing up in a CPAN mirror near you at ... -
No $VERSION for Carp modules
I was documenting the list of modules and module versions that we use in our applications, and noticed that Carp does not have a $VERSION defined.... -
CGI::Carp Problems
Hi all, I'm fairly new to Perl so excuse how stupid i sound. I have this perl script that needs CGI, so Carp.pm gets installed I got the... -
$SIG{__DIE__} doesn't make sense when using CGI::Carp
Me and my cgi-script have the following problem. I'm using the package CGI::Carp (which installs internally some $SIG{__DIE___} handlers). In... -
James Edward Gray II #2
Re: Getting croak or carp into variable ?
On Feb 10, 2004, at 9:56 AM, Dan Muey wrote:
Maybe. Check out this one liner:> Hello List,
>
> I am using a module that does use Carp; and you
> can specify whther you want croak or carp on an error
> Which is cool. But there is no way to specify anythign
> else besides those two.
>
> So the way it is you just do:
>
> my $res = funktion('foobarmonkey',err_doer => 'carp');
>
> And it will do
> carp('what the heck is foobarmonkey');
>
> What I'd like to do is get any errors into a variable:
>
> my $err = '';
> my $res = funktion('foobarmonkey',err_doer => 'puterrorinvar');
>
> if($err) {
> print "Error: $err\n";
> print "Logging error..."'
> if(logerror($err) { print "Ok\n"; } else { print "Failed!\n"; }
> print "Emailing Admin...";
> if(emailadmin($err)) { print "Ok\n"; } else { print "Failed!\n"; }
> # and now that we did that we can go ahead and carp, craok die,
> whatever
> }
>
> Except I can't simply specify a new function for errors because it
> checks for what you enter to see if its in a hash and if not defaults
> to one or the other. (I could modify the module but then it won't work
> for all)
>
> SO I guess the question is, is there a way to get carp or croak into a
> variable?
> Something like this perhaps: ?
perl -MCarp -e '$SIG{__WARN__} = sub { $err = shift; }; carp
"Error!\n"; print "We caught $err";'
Strangely though, this did not work for me, though I expected it to:
perl -MCarp -e '$SIG{__DIE__} = sub { $err = shift; }; croak
"Error!\n"; print "We caught $err";'
James
James Edward Gray II Guest
-
Dan Muey #3
RE: Getting croak or carp into variable ?
Oh yeah %SIG I've nvere really messed with it before.> On Feb 10, 2004, at 9:56 AM, Dan Muey wrote:
>> "Failed!\n"; }> > Hello List,
> >
> > I am using a module that does use Carp; and you
> > can specify whther you want croak or carp on an error
> > Which is cool. But there is no way to specify anythign
> > else besides those two.
> >
> > So the way it is you just do:
> >
> > my $res = funktion('foobarmonkey',err_doer => 'carp');
> >
> > And it will do
> > carp('what the heck is foobarmonkey');
> >
> > What I'd like to do is get any errors into a variable:
> >
> > my $err = '';
> > my $res = funktion('foobarmonkey',err_doer => 'puterrorinvar');
> >
> > if($err) {
> > print "Error: $err\n";
> > print "Logging error..."'
> > if(logerror($err) { print "Ok\n"; } else { print "Failed!\n"; }
> > print "Emailing Admin...";
> > if(emailadmin($err)) { print "Ok\n"; } else { print> not defaults> > # and now that we did that we can go ahead and carp, craok die,
> > whatever
> > }
> >
> > Except I can't simply specify a new function for errors because it
> > checks for what you enter to see if its in a hash and if> won't work> > to one or the other. (I could modify the module but then it> croak into a> > for all)
> >
> > SO I guess the question is, is there a way to get carp or>> > variable?
> > Something like this perhaps: ?
> Maybe. Check out this one liner:
>
> perl -MCarp -e '$SIG{__WARN__} = sub { $err = shift; }; carp
> "Error!\n"; print "We caught $err";'
>
> Strangely though, this did not work for me, though I expected it to:
>
> perl -MCarp -e '$SIG{__DIE__} = sub { $err = shift; }; croak
> "Error!\n"; print "We caught $err";'
I'll check it out a bit more, thanks James!
Dan
>
> James
>
>Dan Muey Guest
-
Dan Muey #4
RE: Getting croak or carp into variable ?
> perl -MCarp -e '$SIG{__WARN__} = sub { $err = shift; };
This works great!> carp "Error!\n"; print "We caught $err";'
>
And changing $SIG{__WARN__} to '' will return default behaviour correct?
(Same thign with __DIE__ ??)
Anyone have any idea how to get $SIG{__DIE__} to act like the $SIG{__WARN__} ?> Strangely though, this did not work for me, though I expected it to:
>
> perl -MCarp -e '$SIG{__DIE__} = sub { $err = shift; }; croak
> "Error!\n"; print "We caught $err";'
>
I'm sure it has something to do with die doing an exit() or soemthing.
> JamesDan Muey Guest
-
Jeff 'Japhy' Pinyan #5
Re: Getting croak or carp into variable ?
On Feb 10, Dan Muey said:
You want Carp::shortmess().>I am using a module that does use Carp; and you
>can specify whther you want croak or carp on an error
>Which is cool. But there is no way to specify anythign
>else besides those two.
>
>What I'd like to do is get any errors into a variable:
perldoc Carp
--
Jeff "japhy" Pinyan [email]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
[ I'm looking for programming work. If you like my work, let me know. ]
Jeff 'Japhy' Pinyan Guest
-
Wiggins D Anconia #6
Re: Getting croak or carp into variable ?
> Hello List,
checks for what you enter to see if its in a hash and if not defaults to>
> I am using a module that does use Carp; and you
> can specify whther you want croak or carp on an error
> Which is cool. But there is no way to specify anythign
> else besides those two.
>
> So the way it is you just do:
>
> my $res = funktion('foobarmonkey',err_doer => 'carp');
>
> And it will do
> carp('what the heck is foobarmonkey');
>
> What I'd like to do is get any errors into a variable:
>
> my $err = '';
> my $res = funktion('foobarmonkey',err_doer => 'puterrorinvar');
>
> if($err) {
> print "Error: $err\n";
> print "Logging error..."'
> if(logerror($err) { print "Ok\n"; } else { print "Failed!\n"; }
> print "Emailing Admin...";
> if(emailadmin($err)) { print "Ok\n"; } else { print "Failed!\n"; }
> # and now that we did that we can go ahead and carp, craok die, whatever
> }
>
> Except I can't simply specify a new function for errors because it
one or the other. (I could modify the module but then it won't work for all)variable?>
> SO I guess the question is, is there a way to get carp or croak into ashooting for it is not real> Something like this perhaps: ?
>
> my $err = '';
> putcarpinvar_on(\$err); # this is an example to illustrate what I'mshooting for it is not real> my $res = funktion('foobarmonkey',err_doer => 'puterrorinvar');
> putcarpinvar_off(\$err); # this is an example to illustrate what I'mIt appears you are talking about exceptions in a try/catch manner? You> if($err) { ...
>
> Any ideas?
>
may want to look at
perldoc -f die
perldoc -f eval
And see if the mess of docs there helps. It is pretty scattered at least
to me, but I think it will provide what you want. An example might be,
my $res = eval { call_function() };
if ($@) {
# $@ contains your variable's value
}
# else process $res here
This seems like it would simulate what you are after. Because croak is
just a replacement for 'die' it should work the same way giving you the
same benefits. It is really the 'eval' that is the significant part of
the construct despite it being explained in the 'die' docs...
I have a cursory understanding of standard exception models, and have
built my own version for Perl modules that doesn't quite fit into the
above (yet!), but reading the Learning Python book has helped solidify
my understanding and suggesting the changes I need to make to my own
implementation.
Thoughts?
[url]http://danconia.org[/url]
Wiggins D Anconia Guest
-
Dan Muey #7
RE: Getting croak or carp into variable ?
That would require modifying the module which I want to avoid.>> >I am using a module that does use Carp; and you
> >can specify whther you want croak or carp on an error
> >Which is cool. But there is no way to specify anythign
> >else besides those two.
> >
> >What I'd like to do is get any errors into a variable:
> You want Carp::shortmess().
>
> perldoc Carp
Inside a function of another package it calls either carp or croak.
I'd like to get that error into a variable I can use and not die
or warn from croak or carp.
my $err = '';
$SIG{__WARN__} = sub { $err = shift; }
$SIG{__DIE__} = sub {$err = shift; }
my $res = function_that_carps_or_croaks_that_i_cant_modify() ;
if($err) { handle_error_my_way_instead_of_simply_carping_or_c roaking($err); }
$SIG{__WARN__} = ''; # or is undef or delete better??
$SIG{__DIE__} = ''; # or is undef or delete better??
That works like a charm but it does not work with $SIG{__DIE__} for some reason.
It still just croaks as usual. Any ideas?
>
> --
> Jeff "japhy" Pinyan [email]japhy@pobox.com[/email]Dan Muey Guest
-
Dan Muey #8
RE: Getting croak or carp into variable ?
> Dan Muey wrote:
Cool, I was wondering about if that was possible, overriding the function.>
> [snip]
>> $SIG{__DIE__} => > $SIG{__WARN__} = ''; # or is undef or delete better??>> > ''; # or is undef or delete better??
> >
> > That works like a charm but it does not work with $SIG{__DIE__} for
> > some reason. It still just croaks as usual. Any ideas?
> >
> there are at least a couple of ways of doing that:
>
> #!/usr/bin/perl -w
> use strict;
>
> BEGIN{
> use subs qw(Carp::die);
> use vars qw($e);
> sub Carp::die{ $e = "Carp::die: @_" }
> }
>
> use Carp;
>
> croak "croaking";
>
> print "after croak \$e is: $e";
>
> __END__
>
> prints:
>
> after croak $e is: Carp::die: croaking at x.pl line 10
>
> another way to accomplish the same thing:
>
> #!/usr/bin/perl -w
> use strict;
>
> BEGIN{
>
> our $e;
>
> *CORE::GLOBAL::die = sub{
> $e = "CORE:GLOBAL::die => @_";
> };
> }
>
> use Carp;
>
> use vars qw($e);
>
> croak "croaking";
>
> print "after croak \$e is: $e";
>
> __END__
>
> prints:
>
> after croak $e is: CORE:GLOBAL::die => croaking at x.pl line 10
>
> i don't have time to check out the source of Carp.pm but if
> you do, i would
> suggest you go take a look as there might be a better
> solution to it. out
> of the 2 methods i described, the second one is more natural, imo.
>
I'll try that out a bit and see how iut can fit into my scheme.
Thanks david!
> davidDan Muey Guest
-
David #9
RE: Getting croak or carp into variable ?
Dan Muey wrote:
[snip]
there are at least a couple of ways of doing that:> $SIG{__WARN__} = ''; # or is undef or delete better??
> $SIG{__DIE__} = ''; # or is undef or delete better??
>
> That works like a charm but it does not work with $SIG{__DIE__} for some
> reason. It still just croaks as usual. Any ideas?
>
#!/usr/bin/perl -w
use strict;
BEGIN{
use subs qw(Carp::die);
use vars qw($e);
sub Carp::die{ $e = "Carp::die: @_" }
}
use Carp;
croak "croaking";
print "after croak \$e is: $e";
__END__
prints:
after croak $e is: Carp::die: croaking at x.pl line 10
another way to accomplish the same thing:
#!/usr/bin/perl -w
use strict;
BEGIN{
our $e;
*CORE::GLOBAL::die = sub{
$e = "CORE:GLOBAL::die => @_";
};
}
use Carp;
use vars qw($e);
croak "croaking";
print "after croak \$e is: $e";
__END__
prints:
after croak $e is: CORE:GLOBAL::die => croaking at x.pl line 10
i don't have time to check out the source of Carp.pm but if you do, i would
suggest you go take a look as there might be a better solution to it. out
of the 2 methods i described, the second one is more natural, imo.
david
--
sub'_{print"@_ ";* \ = * __ ,\ & \}
sub'__{print"@_ ";* \ = * ___ ,\ & \}
sub'___{print"@_ ";* \ = * ____ ,\ & \}
sub'____{print"@_,\n"}&{_+Just}(another)->(Perl)->(Hacker)
David Guest
-
James Edward Gray II #10
Re: Getting croak or carp into variable ?
On Feb 10, 2004, at 11:53 AM, Dan Muey wrote:
Just undef them:>>> perl -MCarp -e '$SIG{__WARN__} = sub { $err = shift; };
>> carp "Error!\n"; print "We caught $err";'
>>
> This works great!
>
> And changing $SIG{__WARN__} to '' will return default behaviour
> correct?
> (Same thign with __DIE__ ??)
undef $SIG{__WARN__};
This was my misunderstanding. I looked back over the __DIE__ handler.>>> Strangely though, this did not work for me, though I expected it to:
>>
>> perl -MCarp -e '$SIG{__DIE__} = sub { $err = shift; }; croak
>> "Error!\n"; print "We caught $err";'
>>
> Anyone have any idea how to get $SIG{__DIE__} to act like the
> $SIG{__WARN__} ?
> I'm sure it has something to do with die doing an exit() or soemthing.
It gets called on the way down, it doesn't override the fact that you
are going down. My bad. Use __WARN__.
James
James Edward Gray II Guest
-
Dan Muey #11
RE: Getting croak or carp into variable ?
> there are at least a couple of ways of doing that:
Thanks that helped out abunch I believe it has me up and running!>
> #!/usr/bin/perl -w
> use strict;
>
> BEGIN{
> use subs qw(Carp::die);
> use vars qw($e);
> sub Carp::die{ $e = "Carp::die: @_" }
> }
>
> use Carp;
>
> croak "croaking";
>
> print "after croak \$e is: $e";
>
> __END__
>
> prints:
>
> after croak $e is: Carp::die: croaking at x.pl line 10
>
> another way to accomplish the same thing:
>
> #!/usr/bin/perl -w
> use strict;
>
> BEGIN{
>
> our $e;
>
> *CORE::GLOBAL::die = sub{
> $e = "CORE:GLOBAL::die => @_";
> };
> }
>
> use Carp;
>
> use vars qw($e);
>
> croak "croaking";
>
> print "after croak \$e is: $e";
>
> __END__
>
> prints:
>
> after croak $e is: CORE:GLOBAL::die => croaking at x.pl line 10
>
> i don't have time to check out the source of Carp.pm but if
> you do, i would
> suggest you go take a look as there might be a better
> solution to it. out
> of the 2 methods i described, the second one is more natural, imo.
I use the Carp::die so I won't effect any real die()s just the croak()s
Thanks again!
Dan
Dan Muey Guest



Reply With Quote

