Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Avoiding Global Vars

by Melly (Hermit)
on Sep 23, 2009 at 09:08 UTC ( #796913=note: print w/replies, xml ) Need Help??


in reply to Re: Avoiding Global Vars
in thread Avoiding Global Vars

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

Replies are listed 'Best First'.
Re^3: Avoiding Global Vars
by dsheroh (Prior) on Sep 23, 2009 at 14:03 UTC
    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
Re^3: Avoiding Global Vars
by ikegami (Pope) on Sep 23, 2009 at 19:56 UTC
    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
Node Status?
node history
Node Type: note [id://796913]
help
Chatterbox?
[Corion]: My household somewhat becomes a Dell stronghold since I threw out HP for not wanting to scan documents because of an empty ink cartridge ;)
[1nickt]: Wow. I had heard about the chips on HP cartriddges but not that issue. I rejected HP's equivalent laptop becuase of build quality, very plasticky keyboard. And I believe HP is the champon of bloatware, too.

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (14)
As of 2017-03-27 13:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (320 votes). Check out past polls.