Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

All code blocks are not created equal: HINT_BLOCK_SCOPE

by chip (Curate)
on Dec 11, 2001 at 02:44 UTC ( [id://130803]=note: print w/replies, xml ) Need Help??


in reply to Potential Ternary Operator Bug

The difference you're seeing is due to the complexity (or lack thereof) of the expression in the @{}.

Simple bits of code that can't possibly have local state to unwind or exception handling to perform will work in your test case. That's why the modulo test is OK. (Mind you, its working is an undocumented and unreliable accident, but this being the Monastery I'm not stopping with RTFM.)

More complex bits of code that may have local state to be unwound trigger Perl to make copies of their results when they exit. Classic examples of such expressions are user subroutines and the do BLOCK construct, but they can also arise in other circumstances. Since pattern matches can do really weird and complex things, putting a regex in your array reference expression forces the final result of the expression to be copied ... and that's what leads to the "bizarre copy" message.

The fact that Perl makes this distinction is the basis of an important optimization: Introducing what looks like a code block doesn't necessarily impose the overhead of a code block. The overhead is imposed only if something that looks dangerous or complex appears inside the given block.

For further info on this phenomenon, grep the Perl source code for the constant HINT_BLOCK_SCOPE.

PS: The fact that do BLOCK forces copies is exactly the reason why do { local *FH } makes an anonymous glob value every time it's evaluated.

    -- Chip Salzenberg, Free-Floating Agent of Chaos

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-23 10:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found