[comp.sources.d] perl "local" variables

tom@ssd.harris.com (Tom Horsley) (11/22/89)

I ran into a problem on my first ever perl program which I thought might be
worth describing in case it is a bug. I had a subroutine something like
this:

sub getinfo {
   local($user);

   ...

   $_[0] = $user;
}

And a main program something like:

...

   do getinfo($user);

And I could never seem to get the value passed back from the subroutine.
When I changed the local variable in the subroutine from $user to $luser,
everything started working.

I deduce from this that local variables in perl are not really the same as
local variables in C (for instance). What local seems to mean is make all
occurrences of the variable $user throughout the context of the program into
a new variable until this routine returns. The impression I got reading the
description was that it simply made a local vairable for that routine.

This was sufficiently confusing that I thought it was worth posting a note,
just in case someone else was having similar problems.

I don't think of this as an actual bug, it is more like the scoping rules
are not documented well enough. In any event, perl is very close to
infinitely easier to use than awk, so I am not complaining, just commenting.
I once tried to write an awk program, but gave up after I pulled out my last
hair. In contrast my first perl program took about two hours and it does all
sorts of analysis on the raw output from several benchmark programs I have.

(P.S. This is the latest and greatest perl 3.0 patchlevel 6 I am running).
--
=====================================================================
domain: tahorsley@ssd.csd.harris.com  USMail: Tom Horsley
  uucp: ...!novavax!hcx1!tahorsley            511 Kingbird Circle
      or  ...!uunet!hcx1!tahorsley            Delray Beach, FL  33444
======================== Aging: Just say no! ========================