Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Avoiding Global Vars

by Porculus (Hermit)
on Sep 22, 2009 at 22:24 UTC ( [id://796832]=note: print w/replies, xml ) Need Help??


in reply to Avoiding Global Vars

<java>Simple: use a singleton! Then you can bask in the pleasure of knowing that your code is Object-Oriented, and uses a Genuine GoF Design Pattern, and contains a bunch of unnecessary complexity that does absolutely nothing to improve encapsulation and just makes it more fragile to maintain.</java>

Seriously, worrying about global variables is not the Perl way. If you want to improve your code, don't do it by running down a checklist of "bad" constructs -- do it by reading the code itself and asking which bits could be easier to understand. It is different for every bit of code. I've certainly seen programs rendered treacherous by the abuse of global state, but they were nothing to some of the spaghetti nightmares I've encountered in languages that don't even have global variables.

Replies are listed 'Best First'.
Re^2: Avoiding Global Vars
by Melly (Chaplain) on Sep 23, 2009 at 09:08 UTC

    Thanks all - and apols for the wrong terminology (i.e. global <> "my", just because "my" outside of a block is lexical to the whole file)

    One point to clarify is that the reason it bugs me, apart from wanting vars to only live within the scope where they are needed, is that I might re-use the name $vrx later, with possible side-effects.

    bv's suggestion (use a bare-block) seems the most economical and least confusing, but I will also bear in mind that I shouldn't sweat the details....

    map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
    Tom Melly, pm (at) cursingmaggot (stop) co (stop) uk
      One point to clarify is that the reason it bugs me, apart from wanting vars to only live within the scope where they are needed, is that I might re-use the name $vrx later, with possible side-effects.

      If you use warnings, you'll get a warning if you redefine a new my $vrx in the same scope as the first. If it's not in the same scope, then it'll be a different $vrx, so there should be little to no chance of unintended side-effects.

      Of course, the better way to deal with it would be to use a more meaningful/distinctive name so that it's unlikely that you'll have cause to re-use the name...

        Ah, but I might (stupidly) do something like:

        my $urx = 0; # no warning on mistyped $vrx
        print $vrx; # get's old $vrx
        

        As for more meaningful/distinctive, vrx, for reasons relating to the specific tech I'm working with, is very meaningful...

        Thanks again.

        map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
        Tom Melly, pm (at) cursingmaggot (stop) co (stop) uk
      I find that if I want to use a bare block, it's either unnecessary or I should be using a new sub instead.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-20 02:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found