Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
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


Comment on Re^2: Avoiding Global Vars
Download Code
Re^3: Avoiding Global Vars
by dsheroh (Parson) 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?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (16)
As of 2014-12-18 16:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (58 votes), past polls