Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Why does Capture::Tiny require loading via BEGIN to successfully function?

by Anonymous Monk
on Nov 10, 2010 at 00:44 UTC ( [id://870447]=note: print w/replies, xml ) Need Help??


in reply to Why does Capture::Tiny require loading via BEGIN to successfully function?

I disagree that this module's code is straightforward. I couldn't actually find the capture sub in that module at a glance -- the author redefines *_debug at run time, for instance, which hints at the possibility for obfuscation-like complexity).

However, what appears to be happening is that it's doing something to change parsing rules at compile time. This can be done with a function prototype, like sub foo (&) {}, which is how you create functions that act like builtins such as map and grep.

What this means is that when you require it at run time, that compile-time code isn't executed, so the magic of capture BLOCK isn't applied. Instead, you need to use capture( CODEREF ).

Look at this:

# perl -lwE "use Capture::Tiny (); say 'captured: ', Capture::Tiny::ca +pture { say 'foo' }" captured: foo
# perl -lwE "require Capture::Tiny; say 'captured: ', Capture::Tiny::c +apture { say 'foo' }" foo Can't call method "Capture::Tiny::capture" without a package or object + reference at -e line 1.
# perl -lwE "require Capture::Tiny; say 'captured: ', Capture::Tiny::c +apture(sub { say 'foo' })" captured: foo

Upshot: without the compile-time-added syntax sugar, you need to call this sub like capture($coderef);.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-24 09:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found