"David McNerney" <> wrote in
news: ups.com:
> sub function1
> {
> # PROBLEM HERE: if $_[0] is false here on successive invocations,
> # @list is not cleared on the 2nd and later cycles, and any values
> # added to it build up.
> #
> my @list = ("a") if $_[0];
If you had written this as:
if ( $_[0] ) {
my @list = ("a");
}
the problem with your code would have been clearer. Basically, putting
the declaration and initialization of @list in a compound if statement
causes ambiguity.
I am not sure why no warnings are emitted. I do remember a section in
the docs that deals with this but I am not able to locate it right now.
>
> # Uncomment this, and comment out the one above, and of course the
> # strange behavior is gone
> #
> #my @list;
>
> push(@list,"b") if $_[1];
> print "list was: ".join(" ", @list)."\n";
> }
Rewrite this sub as:
sub function1 {
my @list;
@list = qw( a ) if $_[0];
push @list, "b" if $_[1];
print "list was: @list\n";
}
> &function1(1,1); # should print "a b", and does
Don't use the ampersand in function invocations unless you know what it
does, and you specifically need that effect.
See perldoc perlsub
Sinan
--
A. Sinan Unur <>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc...uidelines.html