Re: I find the perl syntax easier than python



On 29 Apr 2007 22:08:41 -0700,
grocery_stocker <cdalten@xxxxxxxxx> wrote:

Versatility isn't always a good thing. Sometimes this can lead to
stuff being inconsistent. Consider the following code I duped from a
Google search -).
#!/usr/bin/perl -w

sub alter ($) {
$_[0]= $_[0] . " blub";

}

sub alter ($) {
my $string = shift;
$string = $string . " blub";
}

I wouldn't call that inconsistent. I can understand that some people
find it unexpected -- and I could come up with other description for the
behaviour -- but it's not inconsistent for the following two reasons:

1 - The code is different, and alters different variables, so there is
no consistency to violate. The code is not consistent, so there is
no need for perl to behave in an identical, or even similar, way.

2 - It's documented to work this way (in perlsub).

The @_ array is 'magical' in subroutines, and its elements are aliased
to the scalar parameters to the subroutine call.

In the second subroutine, you create a lexically scoped local variable,
which you initialise by copying the contents of the first element of the
@_ array.


Note that this sort of aliasing behaviour occurs in only a few places,
and is documented that way. If you get surprised by it, get in the habit
never to modify variables unless you know they're local, or you intend
to modify a global variable.

Whether it's elegant/desirable for @_ to be aliased this way is a
separate discussion. For now, we're stuck with it.

Martien
--
|
Martien Verbruggen | Freudian slip: when you say one thing but
| mean your mother.
|
.