|
Revision 14595, 1.2 kB
(checked in by takesako, 5 years ago)
|
|
fixed up for shibuya.pm#9 XS Nite
|
| Line | |
|---|
| 1 | package arguments; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | |
|---|
| 6 | our $VERSION = '0.03'; |
|---|
| 7 | |
|---|
| 8 | use Exporter; |
|---|
| 9 | our @ISA = qw(Exporter); |
|---|
| 10 | our @EXPORT = qw(arguments callee); |
|---|
| 11 | |
|---|
| 12 | sub arguments { |
|---|
| 13 | { package DB; () = caller(1); } |
|---|
| 14 | return wantarray |
|---|
| 15 | ? @DB::args |
|---|
| 16 | : bless \@DB::args, 'arguments'; |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | sub length { |
|---|
| 20 | { package DB; () = caller(1); } |
|---|
| 21 | return scalar @DB::args; |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | require XSLoader; |
|---|
| 25 | XSLoader::load('arguments', $VERSION); |
|---|
| 26 | |
|---|
| 27 | sub callee { |
|---|
| 28 | my $cx = upcontext(1); |
|---|
| 29 | return sub {} unless $cx; |
|---|
| 30 | return $cx; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | 1; |
|---|
| 34 | __END__ |
|---|
| 35 | |
|---|
| 36 | =head1 NAME |
|---|
| 37 | |
|---|
| 38 | arguments.xs - Perl extension for arguments::callee |
|---|
| 39 | |
|---|
| 40 | =head1 SYNOPSIS |
|---|
| 41 | |
|---|
| 42 | use arguments; |
|---|
| 43 | |
|---|
| 44 | print sub { |
|---|
| 45 | my $x = shift; |
|---|
| 46 | return 1 if ($x <= 1); |
|---|
| 47 | return $x * arguments->callee->($x - 1); |
|---|
| 48 | }->(5); # 120 |
|---|
| 49 | |
|---|
| 50 | sub { |
|---|
| 51 | my $c = shift; |
|---|
| 52 | print "$c\n"; |
|---|
| 53 | arguments->callee->($c) if ($c--); |
|---|
| 54 | }->(10); |
|---|
| 55 | |
|---|
| 56 | =head1 DESCRIPTION |
|---|
| 57 | |
|---|
| 58 | inspired by arguments.callee from ECMAScript. |
|---|
| 59 | |
|---|
| 60 | see also. |
|---|
| 61 | http://d.hatena.ne.jp/amachang/20080501/1209623634 |
|---|
| 62 | http://d.hatena.ne.jp/amachang/20080501/1209640306 |
|---|
| 63 | |
|---|
| 64 | =head1 SEE ALSO |
|---|
| 65 | |
|---|
| 66 | Devel::Caller, PadWalker |
|---|
| 67 | |
|---|
| 68 | =head1 LICENSE |
|---|
| 69 | |
|---|
| 70 | This library is free software; you can redistribute it and/or modify |
|---|
| 71 | it under the same terms as Perl itself. |
|---|
| 72 | |
|---|
| 73 | =cut |
|---|