[Pdx-pm] New DA question - sub return'd values

Ovid curtis_ovid_poe at yahoo.com
Thu Jan 8 12:33:22 CST 2004


--- "Roderick A. Anderson" <raanders at acm.org> wrote:
> On Thu, 8 Jan 2004, Ovid wrote:
> 
> Got all the above.  And basically understood it already.

I hope I didn't sound too pedantic.  I often don't know the skill level
of the person I am responding to and I try to be cognizant of the fact
that others might be reading.
 
> > I might, however, add a "local" statement to the assignment:
> > 
> >   local $_ = typeofcust($username, $password);
> 
> This is OK inside of the main context?

I assume you're asking if this is OK inside of the main namespace? 
Well, that depends.  I can't think of any situations  where "local $_"
can hurt you, but I can think of plenty of ways where leaving off the
"local" will make life miserable.

Also, consider this:

  package main;

  while (<>) {
    zero()
    if (/foo/) {
      # will never execute
    }
  }

  sub zero {
    # do stuff
    $_ = 0;
    print "Life now becomes unpleasant";
  }

In that example, we're still in the main namespace (which I think was
your question), but we've stomped on $_ in &main::zero and the regex
match in the while loop will never succeed.  From your email, it looks
like you understand this issue, but just in case, I thought I would
clarify that.

The reason why that simplistic example is important to me is because I
often build small utility scripts and as they grow, I pull out large
chunks into subroutines.  If I've already predeclared any use of $_ as
local, I don't have to worry about going back and fixing it when the
code gets moved.

Cheers,
Ovid

=====
Silence is Evil            http://users.easystreet.com/ovid/philosophy/indexdecency.htm
Ovid                       http://www.perlmonks.org/index.pl?node_id=17000
Web Programming with Perl  http://users.easystreet.com/ovid/cgi_course/



More information about the Pdx-pm-list mailing list