|
NAMEPerl6::Subs - Define your subroutines in the Perl 6 styleVERSIONVersion 0.05SYNOPSISuse Perl6::Subs; sub foo ($x) # Positional parameters { bar($x) } sub get (Array $x) # Type validation { pop @$x } sub get_or_die ($x of Array where { @$_ }) # Subtyping { pop @$x } sub show (Str $s, IO ?$io) # Optional parameters { print { $io || *STDOUT } $s } sub limit (Int $i, Int +$hi, Int +$low) # Named parameters { ... } method foo # Invocant: '$self' { $self->bar } method foo (Foo $self: Bar $x) # Parameter 'isa' classname { $self->use_bar($x) } And there's more... DESCRIPTIONPerl6::Subs is a source filter that adds a very useful subset of Perl 6 subroutine syntax to Perl 5. Given a subroutine defined with a Perl 6 prototype, the code generated by Perl6::Subs will, at runtime, declare the formal parameters, assign them their values, and validate their contents according to both built-in and user-given rules.Perl6::Subs supports all five categories of Perl 6 subroutine parameters, here listed in their mandatory order. (You may skip categories, but not reorder them.)
Perl 5's limited function call semantics (the single array @_) prevent Perl6::Subs from supporting all the features of Perl 6 parameter lists. A given subroutine can have either optional positional parameters or named parameters or a slurpy parameter; combining them is illegal. As the lone exception, a subroutine with named parameters may also have a slurpy hash; the hash will contain all the key/value pairs not explicitly given as named parameters. TYPES AND VALIDATIONPerl6::Subs understands the following type names, and will generate code that validates at runtime that a declared parameter's value actually has the given type, and throws an exception if it fails.First, the fundamental Perl 6 types, which in Perl 6 will be unboxed (non-objects):
Now, the "object types". Note that while Perl 6 considers all these to be objects, Perl 5 often doesn't. Also note that, in general, "undef" is permitted as a valid value for all object types.
Perl6::Subs also supports these type names which are not legal in Perl 6, but which may be useful in writing Perl 5:
Finally, any bareword other than the above used as a type name is assumed to name a user-defined class. A parameter so typed must satisfy UNIVERSAL::isa() of that class, or be undef. If Perl warnings are enabled at compile time, a mispelled class name will generate a diagnostic. SUBROUTINE DECLARATION SYNTAXPerl6::Subs filters subroutines declared either with a Perl 6 style prototype; or with the "method" keyword replacing the word "sub" (in which case the "method" trait is implied). Perl6::Subs also understand both Perl 5 and Perl 6 syntax for subroutine traits (Perl 5 calls them "attributes"). Thus, these declarations are synonymous:sub foo is method {...} sub foo returns(Any) is method {...} sub foo ($self:) : method {...} method foo {...} method foo ($self:) {...} However, this declaration uses no Perl 6 features, and therefore Perl6::Subs does not filter it: sub foo : method {...} Perl6::Subs understands subroutine traits after the prototype declared with three syntax flavors:
PARAMETER DECLARATION SYNTAX, WITH A SIDE OF SUBTYPINGPerl6::Subs supports a subset of the Perl 6 type declaration syntax. Allowed parameter forms are "Type $var" and "$var of Type". Thus,sub foo (Int $i) and sub foo ($i of Int) are synonymous. Any parameter traits must be specified at the end of the given declaration. Thus a required named parameter of type Int may be specified as: sub foo (Int +$i is required) or sub foo (+$i of Int is required) (The only implemented parameter trait as of this writing is "is required", which is only meaningful on named parameters.) You may create an anonymous subtype (restricted type) using a "where" clause, which specifies a block of code that must evaluate to true when $_ is set to a given value. For example, to accept only positive integers as parameters, you could write: sub foo (Int where { $_ > 0 } $i) or sub foo ($i of Int where { $_ > 0 }) (The latter is clearer, in the author's opinion, since the variable is textually closer to the base type.) DEBUGGINGIf the environment variable "PERL6_SUBS_DEBUG" is set to a true value, Perl6::Subs will print to standard output the complete filtered text of any source file in which it is used.If you're debugging Perl6::Subs itself, the environment variables "PERL6_SUBS_RD_TRACE" and "PERL6_SUBS_RD_HINTS" set the $::RD_TRACE and $::RD_HINTS variables, respectively, opening a window on the operation of "Parse::RecDescent". CAVEATS
TODO
BUGSThis module is a source filter. Source filters always break. For example, the breakage caused by parameter names that turn into Perl quoting operators when their sigils are stripped may never be fixed.Please report any other bugs or feature requests to "bug-perl6-subs@rt.cpan.org", or through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Perl6-Subs>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. AUTHORChip Salzenberg, "<chip@pobox.com>"ACKNOWLEDGEMENTSThanks to Heath Market Science <hmsonline.com> for funding creation of this module. Thanks also to Larry, Damian, Allison, et al for Perl 6 subroutine syntax, and to Damian for Filter::Simple and Parse::RecDescent.COPYRIGHT & LICENSECopyright 2005 Chip Salzenberg and Health Market Science.This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Visit the GSP FreeBSD Man Page Interface. |