FAQ

On 2014-01-29 11:17, David Golden wrote:

At what point is the lexical scope established to shadow existing variables?

my $x = 5;
sub foo($x, $y=2*$x) { $x+$y }
foo(1); # returns 3 or returns 11?
I expect the "sub foo (...)" to start a new scope,
like "if (...) {...}".


The other way needs access to @_, or to a new parameter alias array:

    sub foo( $x, $y = 2 * $_[0] ) { $x + $y }

which would be fine with me too, because I don't expect to want to use
it much. (but could hurt @_ optimizations)

Are subroutine definitions possible as defaults?

sub xform($x, $filter=sub ($y) {$y}) { $filter->($x) }
Sure, is just another scalar.

Are closures possible?

sub xform($x, $cb=sub { foo($x) }) { ... }
Yes please. Appears to assume that "sub xform(...) ... {...}" forms a scope.

--
Ruud

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

People

Translate

site design / logo © 2021 Grokbase