D'Oh! There were other two interesting posts in the original thread, which settle down the question completely. I'm reporting them here as a reply, else I think the root node may become cluttered as it's late now for such thorough update. To Anno's reply Paul Lalli posted the followup hereafter:

Re: croak/confess from within File::Find (Paul Lalli)

Add this:

push our @CARP_NOT, 'File::Find';
find(\&wanted, '.'); #line 22 __END__

Carp has a hard time assigning an error location in callback situations. It climbs the stack, essentially watching for a change in the calling package. When called from a callback it meets that change earlier than intended (from your main to File::Find). The variable @CARP_NOT is checked and a package change is ignored if one of the packages is on the other's @CARP_NOT.

Anno,

Thank you for the information. Annoyingly, it seems that if all the packages are "trusted" (as per Carp's docs), then croak behaves exactly like confess:

#!/opt2/perl/bin/perl use strict; use warnings; use Carp; use File::Find; sub err { croak ("You did something bad!"); #line 8 } sub wanted { err(); #line 12 } push our @CARP_NOT, 'File::Find'; find(\&wanted, '.'); #line 15 __END__
$ ./ff_carp.pl You did something bad! at ./ff_carp.pl line 8 main::err() called at ./ff_carp.pl line 12 main::wanted() called at /opt2/Perl5_8_4/lib/perl5/5.8.4/File/ Find.pm line 810 File::Find::_find_dir('HASH(0x13e8d4)', ., 2) called at /opt2/ Perl5_8_4/lib/perl5/5.8.4/File/Find.pm line 690 File::Find::_find_opt('HASH(0x13e8d4)', .) called at /opt2/ Perl5_8_4/lib/perl5/5.8.4/File/Find.pm line 1193 File::Find::find('CODE(0x15e498)', .) called at ./ff_carp.pl line 15

I suppose that's better than nothing. It just bugs me that there doesn't seem to be a way to just have it print where the subroutine was called from. I suppose I could fiddle with caller() to get exactly what I want, but doesn't it seem like carp/croak should be able to do this on its own?

Paul Lalli

Re: croak/confess from within File::Find (Anno)

Add this:

push our @CARP_NOT, 'File::Find';
find(\&wanted, '.'); #line 22 __END__

Carp has a hard time assigning an error location in callback situations. It climbs the stack, essentially watching for a change in the calling package. When called from a callback it meets that change earlier than intended (from your main to File::Find). The variable @CARP_NOT is checked and a package change is ignored if one of the packages is on the other's @CARP_NOT.

Anno,

Thank you for the information. Annoyingly, it seems that if all the packages are "trusted" (as per Carp's docs), then croak behaves exactly like confess:

#!/opt2/perl/bin/perl use strict; use warnings; use Carp; use File::Find; sub err { croak ("You did something bad!"); #line 8 } sub wanted { err(); #line 12 } push our @CARP_NOT, 'File::Find'; find(\&wanted, '.'); #line 15 __END__
$ ./ff_carp.pl You did something bad! at ./ff_carp.pl line 8 main::err() called at ./ff_carp.pl line 12 main::wanted() called at /opt2/Perl5_8_4/lib/perl5/5.8.4/File/ Find.pm line 810 File::Find::_find_dir('HASH(0x13e8d4)', ., 2) called at /opt2/ Perl5_8_4/lib/perl5/5.8.4/File/Find.pm line 690 File::Find::_find_opt('HASH(0x13e8d4)', .) called at /opt2/ Perl5_8_4/lib/perl5/5.8.4/File/Find.pm line 1193 File::Find::find('CODE(0x15e498)', .) called at ./ff_carp.pl line 15

I suppose that's better than nothing. It just bugs me that there doesn't seem to be a way to just have it print where the subroutine was called from. I suppose I could fiddle with caller() to get exactly what I want, but doesn't it seem like carp/croak should be able to do this on its own?

You can have that if you compile the wanted() function in a package of its own:

{ package CarpCatcher; use Carp; sub wanted { err(); #line 12 } sub err { croak ("You did something bad!"); } push our @CARP_NOT, 'File::Find'; } use File::Find; find(\&CarpCatcher::wanted, '.'); __END__

Anno


In reply to Re: croak/confess from within File::Find by blazar
in thread croak/confess from within File::Find by blazar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.