File Coverage

blib/lib/Sub/ArgShortcut.pm
Criterion Covered Total %
statement 21 23 91.3
branch 9 10 90.0
condition n/a
subroutine 6 7 85.7
pod 1 2 50.0
total 37 42 88.1


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             =head1 NAME
4              
5             Sub::ArgShortcut - simplify writing functions that use default arguments
6              
7             =head1 VERSION
8              
9             This document describes Sub::ArgShortcut version 1.02
10              
11             =cut
12              
13             =head1 SYNOPSIS
14              
15             use Sub::ArgShortcut::Attr;
16              
17             sub mychomp :ArgShortcut { chomp @_ }
18              
19             while ( <> ) {
20             # make a chomped copy of $_ without modifying it
21             my $chomped_line = mychomp;
22            
23             # or, modify $_ in place
24             mychomp;
25              
26             # ...
27             }
28              
29             =head1 DESCRIPTION
30              
31             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.
32              
33             =head2 C
34              
35             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:
36              
37             use Sub::ArgShortcut;
38             my $mychomp = argshortcut { chomp @_ };
39              
40             This function is exported by default.
41              
42             =head2 Sub::ArgShortcut::Attr and C<:ArgShortcut> - The attribute interface
43              
44             Instead of using L> to wrap a code reference, you can use an L-based interface to add Sub::ArgShortcut functionality to regular subs. Simply C Sub::Shortcut::Attr instead of Sub::Shortcut, then request its behaviour using the C<:ArgShortcut> attribute on functions:
45              
46             sub mychomp :ArgShortcut { chomp @_ }
47              
48             my $mychomp = sub :ArgShortcut { chomp @_ };
49              
50             =head1 BUGS AND LIMITATIONS
51              
52             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.
53              
54             Please report any other bugs or feature requests to L, or through the web interface at L.
55              
56              
57             =head1 AUTHOR
58              
59             Aristotle Pagaltzis L
60              
61              
62             =head1 LICENCE AND COPYRIGHT
63              
64             Copyright (c) 2006-2008, Aristotle Pagaltzis. All rights reserved.
65              
66             This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See L.
67              
68              
69             =head1 DISCLAIMER OF WARRANTY
70              
71             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
72              
73             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
74              
75             =cut
76              
77             package Sub::ArgShortcut;
78              
79             $VERSION = '1.02';
80              
81 3     3   59257 use strict;
  3         6  
  3         110  
82 3     3   17 use warnings;
  3         4  
  3         599  
83              
84 0     0 0 0 sub croak { require Carp; goto &Carp::croak }
  0         0  
85              
86             sub argshortcut(&) {
87 2     2 1 15 my ( $code ) = @_;
88             return sub {
89 8     8   5672 my @byval;
90 8         16 my $nondestructive = defined wantarray;
91 8 100       55 $code->(
    100          
    100          
92             $nondestructive
93             ? ( @byval = @_ ? @_ : $_ )
94             : ( @_ ? @_ : $_ )
95             );
96 8 100       98 return $nondestructive ? @byval[ 0 .. $#byval ] : ();
97 2         18 };
98             }
99              
100             sub import {
101 3     3   20 my $class = shift;
102 3         9 my $install_pkg = caller;
103 3 50       17 die q(Something mysterious happened) if not defined $install_pkg;
104 3     3   16 { no strict 'refs'; *{"${install_pkg}::argshortcut"} = \&argshortcut; }
  3         15  
  3         215  
  3         6  
  3         8  
  3         181  
105             }
106              
107             1;