From: Vincent Pit Date: Fri, 8 Jan 2010 18:52:20 +0000 (+0100) Subject: Test and document caveats about defining a sub whose name is handled in the current... X-Git-Tag: v0.02~2 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=e7ec7b5660437ed920a2d671f933d8db331e27d0;p=perl%2Fmodules%2FSub-Op.git Test and document caveats about defining a sub whose name is handled in the current scope --- diff --git a/lib/Sub/Op.pm b/lib/Sub/Op.pm index 6b421c4..e44ebca 100644 --- a/lib/Sub/Op.pm +++ b/lib/Sub/Op.pm @@ -435,6 +435,11 @@ BEGIN { _monkeypatch() } See the F directory that implements a complete example. +=head1 CAVEATS + +Preexistent definitions of a sub whose name is handled by L are restored at the end of the lexical scope in which the module is used. +But if you define a sub in the scope of action of L with a name that is currently being replaced, the new declaration will be obliterated at the scope end. + =head1 DEPENDENCIES L 5.10. diff --git a/t/11-existing.t b/t/11-existing.t index 2e6c6ca..6c2e9ba 100644 --- a/t/11-existing.t +++ b/t/11-existing.t @@ -5,7 +5,7 @@ use warnings; use blib 't/Sub-Op-LexicalSub'; -use Test::More tests => 2 * ((2 + 2) * 4 + (1 + 2) * 5) + (2 + 2) + 4; +use Test::More tests => 2 * ((2 + 2) * 4 + (1 + 2) * 5) + 2 * (2 + 2) + 4; our $call_foo; sub foo { ok $call_foo, 'the preexistent foo was called' } @@ -15,6 +15,9 @@ sub bar () { ok $call_bar, 'the preexistent bar was called' } sub X () { 1 } +our $call_blech; +sub blech { ok $call_blech, 'initial blech was called' }; + our $called; { @@ -59,7 +62,7 @@ our $called; } $test .= "{\n$code\n}\n"; $test .= "}\n"; - for my $name (grep +{ map +($_, 1), qw/foo bar/ }->{ $_ }, @names) { + for my $name (grep +{ map +($_, 1), qw/foo bar blech/ }->{ $_ }, @names) { $test .= <<" CHECK_SUB" { local \$call_$name = 1; @@ -179,3 +182,11 @@ bar # () # is X, 2, 'constant overriding'; ---- X # 2 # [ ] +#### +no warnings 'redefine'; +sub blech { fail 'redefined blech was called' } +BEGIN { $call_blech = 0 } +blech 7; +BEGIN { $call_blech = 1 } +---- +blech # () # [ 7 ]