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

Re^2: Pointers and References

by Bod (Chaplain)
on Nov 23, 2020 at 18:22 UTC ( #11124070=note: print w/replies, xml ) Need Help??


in reply to Re: Pointers and References
in thread Pointers and References

Really helpful explanation GrandFather

In your example code, what is achieved by wrapping part of the code in a do block?
Is this purely to aid legibility or does it have a functional impact? The documentaion doesn't answer this query.

Replies are listed 'Best First'.
Re^3: Pointers and References
by GrandFather (Saint) on Nov 23, 2020 at 19:57 UTC

    Anonymous Monk is quite right. The do block is there to wrap up a "main". I don't usually bother, but in this case a previous iteration of the sub had variables with the same name as were in the main block of code so I used the do block to limit the scope of those variables.

    As a general thing avoiding global variables is good. In larger scripts I write a formal sub main to ensure the only global variables are deliberate and explicit. So the do block is a light weight version of that practice that remained somewhat beyond its use by date, but a useful discussion point as it turns out.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
      > I used the do block to limit the scope of those variables.

      maybe consider a "naked BLOCK" instead of do BLOCK to limit the scope.

      { my $limited_in_scope; }

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        Sure. For me naked blocks in C++ and similar languages are almost universally scope limiting blocks for process locking of various sorts. I tend to not use them in other contexts and that bleeds through to Perl. And again, for trivial code like this I wouldn't usually bother, but I was "belt and braces" checking the example code in an earlier iteration and forgot to remove the block.

        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

      Where I want to explicitly show that a variable is deliberately global, I declare it with our.

        I almost never use our. Usually when I say global I mean global to the file and I might prefix the variable name with a g. our tells you something is "global" where you declare the variable, but it doesn't tell you that where you use it. You tend to use a variable much more often than you declare it so the ugliness of a g prefix is an immediate and obvious reminder at the point of use that this is a global variable.

        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

        I use a simple convention: local variables have names in all_lowercase, global variables have names with an Initial_capital, and constants or quasi-constants have names that are ALL_UPPERCASE.

Re^3: Pointers and References
by Anonymous Monk on Nov 23, 2020 at 18:54 UTC

    In this case, only the latter. He is doing it as a personal practice for designating the "main routine" of his program.

    do does have definite functional uses. None of them are being leveraged here.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2021-06-13 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (54 votes). Check out past polls.

    Notices?