YAMASHINA Hio
hio****@users*****
2006年 10月 26日 (木) 01:25:27 JST
Index: docs/modules/autobox-1.03/autobox.pod diff -u /dev/null docs/modules/autobox-1.03/autobox.pod:1.1 --- /dev/null Thu Oct 26 01:25:27 2006 +++ docs/modules/autobox-1.03/autobox.pod Thu Oct 26 01:25:26 2006 @@ -0,0 +1,610 @@ +=encoding utf8 + +=head1 åå + +autobox - çµã¿è¾¼ã¿åããã¡ã¼ã¹ãã¯ã©ã¹ãªãã¸ã§ã¯ãã¨ãã¦å©ç¨ + +=head1 æ¦è¦ + + use autobox; + + # call methods on builtin values and literals + # çµã¿è¾¼ã¿ã®å¤ããªãã©ã«ããã¡ã½ããå¼ã³åºã + + # integers + # æ´æ° + + my $range = 10->to(1); # [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ] + + # floats + # æµ®åå°æ°ç¹æ° + + my $error = 3.1415927->minus(22/7)->abs(); + + # strings + # æåå + + my $uri = 'www.%s.com/foo.pl?arg=%s'->f($domain, $arg->escape()); + my $links = 'autobox'->google(); + + my $word = 'rubicund'; + my $definition = $word->lookup_on_dictionary_dot_com(); + + my $greeting = "Hello, World"->upper(); # "HELLO, WORLD" + + $greeting->to_lower(); # greeting is now "hello, world" + $greeting->for_each(\&character_handler); + + # ARRAY refs + # é åãªãã¡ã¬ã³ã¹ + + my $schwartzian = [ @_ ]->map(...)->sort(...)->map(...); + my $sd = [ 1, 8, 3, 3, 2, 9 ]->standard_deviation(); + + # HASH refs + # ããã·ã¥ãªãã¡ã¬ã³ã¹ + + { alpha => 'beta', gamma => 'vlissides' }->for_each(...); + + #XXX CODE refs + #XXX CODE ãªãã¡ã¬ã³ã¹ + + my $plus_five = (\&add)->curry()->(5); + my $minus_three = sub { $_[0] - $_[1] }->reverse->curry->(3); + + # can(), isa() and VERSION() work as expected + # can(), isa() åã³ VERSION() ã¯æ³å®éãã«åä½ãã¾ã + + if ("Hello, World"->can('foo')) ... + if (3.1415927->isa('Number')) ... + if ([ ... ]->VERSION() > 0.01) ... + +=head1 説æ + +The autobox pragma endows Perl's core datatypes with the capabilities of +first-class objects. This allows methods to be called on ARRAY refs, +HASH refs, CODE refs and raw scalars in exactly the same manner as blessed +references. The autoboxing is transparent: boxed values are not blessed +into their (user-defined) implementation class (unless the method elects to +bestow such a blessing) - they simply use its methods as though they are. + +autobox ãã©ã°ã㯠Perl ã®çµã¿è¾¼ã¿åã«ãã¡ã¼ã¹ãã¯ã©ã¹ãªãã¸ã§ã¯ã +ã¨ãã¦ã®è½åã賦ä¸ãã¾ã. ãã㯠ARRAY, HASH, CODE ãªãã¡ã¬ã³ã¹ã +çã®ã¹ã«ã©ã¼ãããã¬ã¹ããããªãã¡ã¬ã³ã¹ã¨å ¨ãåãããã«ã¡ã½ããã +å¼ã¹ããã¨ã許å¯ãã¾ã. autoboxã¯ééçã«è¡ããã¾ã: autoboxããã +å¤ã¯(ã¦ã¼ã¶ãå®ç¾©ãã)å®è£ ã¯ã©ã¹ã«ãã¬ã¹ããããã¨ã¯ããã¾ãã( +ã¡ã½ããããã¬ã¹ã®ãããªãªã«ããä¸ãããã¨ãé¸ã¶ãã¨ãªãã«) - +ããã«ããããããåç´ã«ãã®ã¡ã½ãããå©ç¨ãã¾ã. + +autobox is lexically scoped, and handlers (see below) for an outer scope +can be overridden or countermanded in a nested scope: + +autobox ã¯ã¬ãã·ã«ã«ã¹ã³ã¼ãã«ãã, å¤é¨ã®ã¹ã³ã¼ãã®ãã³ãã©(å¾è¿°)㯠+å å´ã®ã¹ã³ã¼ãã§ä¸æ¸ãåã¯åãæ¶ããã¨ãã§ãã¾ã. + + { + use autobox; # default handlers + ... + { + use autobox SCALAR => 'MyScalar'; + ... + } + # back to the default + ... + } + +Autoboxing can be turned off entirely by using the C<no> syntax: + +autoboxå㯠C<no> æ§æã使ããã¨ã§å®å ¨ã«è§£é¤ã§ãã¾ã: + + { + use autobox; + ... + no autobox; + ... + } + +- as well as by specifying a sole default value of undef (see below): + +- undef ãåç¬ã§ããã©ã«ãã®å¤ã«æå®ãã¦ãåæ§ã§ã(å¾è¿°): + + use autobox DEFAULT => undef; + +Autoboxing is not performed for barewords i.e. + +autoboxåã¯è£¸ã®åèª(bareword)ã«ã¯å½±é¿ãã¾ãã, ããªãã¡, + + my $foo = Foo->new(); + +and: + +åã³: + + my $foo = new Foo; + +behave as expected. + +ã¯æã£ãéãã«æ¯ãèãã¾ã. + +In addition, it only covers named methods, so while this works: + +å ãã¦, ååä»ãã®ã¡ã½ããã®ã¿ã対象ã¨ãã¾ã, ã¤ã¾ãããã¯åä½ãã¾ãã: + + my $foobar = { foo => 'bar' }->some_method(); + +These don't: + +ããã¯åä½ãã¾ãã: + + my $method1 = 'some_method'; + my $method2 = \&HASH::some_method; + + my $error1 = { foo => 'bar' }->$method1(); + my $error2 = { foo => 'bar' }->$method2(); + +The classes into which the core types are boxed are fully configurable. +By default, a method invoked on a non-object value is assumed to be +defined in a package whose name corresponds to the ref() type of that +value - or 'SCALAR' if the value is a non-reference. + +ã³ã¢ã¿ã¤ããboxåãããã¯ã©ã¹ã¯å®å ¨ã«è¨å®å¯è½ã§ã. ããã©ã«ãã§ã¯ +ãªãã¸ã§ã¯ãã§ã¯ãªãå¤ããå¼ã³åºãããã¡ã½ããã¯, ãã®å¤ã® +ref() ã¨å¯¾å¿ããããã±ã¼ã¸åã, ãªãã¡ã¬ã³ã¹ã§ãªããã° 'SCALAR' +ã§ã. + +Thus a vanilla: + +ããªãã¡æ¨æºçãª: + + use autobox; + +registers the following default handlers (for the current lexical scope): + +ã¯ä»¥ä¸ã®ããã©ã«ããã³ãã©ã(ç¾å¨ã®ã¬ãã·ã«ã«ã¹ã³ã¼ãã«å¯¾ãã¦) +ç»é²ãã¾ã: + + { + SCALAR => 'SCALAR', + ARRAY => 'ARRAY', + HASH => 'HASH', + CODE => 'CODE' + } + +Consequently: + +ããã«ãã: + + "hello, world"->upper() + +would be invoked as: + +ã¯ä»¥ä¸ã®å¼ã³åºãã¨ãªãã¾ã: + + SCALAR::upper("hello, world") + +while: + +ããã«: + + [ 1 .. 10 ]->for_each(sub { ... }) + +resolves to: + +ã¯ä»¥ä¸ã¨ãªãã¾ã: + + ARRAY::for_each([ 1 .. 10 ], sub { ... }) + +A mapping from the builtin type to the user-defined class can be specified +by passing a list of key/value bindings to the C<use autobox> statement. + +çµã¿è¾¼ã¿åããã¦ã¼ã¶å®ç¾©ã¯ã©ã¹ã¸ã®ãããã³ã°ã¯ +C<use autobox> æã¸ãã¼/å¤ãã¤ã³ãã£ã³ã°ã®ãªã¹ãã渡ããã¨ã§ +æå®ã§ãã¾ã. + +The following example shows the range of valid arguments: + +以ä¸ã®ä¾ã§ã¯æå¹ãªå¼æ°ã®ç¯å²ã示ãã¾ã: + + use autobox SCALAR => 'MyScalar' # package name + ARRAY => 'MyNamespace::', # package prefix (ending in '::') + HASH => '', # use the default i.e. HASH + CODE => undef, # don't autobox this type + DEFAULT => ..., # can take any of the 4 types above + UNDEF => ..., # can take any of the 4 types above + REPORT => ...; # boolean or coderef + +SCALAR, ARRAY, HASH, CODE, UNDEF and DEFAULT can take four different types of value: + +SCALAR, ARRAY, HASH, CODE, UNDEF ãã㦠DEFAULT ã¯ï¼ç¨®é¡ã®å¤ãåããã¨ã +åºæ¥ã¾ã: + +=over + +=item * + + + +A package name e.g. + +ããã±ã¼ã¸å, ä¾ãã°: + + use autobox SCALAR => 'MyScalar'; + +This overrides the default package - in this case SCALAR. All methods invoked on +literals or values of builtin type 'key' will be dispatched +as methods of the package specified in the corresponding 'value'. + +ããã¯ããã©ã«ãã®ããã±ã¼ã¸ - ãã®å ´åã«ã¯ SCALAR ãä¸æ¸ããã¾ã. +ãªãã©ã«è¥ããã¯çµã¿è¾¼ã¿å 'key' ã§ã®å ¨ã¦ã®ã¡ã½ããå¼ã³åºã㯠+'value' ã«å¯¾å¿ãã¦æå®ãããããã±ã¼ã¸ã®ã¡ã½ããã¨ãã¦é éããã¾ã. + +If a package name is supplied for DEFAULT, it becomes the default package +for all unhandled cases. Thus: + +ããããã±ã¼ã¸åã DEFAULT ã«å¯¾ãã¦æä¾ãããã®ãªã, å ¨ã¦ã®å¦çãã㦠+ããªãé¨åã®ããã©ã«ãã®ããã±ã¼ã¸ã«ãªãã¾ã. ããªãã¡: + + use autobox ARRAY => 'MyArray', + DEFAULT => 'MyDefault'; + +will invoke ARRAY methods on MyArray and all other methods on MyDefault. + +㯠ARRAY ã¡ã½ããã MyArray ã§å¼ã³åºã, ãã以å¤ã MyDefault 㧠+å¼ã³åºãã¾ã. + +=item * + + + +A namespace: this is a package prefix (up to and including the final '::') +to which the name of the default handler for this type will be appended: + +åå空é: ããã¯ãã®ã¿ã¤ãã«å¯¾ããããã©ã«ãã®ãã³ãã©åãä»ä¸ããã +ããã±ã¼ã¸ã®ãã¬ãã£ãã¯ã¹ã«ãªãã¾ã(æ«å°¾ã® '::' ã¾ã§ãå«ã¿ã¾ã): + +Thus: + +ããªãã¡: + + use autobox ARRAY => 'Prelude::'; + +binds ARRAY types to the Prelude::ARRAY package. + +㯠ARRAY ã¿ã¤ãã Prelude::ARRAY ããã±ã¼ã¸ã«æç¸ãã¾ã. + +As with the package name form, specifying a default namespace e.g. + +ããã±ã¼ã¸åã¨åæã«, ããã©ã«ãã®åå空éãæå®ãããã¨ã§, ä¾ãã°, + + use autobox SCALAR => 'MyScalar', + DEFAULT => 'MyNamespace::'; + +binds MyNamespace::ARRAY, MyNamespace::HASH &c. to the corresponding builtin +types. + +ã¯, MyNamespace::ARRAY, MyNamespace::HASH &c. ã対å¿ããçµã¿è¾¼ã¿åã« +æç¸ãã¾ã. + +=item * + + + +An empty string: this is shorthand for the builtin type name. e.g. + +空ã®æåå: ããã¯çµã¿è¾¼ã¿åã®ååã®ç縮表ç¾ã§ã, ä¾ãã°, + + use autobox SCALAR => 'MyScalar', + ARRAY => '', + DEFAULT => 'MyDefault::'; + +is equivalent to: + +ã¯ä»¥ä¸ã¨çå¤ã§ã: + + use autobox SCALAR => 'MyScalar' + ARRAY => 'ARRAY', + DEFAULT => 'MyDefault::'; + +which in turn is equivalent to: + +ããã¦ããã¯ä»¥ä¸ã¨çå¤ã§ã: + + use autobox SCALAR => 'MyScalar' + ARRAY => 'ARRAY', + HASH => 'MyDefault::HASH', + CODE => 'MyDefault::CODE'; + +If DEFAULT is set to an empty string (as it is by default), +it fills in the default type for all the unhandled cases e.g. + +ãã DEFAULT ã«ç©ºã®æååãè¨å®ãããã¨(ããã¯ããã©ã«ããªã®ã§), +å ¨ã¦ã®å¦çããã¦ããªãç®æãããã©ã«ãã®åã§åãã¾ã, ä¾ãã°, + + use autobox SCALAR => 'MyScalar', + CODE => 'MyCode', + DEFAULT => ''; + +is equivalent to: + +ã¯ä»¥ä¸ã¨ç価ã§ã: + + use autobox SCALAR => 'MyScalar', + CODE => 'MyCode', + ARRAY => 'ARRAY', + HASH => 'HASH'; + +=item * + + + +undef: this disables autoboxing for the specified type, or all unhandled types +in the case of DEFAULT. + +undef: ããã¯æå®ããå, ããã㯠DEFAULT ã®æã«ã¯å ¨ã¦ã®æå®ãã¦ããªã +åã«å¯¾ã㦠autobox ãç¡å¹ã«ãã¾ã. + +=back + +In addition to the SCALAR, ARRAY, HASH, CODE and DEFAULT fields above, +there are two additional fields: UNDEF and REPORT. + +ããã¾ã§ã«è¿°ã¹ã SCALAR, ARRAY, HASH, CODE ãã㦠DEFAULT ã® +ãã£ã¼ã«ãã«å ãã¦ï¼ã¤ã®ãã£ã¼ã«ã, UNDEF åã³ REPORT ãããã¾ã. + +=head2 UNDEF + +The pseudotype, UNDEF, can be used to autobox undefined values. These are +not autoboxed by default (i.e. the default value is undef): + +ä»®æ³å, UNDEF, ã¯æªå®ç¾©å¤ã® autobox ã«å©ç¨ããã¾ã. +ããã¯ããã©ã«ãã§ã¯ autobox ããã¾ãã (ããªãã¡ããã©ã«ã㯠undef +ã§ã): + +This doesn't work: + +ããã¯åä½ãã¾ãã: + + use autobox; + + undef->foo() # runtime error + +This works: + +ãããªãåä½ãã¾ã: + + use autobox UNDEF => 'MyPackage'; + + undef->foo(); # ok + +So does this: + +ããã¦ãããåä½ãã¾ã: + + use autobox UNDEF => 'MyNamespace::'; + + undef->foo(); # ok + +=head2 REPORT + +REPORT exposes the current handlers by means of a callback, or a +static reporting function. + +REPORT ã¯ç¾å¨ã®ãã³ãã©ãã³ã¼ã«ããã¯è¥ããã¯éçãªã¬ãã¼ãé¢æ° +ã§é²åºããã¾ã. + +This can be useful if one wishes to see the computed bindings +in 'longhand'. + +ããã¯ææ¸ãã§ç®åºãããæç¸ãè¦ãã人ã«ã¯ä¾¿å©ã§ã. + +Reporting is ignored if the value corresponding to the REPORT key is false. + +ã¬ãã¼ã㯠REPORT ãã¼ã«å¯¾å¿ããå¤ãå½ã§ããã°ç¡è¦ããã¾ã. + +If the value is a CODE ref, then this sub is called with a reference to +the HASH containing the computed handlers for the current scope. + +å¤ã CODE ãªãã¡ã¬ã³ã¹ã§ããã°ãã®é¢æ°ãç¾å¨ã®ã¹ã³ã¼ãã«å¯¾ã㦠+ç®åºããããã³ãã©ãå«ãã ããã·ã¥ã¸ã®ãªãã¡ã¬ã³ã¹ãä¼´ã£ã¦ +å¼ã³åºããã¾ã. + +Finally, if REPORT is true but not a CODE ref, the handlers are dumped +to STDERR. + +ããã¦æå¾ã«, REPORT ãçã§ä¸ã¤ CODE ãªãã¡ã¬ã³ã¹ã§ãªããã°, ãã³ãã©ã¯ +æ¨æºã¨ã©ã¼ã«ãã³ãããã¾ã. + +Thus: + +ããªãã¡: + + use autobox REPORT => 1, ... + +or + +è¥ãã㯠+ + use autobox REPORT => sub { ... }, ... + +or + +è¥ãã㯠+ + sub my_callback ($) { + my $hashref = shift; + ... + } + + use autobox REPORT => \&my_callback, ... + +=head1 è¦å + +Due to Perl's precedence rules some autoboxed literals may need to be +parenthesized: + +Perl ã®åªå é ä½ã®ããã«å¹¾ã¤ãã® autobox ãªãã©ã«ã¯æ¬å¼§ã§ããã +å¿ è¦ãããã¾ã: + +For instance, while this works: + +ä¾ãã°ããã¯åä½ãã¾ãã: + + my $curried = sub { ... }->curry(); + +this doesn't: + +ããã¯åä½ãã¾ãã: + + my $curried = \&foo->curry(); + +The solution is to wrap the reference in parentheses: + +ãªãã¡ã¬ã³ã¹ãæ¬å¼§ã§ããããã¨ã§è§£æ±ºåºæ¥ã¾ã: + + my $curried = (\&foo)->curry(); + +The same applies for signed integer and float literals: + +符å·ä»ãã®æ´æ°åã³æµ®åå°æ°ç¹æ°ãªãã©ã«ã«ãåæ§ã«é©ç¨ã§ãã¾ã: + + # this works + # ããã¯åä½ãã¾ã + my $range = 10->to(1); + + # this doesn't work + # ããã¯åä½ãã¾ãã + my $range = -10->to(10); + + # this works + # ããã¯åä½ãã¾ã + my $range = (-10)->to(10); + +Perl's special-casing for the C<print BLOCK ...> syntax +(see perlsub) means that C<print { expression() } ...> +(where the curly brackets denote an anonymous HASH ref) +may require some further disambiguation: + +Perl ã® C<print BLOCK> æ§æ(perlsubåç §)ã«é¢ããç¹æ®ãªã±ã¼ã¹ã§ã¯ +C<print { expression() } ...> (ããã§æ³¢æ¬å¼§ã¯ç¡åããã·ã¥ +ãªãã¡ã¬ã³ã¹ãæå³ãã¾ã)ã«ããã¦å¹¾ã¤ãã®ææ§æ§ã®é¤å»ã®ããã« +å¹¾åè¸ã¿è¾¼ãå¿ è¦ãããã¾ã. + + # this works ( + # ããã¯åä½ãã¾ã( + print { foo => 'bar' }->foo(); + + # and this + # ããã¦ããã + print { 'foo', 'bar' }->foo(); + + # and even this + # ãããã¾ã + print { 'foo', 'bar', @_ }->foo(); + + # but this doesn't + # ã§ãããã¯ã ãã§ã + print { @_ }->foo() ? 1 : 0 + +In the latter case, the solution is to supply something +other than a HASH ref literal as the first argument +to print(): + +å¾è ã®ã±ã¼ã¹ã§ã¯, ããã解決ããããã« print() ã®æåã®å¼æ°ã« +HASH ãªãã¡ã¬ã³ã¹ä»¥å¤ã®ä½ãã渡ãå¿ è¦ãããã¾ã: + + # e.g. + # ä¾ + print STDOUT { @_ }->foo() ? 1 : 0; + + # or + # è¥ãã㯠+ my $hashref = { @_ }; + print $hashref->foo() ? 1 : 0; + + # or + # è¥ãã㯠+ print '', { @_ }->foo() ? 1 : 0; + + # or + # è¥ãã㯠+ print '' . { @_ }->foo() ? 1 : 0; + + # or even + # ããã«è¥ãã㯠+ { @_ }->print_if_foo(1, 0); + +Although C<isa> and C<can> are "overloaded" for autoboxed values, the C<VERSION> method isn't. +Thus, while these work: + +C<isa> åã³ C<can> 㯠autobox ãããå¤ã§ã¯"ä¸æ¸ã(overload)"ãã㦠+ãã¾ãã, C<VERSION> ã¡ã½ããã¯ããã¦ãã¾ãã. +ã¤ã¾ã, ãããã¯åä½ãã¾ãã: + + [ ... ]->can('pop') + + 3.1415->isa('MyScalar') + +This doesn't: + +ããã¯åä½ãã¾ãã: + + use MyScalar 1.23; + + use autobox SCALAR => MyScalar; + + print "Hello, World"->VERSION(), $/; + +Though, of course: + +ããã©ããã¡ãã: + + print MyScalar->VERSION(), $/; + +and + +åã³ + + print $MyScalar::VERSION, $/; + +continue to work. + +ã¯å¾æ¥éãã«åä½ãã¾ã. + +This is due to a limitation in perl's implementation of C<use> and C<no>. +Likewise, C<import> and C<unimport> are unaffected by the autobox pragma: + +ãã㯠perl ã® C<use> åã³ C<no> ã®å®è£ ã«ããå¶éã§ã. åãããã« +C<import> åã³ C<unimport> 㯠autobox ãã©ã°ãããã¯å½±é¿ãåãã¾ãã. + + 'Foo'->import() # equivalent to Foo->import() rather than MyScalar->import('Foo') + # MyScalar->import('Foo') ã§ã¯ãªãFoo->import() ã¨ç価 + + []->import() # error: Can't call method "import" on unblessed reference + # ã¨ã©ã¼: ãã¬ã¹ããã¦ããªããªãã¡ã¬ã³ã¹ä¸ã§ "import" ã¡ã½ãããå¼ã³åºãã¾ãã + +=head1 ãã¼ã¸ã§ã³ + +1.03 + + +=head1 é¢é£é ç® + +L<autobox::Core>, L<Perl6::Contexts>, L<Scalar::Properties>, L<Set::Array>, L<String::Ruby>, L<Language::Functional> + + +=head1 èè + +chocolateboy: <choco****@email*****> + + +=head1 èä½æ¨© + +Copyright (c) 2005, chocolateboy. + + +This module is free software. It may be used, redistributed +and/or modified under the same terms as Perl itself. + +ãã®ã¢ã¸ã¥ã¼ã«ã¯ããªã¼ã½ããã¦ã§ã¢ã§ã. Perl ã¨åãã©ã¤ã»ã³ã¹ã® +å ã§å©ç¨, åé å¸åã³å¤æ´ãè¡ããã¨ãåºæ¥ã¾ã. +