Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Re: A few random questions from Learning Perl 3

by Juerd (Abbot)
on Jan 06, 2003 at 12:42 UTC ( #224610=note: print w/replies, xml ) Need Help??

in reply to Re: A few random questions from Learning Perl 3
in thread A few random questions from Learning Perl 3

1)Naked Blocks can be used to turn off warnings for the scope of the block - useful if you're doing some work that will encounter a lot of "uninitialized variable" warnings.

Blocks are used to create scope. What you do with that scope is up to you.

This kind of scope is 'lexical'. You can do a lot with it:

  • Use a lexical pragma (like strict, warnings or utf8)
  • Use a lexical variable (declared with my)
  • Declare a global variable lexically (with our)
  • Temporarily give a package global another value (using local)
Each of these use the lexical scope the block creates.

You don't create a block to turn off warnings. You use no warnings to turn them off. The block only limits the effect of that statement. It's important to know that the block itself has nothing to do with warnings (well, you can get warnings regarding the block of course).

This lexical stuff goes for all code blocks, bare or belonging to something like if or while.

Bare blocks (in this thread called 'naked blocks'; haven't seen them called that anywhere else) act like loops. In the code blocks for while, until and for, you can the use loop controlling operators next, redo and last. These can also be used with bare blocks: redo goes to the beginning of the block, last goes to the end.

You cannot use loop control operators with non-loop constructs like do, if and eval.

if (...) { ...; last if ...; # cannot use C<last> with G<if> ...; }
You can put a bare block in if's block to create the loop you want:
if (...) { { ...; last if ...; # can use C<last> with a bare block ...; } }
Bare blocks are used like this everywhere, but often disguised: double curlies are used to make the code look nicer.
if (...) {{ ...; last if ...; # same ...; }}
So if you see doubled curly brackets, the extra block is probably only there to make breaking out of it easy.

- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2020-02-25 18:47 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (113 votes). Check out past polls.