| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use v5.14; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Test2::V0; |
| 7 |
|
| 8 |
use lib "t"; |
| 9 |
use testcase "t::func"; |
| 10 |
|
| 11 |
BEGIN { $^H{"t::func/func"}++ } |
| 12 |
|
| 13 |
# named func |
| 14 |
{ |
| 15 |
func example { return 123; } |
| 16 |
|
| 17 |
is( example(), 123, 'named func' ); |
| 18 |
} |
| 19 |
|
| 20 |
# anon func |
| 21 |
{ |
| 22 |
my $ex2 = func { return 456; }; |
| 23 |
|
| 24 |
is( $ex2->(), 456, 'anon func' ); |
| 25 |
} |
| 26 |
|
| 27 |
# func still obtains :ATTRS |
| 28 |
{ |
| 29 |
my $modify_invoked; |
| 30 |
|
| 31 |
sub MODIFY_CODE_ATTRIBUTES |
| 32 |
{ |
| 33 |
my ( $pkg, $sub, $attr ) = @_; |
| 34 |
$modify_invoked++; |
| 35 |
::is( $attr, "MyCustomAttribute(value here)", |
| 36 |
'MODIFY_CODE_ATTRIBUTES takes attribute' ); |
| 37 |
|
| 38 |
return (); |
| 39 |
} |
| 40 |
|
| 41 |
func withattr :MyCustomAttribute(value here) { } |
| 42 |
func attrnospace:MyCustomAttribute(value here) { } |
| 43 |
|
| 44 |
is( $modify_invoked, 2, 'MODIFY_CODE_ATTRIBUTES invoked' ); |
| 45 |
} |
| 46 |
|
| 47 |
# named func in another package |
| 48 |
{ |
| 49 |
func Some::Other::Package::example { return 456; } |
| 50 |
|
| 51 |
is( Some::Other::Package->example, 456, 'named func in another package' ); |
| 52 |
|
| 53 |
my $e = defined eval 'nopkgfunc Some::Other::Package::example2 { }; 1' ? undef : $@; |
| 54 |
like( $e, qr/^Declaring this sub-like function in another package is not permitted /, |
| 55 |
'nopkgfunc does not permit other package name' ); |
| 56 |
} |
| 57 |
|
| 58 |
done_testing; |