[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] aCertainName.pm



On Wed, Nov 10, 1999 at 02:15:05PM -0800, Larry Rosler wrote:
> > From: Ronald J Kimball [mailto:rjk@linguist.dartmouth.edu]
> > Sent: Wednesday, November 10, 1999 13:44
> > To: Larry Rosler
> > Cc: 'Steve Lane'; fwp@technofile.org
> > Subject: Re: [FWP] aCertainName.pm
> >
> > On Wed, Nov 10, 1999 at 01:23:40PM -0800, Larry Rosler wrote:
> > > > From: Steve Lane [mailto:sml@zfx.com]
> > > >
> > > > wouldn't the functions that take either a scalar
> > > > or list argument be easier to write using:
> > > >
> > > >   @_ = ($_) unless @_;
> > > >
> > > > instead?
> > >
> > > Easier, perhaps.  Correct, no!
> > >
> > > We're trying to reproduce the semantics of abs(), which say nothing
> > > about clobbering @_ if there is not argument.
> >
> > If there is no argument, then there is nothing in @_ to clobber.
> >
> > Correct, yes!
> 
> Correct, no!
> 
> my foo {
>    my $x = abs; # Sets @_ to $_.
>    &bar; # Expects the same arguments as foo(), namely none, but now
> gets $_.
> }
> 
> @_ has been 'clobbered' by abs()!
> 

Correct, yes!

@_ is always localized to the current subroutine.  How could it be
otherwise, considering that all subroutine calls modify @_?

sub foo {
  bar('a');
  # Uh oh, has foo()'s @_ been changed by the call to bar()?
  # Fortunately, no!
}


You might want to actually try it, before putting your foot in your mouth a
third time.  ;)


sub abs {
    @_ = ($_) unless @_;
    return $_[0] >= 0 ? $_[0] : -$_[0];
}

sub foo {
   my $x = abs; # Sets @_ to $_.
   &bar; 
}

sub bar {
    print '@_ has ', scalar @_, " elements.\n";
}

$_ = 'abc';

foo();


Ronald


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe