Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: Re: A few random questions from Learning Perl 3 by Juerd
in thread A few random questions from Learning Perl 3 by sulfericacid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-25 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found