Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: A few random questions from Learning Perl 3

by davis (Vicar)
on Jan 06, 2003 at 09:49 UTC ( #224588=note: print w/replies, xml ) Need Help??


in reply to A few random questions from Learning Perl 3

Because nobody's said it already:

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.

#Some code up here, with warnings on. { no warnings; print $foo{$bar}; #Causes lots of "uninitialized variable" warnings, b +ut in this case it's ok to ignore them. }
Wether or not you should do this is up to you.
Woo. 100th post. Verily, feh.
davis
Is this going out live?
No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist

Replies are listed 'Best First'.
Re: Re: A few random questions from Learning Perl 3
by Juerd (Abbot) on Jan 06, 2003 at 12:42 UTC

    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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2020-02-17 12:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (71 votes). Check out past polls.

    Notices?