>>>>> "BM" == Brandon McCaig <bamcc...@gmail.com> writes:

  BM> 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?
  BM> *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";

  BM> $a and $b are special global variables used by sort. :) See perldoc
  BM> perlvar and/or perldoc -f sort. :) So, in short, $a and $b already
  BM> existed, so there was no problem using them without declaring them
  BM> first. You should ALWAYS declare your variables. :) You either use
  BM> 'my' for a lexical scope or 'our' for module scope. :) There is also

there is no such thing as module scope. our declares package globals and
give them a short name in a lexical scope.

  BM> 'local', but I'm not going to attempt to explain how that works so
  BM> early in the morning. You should be able to learn about each using
  BM> perldoc -f though. To be safe and clear you shouldn't use the names $a
  BM> and $b unless you are actually using them within a sort.

single letter variable names are bad in general. they tell you nothing
about the use and content of the variable. about the only exception are
$i and $j for array/matrix indexing and the aforementioned $a and $b for
scoping.

uri

-- 
Uri Guttman  --  uri AT perlhunter DOT com  ---  http://www.perlhunter.com --
------------  Perl Developer Recruiting and Placement Services  -------------
-----  Perl Code Review, Architecture, Development, Training, Support -------

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to