Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Should this work or am I relying on any unspecified behavior? I ask because I was surpised that strict did not throw an error when using the dynamically created functions even though they are lexically before the call that creates them.
a: 1 b: 6#implements something like "do" notation for the Identity monad. #bound variables become subs in the main namespace #add_var thunks the subs to get laziness with sharing sub add_var { my ($name, $fn) = @_; *{"main::$name"} = sub{ my $ret = $fn->(@_); *{"main::$name"}= sub{$ret}; return $ret }; return 1; } use strict; sub mdo { while (scalar @_ > 1) { my $name = shift; my $fn = shift; add_var($name,$fn); } my $fn = shift; return $fn->(); } my $f = sub { print "a: ", a(), " b: ", b(), "\n";}; mdo( a => sub{1} , b => sub{ a() + 5} , $f ); mdo( a => sub{2} , b => sub{ a() * 5} , $f );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: strict, typeglobs, and evaluation order
by tobyink (Canon) on Oct 11, 2013 at 07:35 UTC | |
|
Re: strict, typeglobs, and evaluation order
by choroba (Cardinal) on Oct 11, 2013 at 07:40 UTC | |
|
Re: strict, typeglobs, and evaluation order (strict on top)
by Anonymous Monk on Oct 11, 2013 at 07:32 UTC |