On Sun, Sep 4, 2011 at 11:53 PM, flebber <flebber.c...@gmail.com> wrote: > For this example. the first two assignments work okay, but then at $c > if I do not declare my local then I get an error. $d also requires > that it be assigned using my, though it is directly the same as $b, my > assumption here is that $d required explicit assignment as it used the > explicitly assigned variable $c (could be wrong). > > Why from $c was "my" required? *snip* > $a = 6*9; > print "six times 9 is ", $a, "\n"; > $b = $a + 3; > print "plus three is ", $b,"\n"; > my $c = $b/3; > print "Divided by 3 is ", $c, "\n"; > my $d = $c + 1; > print "plus one is ", $d, "\n";
$a and $b are special global variables used by sort. :) See perldoc perlvar and/or perldoc -f sort. :) So, in short, $a and $b already existed, so there was no problem using them without declaring them first. You should ALWAYS declare your variables. :) You either use 'my' for a lexical scope or 'our' for module scope. :) There is also 'local', but I'm not going to attempt to explain how that works so early in the morning. You should be able to learn about each using perldoc -f though. To be safe and clear you shouldn't use the names $a and $b unless you are actually using them within a sort. -- Brandon McCaig <http://www.bamccaig.com/> <bamcc...@gmail.com> V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl. Castopulence Software <http://www.castopulence.org/> <bamcc...@castopulence.org> -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/