Monks,
I ran across a strange bug in the code of one of my modules, and I'm puzzled.
I use the $haveModule = eval { require Module; 1; } idiom fairly frequently, but it broke down for the Capture::Tiny module. Basically, it boils down to Capture::Tiny seemingly requiring module load via a BEGIN block in order to function without error. But I don't understand why, and it chafes me.
Here's code using Capture::Tiny that works:
Output:use strict; use warnings; BEGIN { require Capture::Tiny; } my $out = Capture::Tiny::capture { print "STDOUT output to capture"; } +; print "Captured STDOUT: [" . ( defined $out ? $out : 'undef' ) ."]\n";
Captured STDOUT: [STDOUT output to capture]
By just removing the BEGIN block around require Capture::Tiny, the code now fails:
Output:use strict; use warnings; require Capture::Tiny; my $out = Capture::Tiny::capture { print "STDOUT output to capture"; } +; print "Captured STDOUT: [" . ( defined $out ? $out : 'undef' ) ."]\n";
Can't call method "Capture::Tiny::capture" without a package or object + reference at C:\@local\@projects\@tests\test-Capture-Tiny.pl line 5.
The source for Capture::Tiny (@ http://cpan.uwinnipeg.ca/htdocs/Capture-Tiny/Capture/Tiny.pm.html) seems reasonably straight-forward, but I must be missing something.
Can someone offer an explanation for this behavior?
Thanks, in advance, for the illumination.
In reply to Why does Capture::Tiny require loading via BEGIN to successfully function? by Wyrdweaver
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |