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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
There are many uses for naked blocks, but one common way is for restricting access to lexical variables. Declare them inside the block and they are inaccessible (by name) outside the block. Their values can still exist in memory if there are references to them.
{ my $counter = 0; sub inc_counter { $counter++ }; # $counter is visible here sub get_counter { $counter }; # and here }
The $counter variable has gone out of scope at the end of the block, therefore, you can never directly modify its value explicitly. However, since references to the variable are still made (in the two subroutines), the $counter variable's value still exists. It can only be accessed through these subs, however. But no one code can (maliciously) say $counter = -100, for example. This type of construction is called a closure, BTW.

Naked blocks are also convenient for setting temporary values to globals:

$/ = "\n"; my $line = <STDIN>; # <> operator reads just one line my $whole_file; { local($/) = undef; $whole_file = <F>; # <> operator reads in all of the data from fi +lehandle F } $line = <STDIN>; # reads just one line again, since $/ returns +to "\n"

As for the next keyword, sure it can be used twice or more inside a looping block:

for (0 .. 100) { next unless $_ % 2; # skip multiples of 2 next unless $_ % 5; # skip multiples of 5 print "$_\n"; }
This prints all numbers that aren't divisible by 2 or 5. You can code next many times in your block, but of course only one of them can be followed per iteration. next does not skip you automatically to the 100th iteration of this loop, just the next one. Try it out!

blokhead


In reply to Re: A few random questions from Learning Perl 3 by blokhead
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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (2)
    As of 2020-02-24 01:40 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?










      Results (104 votes). Check out past polls.

      Notices?