Filter::signatures - very simplicistic signatures for Perl < 5.20
use Filter::signatures; no warnings 'experimental::signatures'; # does not raise an error use feature 'signatures'; # this now works on <5.20 as well sub hello( $name ) { print "Hello $name\n"; } hello("World"); sub hello2( $name="world" ) { print "Hello $name\n"; } hello2(); # Hello world
This module implements a backwards compatibility shim for formal Perl subroutine signatures that were introduced to the Perl core with Perl 5.20.
The technique used is a very simplicistic transform to allow for using very simplicistic named formal arguments in subroutine declarations. This module does not implement warning if more or fewer parameters than expected are passed in.
The module also implements default values for unnamed parameters by splitting the formal parameters on /,/ and assigning the values if @_ contains fewer elements than expected. Function calls as default values may work by accident. Commas within default values happen to work due to the design of Filter::Simple, which removes them for the application of this filter.
/,/
@_
Note that this module inherits all the bugs of Filter::Simple and potentially adds some of its own. Most notable is that Filter::Simple sometimes will misinterpret the division operator / as a leading character to starting a regex match:
/
my $wait_time = $needed / $supply;
This will manifest itself through syntax errors appearing where everything seems in order. The hotfix is to add a comment to the code that "closes" the misinterpreted regular expression:
my $wait_time = $needed / $supply; # / for Filter::Simple
A better hotfix is to upgrade to Perl 5.20 or higher and use the native signatures support there. No other code change is needed, as this module will disable its functionality when it is run on a Perl supporting signatures.
Due to a peculiarity of how Filter::Simple treats here documents in some versions, line numbers may get out of sync if you use here documents.
If you spread your formal signatures across multiple lines, the line numbers may also go out of sync with the original document.
The module does not support comments within signatures
sub invalid ( $name, # use this as the name $location, # use this as the location ) { "This is an example" }
The workaround is to not do that or to upgrade to Perl 5.20 or higher and use the native signatures support there.
eval
Filter::Simple does not trigger when using code such as
eval <<'PERL'; use Filter::signatures; use feature 'signatures'; sub foo (...) { } PERL
So, creating subroutines with signatures from strings won't work with this module. The workaround is to upgrade to Perl 5.20 or higher.
The generated code does not deparse identically to the code generated on a Perl with native support for signatures.
If you want to force the use of this module even under versions of Perl that have native support for signatures, set $ENV{FORCE_FILTER_SIGNATURES} to a true value before the module is imported.
$ENV{FORCE_FILTER_SIGNATURES}
"Signatures" in perlsub
signatures - a module that doesn't use a source filter but optree modification instead
Sub::Signatures
The public repository of this module is http://github.com/Corion/filter-signatures.
The public support forum of this module is https://perlmonks.org/.
Please report bugs in this module via the RT CPAN bug queue at https://rt.cpan.org/Public/Dist/Display.html?Name=Filter-signatures or via mail to filter-signatures-Bugs@rt.cpan.org.
Max Maischein corion@cpan.org
corion@cpan.org
Copyright 2015-2016 by Max Maischein corion@cpan.org.
This module is released under the same terms as Perl itself.
To install Filter::signatures, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Filter::signatures
CPAN shell
perl -MCPAN -e shell install Filter::signatures
For more information on module installation, please visit the detailed CPAN module installation guide.