Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

carp always showing stacktrace

by LanX (Saint)
on Jul 20, 2020 at 16:25 UTC ( [id://11119550]=perlquestion: print w/replies, xml ) Need Help??

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

Hi

according to Carp should carp()

# warn user (from perspective of caller) but without stacktrace

But no matter what I try, I'm getting a stacktrace and can't switch it off.

Any idea what's going wrong?

use strict; use warnings; use Carp; BEGIN {$Carp::Verbose=0}; $\="\n"; use constant DBG => 1; sub dbout { $Carp::Verbose=0; carp "@_" if DBG; } dbout 1..3; __END__;

C:/Perl_524/bin\perl.exe -w d:/PERL/perl/perl/signature.pl 1 2 3 at d:/PERL/perl/perl/signature.pl line 15. + # don't need this ... main::dbout(1, 2, 3) called at d:/PERL/perl/perl/signature.pl line + 18 # ... but this Compilation finished at Mon Jul 20 18:22:11

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

update

FWIW, my workaround. I never liked Carp anyway.

sub dbout { my ( undef, $filename, $line ) = caller(0); warn "@_ at $filename line $line\n" if DBG; }

Replies are listed 'Best First'.
Re: carp always showing stacktrace
by hippo (Bishop) on Jul 20, 2020 at 17:19 UTC

    You are not calling the sub from a different module. The Carp documentation says right at the top (emphasis mine):

    For a shorter message you can use carp() or croak() which report the error as being from where your module was called. shortmess() returns the contents of this error message.

    Therefore this carps with the context of the call in main only:

    use strict; use warnings; package Foo; use Carp; sub dbout { carp "@_"; } package main; Foo::dbout (1..3);

    Here's the STDERR:

    1 2 3 at /tmp/lanxcarp.pl line 14.
      > You are not calling the sub from a different module.

      Argh damn ... we had this discussion before...

      Thanks! :)

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

Re: carp always showing stacktrace
by Perlbotics (Archbishop) on Jul 20, 2020 at 16:44 UTC

    BEGIN {$Carp::Verbose=0; $Carp::CarpLevel = 1; #<-- add this (for your demo) };
    From Carp:
    6. $Carp::CarpLevel can be set to skip a fixed number of additional call levels. Using this is not recommended because it is very difficult to get it to behave correctly.
    So, you might need to use something like local $Carp::CarpLevel=1+=$whatever; ...

      Thanks, I'm using my own solution from the update now.

      It's shorter and easier than fiddling with Carp.

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

Re: carp always showing stacktrace
by siberia-man (Friar) on Jul 21, 2020 at 12:07 UTC
    By this link I've found the answer how to distinguish difference among these functions: What's the difference between Carp/Croak, Cluck/Confess, and verbose options?. Unfortunately there are no any mnemonic rules to memorize their usage.

    Too hard to foresee meanings of these four C-words, when they express something very similar each other. I understand that using them makes Perl lexicon richer even more poetical, but logic is definitely lost.
    warn - carp (critisize) - cluck (sound like a chicken) die - croak (sound like a frog) - confess (recognize about sin)
    Just for lulz. cluck looks like Russian клац (meaning something similar to click-clack a weapon). Croak is pronounced closer to кряк, which means in Russian to sound like a goose. In the other hand, quack is pronounced exactly like croak in Russian.

      I personally find the names intuitive but the meanings are idiomatic and perhaps overly cute/clever for non-native speakers. Cluck also means to “wag one’s tongue.” Croak is also to die; even used as a verb to kill. Confess is to explain a sin at length or in detail. Alliteration is a kind of mnemonic.

      Update, typo corrected via hippo. Life imitates me imitating life -> :P s/tonue/tongue/;

      Thanks for the SO-link ...

      I totally agree with you that this naming is unfortunate.

      To the point that I can't read my own modules w/o comments.

      Now I'm pondering about aliasing them in my own modules, to help me understand my code

      expanding the text from SO with potential aliases.

      There are 2 sets of yes/no options. The function can be fatal (like die) or nonfatal (like warn). It can report just the line where the function was called, or it can report a full backtrace.

      Fatal Backtrace Alias carp N N warn_user ° cluck N Y warn_full croak Y N die_user confess Y Y die_full

      FWIW: Renaming subs is easy in Perl.

      *warn_user = \&Carp::carp

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

      °) it's not warning the caller

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-19 10:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found