in reply to Avoiding Global Vars

Your specific example doesn't reference $vrx outside the scope of the while loop, so there's no need to declare it globally - hence there's nothing to avoid ... esp. if the variable is declared inside the loop.

That being said, I'm a big fan of 'horses for courses' so, if a global is needed, then by all means use one and as recommended in PBP, ensure the name is capitalized.

Problems arise thro' the over extensive/unthinking use of globals.

A user level that continues to overstate my experience :-))

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

    But if I only declare $vrx within the while loop, then I will lose its value when I need to print it...

    Try it with the data I supplied, and look at the different output...

    1001foo 1002bar 1003hello 2001bish 2002bash

    versus

    1foo 2bar 3hello 1bish 2bash

    So, it's not a question of whether I reference it outside the loop - rather the opposite, since if I did reference it elsewhere, then it would be global, and it wouldn't bug me... if you see what I mean.

    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