Getting croak or carp into variable ?

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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)...
    2. 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 ...
    3. 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....
    4. 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...
    5. $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...
  3. #2

    Default Re: Getting croak or carp into variable ?

    On Feb 10, 2004, at 9:56 AM, Dan Muey wrote:
    > 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: ?
    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";'

    James

    James Edward Gray II Guest

  4. #3

    Default RE: Getting croak or carp into variable ?

    > On Feb 10, 2004, at 9:56 AM, Dan Muey wrote:
    >
    > > 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: ?
    >
    > 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";'
    Oh yeah %SIG I've nvere really messed with it before.
    I'll check it out a bit more, thanks James!

    Dan
    >
    > James
    >
    >
    Dan Muey Guest

  5. #4

    Default RE: Getting croak or carp into variable ?

    > 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__ ??)
    > 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.
    > James
    Dan Muey Guest

  6. #5

    Default Re: Getting croak or carp into variable ?

    On Feb 10, Dan Muey said:
    >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

    --
    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

  7. #6

    Default Re: 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?
    >
    It appears you are talking about exceptions in a try/catch manner? You
    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

  8. #7

    Default RE: Getting croak or carp into variable ?

    > >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
    That would require modifying the module which I want to avoid.
    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

  9. #8

    Default RE: Getting croak or carp into variable ?

    > Dan Muey wrote:
    >
    > [snip]
    >
    > > $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?
    > >
    >
    > 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.
    >
    Cool, I was wondering about if that was possible, overriding the function.
    I'll try that out a bit and see how iut can fit into my scheme.

    Thanks david!
    > david
    Dan Muey Guest

  10. #9

    Default RE: Getting croak or carp into variable ?

    Dan Muey wrote:

    [snip]
    > $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?
    >
    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.

    david
    --
    sub'_{print"@_ ";* \ = * __ ,\ & \}
    sub'__{print"@_ ";* \ = * ___ ,\ & \}
    sub'___{print"@_ ";* \ = * ____ ,\ & \}
    sub'____{print"@_,\n"}&{_+Just}(another)->(Perl)->(Hacker)
    David Guest

  11. #10

    Default Re: Getting croak or carp into variable ?

    On Feb 10, 2004, at 11:53 AM, Dan Muey wrote:
    >> 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__ ??)
    Just undef them:

    undef $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";'
    >>
    >
    > 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.
    This was my misunderstanding. I looked back over the __DIE__ handler.
    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

  12. #11

    Default RE: Getting croak or carp into variable ?

    > 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.
    Thanks that helped out abunch I believe it has me up and running!
    I use the Carp::die so I won't effect any real die()s just the croak()s

    Thanks again!

    Dan
    Dan Muey Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139