Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Carp croak not working when used in an imported function

by Anonymous Monk
on Sep 29, 2006 at 22:26 UTC ( [id://575614]=perlquestion: print w/replies, xml ) Need Help??

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

I am importing a function called "errmsg" into my module. This function formats my error messages, specifically it handles dies. This function lives in a helper module.

The main module dies via "errmsg" when someone passes the wrong argument to a method.

The problem is that the error is always reported as happening in the module and not in the program that calls the method with the wrong argument. So I am trying to use Carp's croak function, but it is not working right.

In my "errmsg" function, I am using 'Carp::croak'. (I have to qualify it because CGI::Carp is also importing croak apparently.) Is there something I need to add to 'Carp::croak' to get it to report the error as I want it? Thanks. Example code:

sub errmsg { Carp::croak "$_[0]"; }

Replies are listed 'Best First'.
Re: Carp croak not working when used in an imported function
by ikegami (Patriarch) on Sep 29, 2006 at 23:10 UTC
    Even though it's been imported (say into PkgB), errmsg was compiled in another package (say into PkgA). croak will skip over any calls within PkgA because that's where errmsg was compiled. It doesn't skip over the calls in PkgB.

    A couple of ways around this:

    package PkgB; sub errmsg { local $Carp::CarpLevel = $Carp::CarpLevel + 1; PkgA::errmsg(@_); }
    or
    package PkgB; our @ISA = 'PkgA';
    or
    package PkgB; our @CARP_NOT = 'PkgA';
Re: Carp croak not working when used in an imported function
by mreece (Friar) on Sep 29, 2006 at 22:49 UTC
    try this?
    sub errmsg { local $Carp::CarpLevel = 1; Carp::croak "$_[0]"; }

    update: i understand this is a rather contentious 'feature'! some suggest +=1 instead, or consider Carp::Clan..
    again: +=1 doesn't work as i expected on a local, use local $Carp::CarpLevel = $Carp::CarpLevel + 1; as shenme suggested below!

      Quick, before tilly notices, change that to
      local $Carp::CarpLevel = $Carp::CarpLevel + 1;

      Hi,

      Why quoting the parameter to croak? "$_[0]" -> $_[0]

      I don't see the problem:

      cucu.pm
      package cucu; use strict; use Carp (); sub error { Carp::croak @_; } 1;
      t.pl
      use strict; use cucu; ty(); sub ty { cucu::error('blabla'); }

      Regards,

      fmerges at irc.freenode.net
      That did it! Thanks.
Re: Carp croak not working when used in an imported function
by diotalevi (Canon) on Sep 29, 2006 at 23:33 UTC

    This is a problem that Carp::Clan solves.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://575614]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-24 02:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found