Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Should I use carp/croak or warn/die

by LanX (Sage)
on May 30, 2018 at 16:13 UTC ( #1215479=note: print w/replies, xml ) Need Help??


in reply to Should I use carp/croak or warn/die

On a meta level ...

Personally I find the interface not too convincing

  • The words "carp", "cluck" and "croak" are far from basic English and hard to explain to colleagues. ( Why do ravens "die" while a fish only "warns"?)
  • The connotation to the caller is not evident for me. (Do ravens croak to (telephone) callers?)
  • Sometimes I'd like to also have a stack-trace for warn/die (i.e. from the actual error line - yes I know Devel::Confess but why an extra module?)
  • Sometimes I don't know how many levels up the user of my module called. Having an option to "carp" from the first caller in a different package would be nice. (This is more a nice to have) happens already.
I ended up writing my own routines handling all of this, because we have to install our own die-handlers too, but I'm far from having a convincing solution yet.

Even considering the interface ...

... maybe something like warn_up $level, msg with special "levels", like a package-regex or something and negative numbers for stack-trace.

Nevertheless thanks for the question, frontpaged! =)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: Should I use carp/croak or warn/die
by jeffenstein (Friar) on May 30, 2018 at 16:45 UTC

    I'm guessing it's an Americanism. Sorry about that to the rest of the world ;)

    "Carping" is slang for complaining about something. ex: He was carping about the wait for a table. "Croaking" is slang for dying. ex: Your pet fish croaked last night

    They were rare in my youth, and outside of Perl, I haven't heard them used in probably 30 years.

    Update: Just for kicks, here are some synonyms for "carp" from the Merriam-Webster dictionary: bellyache, bleat, caterwaul, croak, fuss, gripe, grouse, nag, whine, and whinge. It could have been worse.

      Actually, "carp", used as a verb, meaning complain, has been in use since the 1500s, so is long past the slang stage.

      However, "croak" is still considered a slang term.

      Well yes, I can construct mnemonics, German and English being very similar helps.

      But still I'm confused about the need to have several names for things which are almost similar - i.e. different only in orthogonal dimensions - instead of parametrizing them.

      Thanks anyway! =)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        I agree that PHPisms like 500 extra, semi-overlapping keywords can be a bad thing but I've always liked Carp's interface. Taking the generalized and parameterized route to extremes is warn and die becoming replaced with error($level, $message) or something. I use Carp's functions enough, including longmess and shortmess for logs, that I much prefer having specific functions for acking around and such.

        I can only guess why Larry & Co. did it this way, and that guess would be that it was to preserve backwards-compatibility with perl 4, which already had the current interface for warn and die.

        Did caller exist in perl 4? It kind of makes sense to use a different interface since modules got a lot of changes in 5.0

Re^2: Should I use carp/croak or warn/die
by SBECK (Chaplain) on May 30, 2018 at 16:22 UTC
    Thanks for your reply! When/if you ever release the a module to do this, I definitely would want to use it. I'm learning that I really need to rethink how I do warn/die (or their equivalent).
Re^2: Should I use carp/croak or warn/die
by Haarg (Curate) on May 31, 2018 at 19:23 UTC
    Having an option to "carp" from the first caller in a different package would be nice.
    This is how carp and croak always work.
      Yeah I noticed in the meantime

      See Re^4: Should I use carp/croak or warn/die (@CARP_NOT)

      Though I'm not happy about the documentation.

      the Synopsis says

      > warn user (from perspective of _caller_)

      But later the doc says inside the text

      > carp() or croak() which report the error as being from where your module was called.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Re^2: Should I use carp/croak or warn/die (@CARP_NOT)
by LanX (Sage) on May 30, 2018 at 20:02 UTC
    > Sometimes I don't know how many levels up the user of my module called. Having an option to "carp" from the first caller in a different package would be nice. (This is more a nice to have)

    Though I'm not sure if @CARP_NOT could be used for this ...

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      From my experience, mucking with the likes of caller and/or any aspect of such an undertaking of tracking and reporting from nested modules and subs is fraught with frustration, angst, head-desking and oftentimes throwing things... particularly when dealing with anony-subs and the like :)

        That's right. But sometimes caller is needed to implement nifty YAGNIs :)

        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
      Though I'm not sure if @CARP_NOT could be used for this ...
      How could it?

      I mean @CARP_NOT is a package variable that is (or so I suppose) evaluated at compile-time.

      How could that be used to implement the run-time feature of reporting the the first caller of a different package?

      I don't really know to be honest, you could be right, but my working hypothesis here is that you're simply blah-blahing here.

      So please enlighten us...

        I wouldn't ask if I knew.

        > evaluated at compile-time.

        nope that's obvious from the docs and from a simple test.

        > you're simply blah-blahing here.

        Look who's talking...

        Anyway carp() seems to already do by default what I wanted. \o/

        It doesn't report from the caller function but the caller package.

        use strict; use warnings; use Data::Dump qw/pp dd/; TEST::first(); # <- line 6 #push @main::CARP_NOT, qw(TEST); # uncomment to see dynamic behaviour #TEST::first(); package TEST; use Carp; sub first { second(); } sub second { carp "some warning"; }

        some warning at d:/Users/RolfLangsdorf/pm/carp_not.pl line 6.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2021-12-03 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (30 votes). Check out past polls.

    Notices?