ChrisDennis has asked for the wisdom of the Perl Monks concerning the following question:
This is my first question here, so please don't laugh.
Can anyone explain to me what's going on in this code?
which produces the following output on my Linux system with Perl 5.10.1:#!/usr/bin/perl use strict; use warnings; sub foo ($) { my $x = shift; print "foo gets '$x'\n"; return ($x+2, 33); } my @a = foo(42); print "a=@a\n"; @a = sort(foo(42)); print "a=@a\n"; @a = (sort foo(42)); print "a=@a\n"; @a = sort foo(42); print "a=@a\n";
foo gets '42' a=44 33 foo gets '42' a=33 44 a=42 a=42
If I use old-style subs (i.e. &foo instead of foo), then it produces 'a=33 44' three times, which is what I'd expect.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: New-style sub strangeness
by Corion (Patriarch) on Jun 07, 2011 at 15:42 UTC | |
by ChrisDennis (Sexton) on Jun 07, 2011 at 16:43 UTC | |
|
Re: New-style sub strangeness
by choroba (Cardinal) on Jun 07, 2011 at 15:38 UTC | |
|
Re: New-style sub strangeness
by wind (Priest) on Jun 07, 2011 at 15:40 UTC |