Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ap/sub-argshortcut
simplify writing functions that use default arguments
https://github.com/ap/sub-argshortcut
perl
Last synced: 10 days ago
JSON representation
simplify writing functions that use default arguments
- Host: GitHub
- URL: https://github.com/ap/sub-argshortcut
- Owner: ap
- Created: 2015-01-05T08:49:04.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2022-09-04T15:59:08.000Z (over 2 years ago)
- Last Synced: 2024-11-06T03:03:58.682Z (about 2 months ago)
- Topics: perl
- Language: Perl
- Homepage: https://metacpan.org/release/Sub-ArgShortcut
- Size: 17.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.pod
- Changelog: Changes
Awesome Lists containing this project
README
use 5.006; use strict; use warnings;
package Sub::ArgShortcut;
our $VERSION = '1.022';
sub argshortcut(&) {
my ( $code ) = @_;
return sub {
my @byval;
my $nondestructive = defined wantarray;
$code->(
$nondestructive
? ( @byval = @_ ? @_ : $_ )
: ( @_ ? @_ : $_ )
);
return $nondestructive ? @byval[ 0 .. $#byval ] : ();
};
}sub import {
my $class = shift;
my $install_pkg = caller;
die q(Something mysterious happened) if not defined $install_pkg;
{ no strict 'refs'; *{"${install_pkg}::argshortcut"} = \&argshortcut; }
}1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Sub::ArgShortcut - simplify writing functions that use default arguments
=head1 SYNOPSIS
use Sub::ArgShortcut::Attr;
sub mychomp : ArgShortcut { chomp @_ }
while ( <> ) {
# make a chomped copy of $_ without modifying it
my $chomped_line = mychomp;
# or, modify $_ in place
mychomp;
# ...
}=head1 DESCRIPTION
This module encapsulates the logic required for functions that assume C<$_> as
their argument when called with an empty argument list, and which modify their
arguments in void context but return modified copies in any other context. You
only need to write code which modifies the elements of C<@_> in-place.=head1 INTERFACE
=head2 C
This function takes a code reference as input, wraps a function around it and
returns a reference to that function. The code that is passed in should modify
the values in C<@_> in whatever fashion desired. The function from the
L could therefore also be written like this:use Sub::ArgShortcut;
my $mychomp = argshortcut { chomp @_ };=head2 C<:ArgShortcut>
Instead of using L> to wrap a code reference,
you can write regular subs and then add Sub::ArgShortcut functionality to them
implicitly. Simply C instead of Sub::Shortcut, then
request its behaviour using the C<:ArgShortcut> attribute on functions:sub mychomp : ArgShortcut { chomp @_ }
my $mychomp = sub : ArgShortcut { chomp @_ };
=head1 EXPORTS
Sub::ArgShortcut exports C by default.
=head1 BUGS AND LIMITATIONS
Passing an empty array to a shortcutting function will probably surprise you:
assuming C is a function with C<:ArgShortcut> and C<@bar> is an empty
array, then calling C will cause C to operate on C<$_>! This
is because C has no way of distinguishing whether it was called without
any arguments or called with arguments that evaluate to an empty list.=cut