Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I often create blocks with my-scoped variables to hide my helper variables. Schematically,
my $BigCalc; { my $SmallCalc; .... $BigCalc = f($SmallCalc); } ... DoSomething($BigCalc);
However, I don't really like the leading open brace "{" -- it looks naked to me. What approaches do Monks find best/most readable when wanting to create some scope? Here's some possibilities:
#!/use/bin/perl use warnings; use strict; { my $x = 1; print "small calc $x\n"; } BLOCK: { my $x = 2; print "small calc $x\n"; } do { my $x = 3; print "small calc $x\n"; }; if (1) { my $x = 4; print "small calc $x\n"; }

Replies are listed 'Best First'.
Re: Annotation of Anonymous Blocks
by moritz (Cardinal) on Sep 30, 2011 at 13:04 UTC
    I don't think there's anything wrong with a naked opening brace. If I had to chose, I'd certainly go with do { }, because it expresses the intent more clearly than the others.
      but do requires the extra semicolon! (And a lone do looks alien to C coders.)
Re: Annotation of Anonymous Blocks
by roboticus (Chancellor) on Sep 30, 2011 at 13:13 UTC

    I prefer the first: It shows that you're making a scope for $x and groups the code. I don't care for the others because there's a little extra mental overhead:

    • With the BLOCK: version, I start looking for next or goto elsewhere. When I don't see it, I wonder why the label is there.
    • The do { ... }; version is the least objectionable, but I do have to look at the end of the loop to see if there's a looping condition, etc.
    • The if (1) version is particularly ugly to me. It looks like something is (potentially) optional, that someone hardcoded for the moment, and I wonder in what circumstances you would use a condition.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

    Update: Added code tags and changed "I wonder what the condition is where you'd put in a condition" to "I wonder in what circumstances you would use a condition" for clarity. (Thanks, AM)

      what condition my condition is in?
Re: Annotation of Anonymous Blocks
by BrowserUk (Patriarch) on Sep 30, 2011 at 13:17 UTC

    In your initial example, I could see some merit in doing:

    my $BigCalc = do { my $SmallCalc; .... $smallCalc; };

    But for the most part, the only occasions I've felt the need to use bare-blocks are:

    • To limit the scope of locks on shared vars:
      { lock $fred; $fred = 3; }
    • For limiting the scope of closures:
      { my $var; sub fred { if( $var++ ) { ...; } ... } }

      This use has mostly been superceded by state variables.

    In neither case have I felt the need to introduce the opening brace in any way. Indeed, I think to do so would be more rather than less likely confusing; potentially flattering to deceive that the use of the construct was more important than it is.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Indeed, the post was motivated by "limiting the scope of closures".
Re: Annotation of Anonymous Blocks
by Anonymous Monk on Sep 30, 2011 at 22:13 UTC

    A bare block is perfectly clear. It creates a limiting scope.

    The BLOCK: block almost implies a loop, which it is, but that is not the intent. On the other hand, a more descriptive label like SCOPING_BLOCK: may work.

    The do block by itself seems to be begging for while modifier. Assigning to a variable helps:

    my $BigCalc = do { my $SmallCalc; ... f($SmallCalc); };

    The if block looks like debugging code. Someone commented out a chunk of code with if (0) {...} then turned it back on.

Re: Annotation of Anonymous Blocks
by Perlbotics (Archbishop) on Sep 30, 2011 at 21:19 UTC

    I prefer the classic™ #-annotation style ;-) Something along

    { #------------------- classic annotation style ------------ my $x = 5; print "small calc $x\n"; } #-----------------------------------------------------------
    just giving some kind of visual hint. Many editors allow to fold a block just displaying a comment instead (i.e. Emacs folding-mode) which might be helpful too.