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:
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
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |