> Jonathan,
> 
> Thank you very much for your response!  It looks alot
> tidier now :)  But for some reason when I run the
> script I get the following...
> 
> %popusers = ();
> 
> Ie %popusers is not being populated at all!  I have
> done nothing to your code I simply cut and pasted it
> to test it but for some reason it's not working at all
> now?  There is also no compilation errors either.
> 
> Regards,
> 
> Dan

Try changing:

  view_users();

to:

  my %popusers = view_users();

as you should notice that %popusers only exists in the
subroutine.  Your version created the hash without defining
it, so it became a global variable that you could access
anywhere... my version was more careful about doing that.

In programming you need to consider something called
"variable scoping".  Lets explain with an example shall we?
 Here are some people we'll pretend you know in your real
life (apologies to the owners of the names I've nicked ;)

At Work:
  Alan Cox
  Larry Wall
  Richard Stallman

At local pub:
  Simon Cozens
  Michael G Schwern  
  Piers Cawley
  Alan Penman (some guy that might exist, or maybe he is
the heavy boozer at the bar)

Now, here comes the real imagination bit...

Alan comes along to your desk, and asks if you can
integrate a linux kernel driver by Monday.  Can you guess
if I'm talking about Alan Cox or Alan Penman?  (The answer
is Alan Cox BTW)  Which Alan we are talk about depends on
the context (or scope) of where we are.  Variable scoping
is like this, we say "I'm at work" and then every time we
use a variable we only look in that area - not outside.  If
we move to the pub, and talk about Alan we mean someone
completely different.

An code example looks like:

sub work {
    my $alan = "Cox";
    # Talk about Alan Cox
} 

sub pub {
    my $alan = "Penman";
    # Talk about Alan Penman
}

So, we go into work with

work();

and whenever we are at work (in the work subroutine) we
talk about Alan Cox.  Got that yet?  This is an example of
lexical scoping, in that we only talk about either of them
IN A SUBROUTINE.  A lexical scoped variable is also known
as a local variable (but don't get that confused with my
pub!)

Now, lets talk about yourself (most people like this bit!),
and where you feature in this example.  Do you exist only
at work, or in the pub?  No, you don't.  You are what is
known as globally scoped, in that you appear EVERYWHERE. 
Thats great huh?  Except, if someone says something nasty
(or hopefully a nice thing) about you at work, you are
going to still remember it when you get to the pub, aren't
you?  That's not so good.

An code example looks like:

sub day {
  work();
  pub();
}

sub work {
  $nasty_comment = '$%&^$"!£$';
}

sub pub {
  remember($nasty_comment);
}

I hope my version of variable scoping is more comical than
you'll find elsewhere, as well as making more sense (?). 
Anyway, you should use lexically scoped variables as much
as possible.

---

Now to come to the second related issue, which is to do
with the way I'm returning that value.  The code is
structured somewhat like:

sub function {
    my %hash = { A     => very,
                 large => hash
               };

    return %hash;
}

Unfortunately, because I've told it to return a hash it
will return a hash, slowly.  Perl will make a complete copy
in memory of the version to return, and then throw away the
original - which was identical.  To make things run a
little faster we change the code to:

sub function {
    my %hash = { A     => very,
                 large => hash
               };

    return \%hash;
}

Notice the difference?  The \ before %hash means to take a
reference to, rather like taking the phone number of the
person you've met (rather than bringing them home).

Now, to call the function do:

my %hash = %{ function() };

Where the %{ } says to dereference the reference... giving
you the original data.  However, if its like a phone number
you always get the same variable (or "person" :).

References take time to get used to, but are useful when
used SPARINGLY.  Apply to time critical things, unless you
like looking at all those extra symbols.

---

Jonathan Paton

---

PS: Sorry if I've just made this so confusing, it is fairly
natural once you know how.  Most books on Perl will cover
lexical scoping and references, and probably have nice
pictures to make it easier.



__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to