File Coverage

blib/lib/ARGV/OrDATA.pm
Criterion Covered Total %
statement 21 28 75.0
branch 1 2 50.0
condition 3 6 50.0
subroutine 7 8 87.5
pod n/a
total 32 44 72.7


line stmt bran cond sub pod time code
1             package ARGV::OrDATA;
2              
3 2     2   142744 use 5.006;
  2         27  
4 2     2   10 use strict;
  2         3  
  2         39  
5 2     2   9 use warnings;
  2         2  
  2         178  
6              
7             =head1 NAME
8              
9             ARGV::OrDATA - Let the diamond operator read from DATA if there's no ARGV
10              
11             =head1 VERSION
12              
13             Version 0.003
14              
15             =cut
16              
17             our $VERSION = '0.003';
18              
19             sub import {
20 2   66 2   26 my ($package) = $_[1] || caller;
21 2     2   13 { no strict 'refs';
  2         2  
  2         93  
  2         5  
22 2     2   11 no warnings 'once';
  2         4  
  2         225  
23 2         10 *ORIG = *ARGV;
24 2 50 33     3132 *ARGV = *{$package . '::DATA'} unless @ARGV || ! -t;
  0            
25             }
26             }
27              
28              
29             sub unimport {
30 0     0     my $package = shift;
31 0           *ARGV = *ORIG;
32 2     2   14 { no strict 'refs';
  2         3  
  2         163  
  0            
33 0           delete ${$package . '::'}{ORIG};
  0            
34             }
35 0           undef *ORIG;
36             }
37              
38             =head1 SYNOPSIS
39              
40             use ARGV::OrDATA;
41              
42             while (<>) {
43             print;
44             }
45              
46             __DATA__
47             You'll see this if you don't redirect something to the script's
48             STDIN or you don't specify a filename on the command line.
49              
50             =head1 DESCRIPTION
51              
52             Tell your script it should use the DATA section if there's no input
53             coming from STDIN and there are no arguments.
54              
55             You can also specify which package's DATA should be read instead of
56             the caller's:
57              
58             use My::Module;
59             use ARGV::OrDATA 'My::Module';
60              
61             while (<>) { # This reads from My/Module.pm's DATA section.
62              
63             To restore the old behaviour, you can call the C method.
64              
65             use ARGV::OrDATA;
66              
67             my $from_data = <>;
68              
69             @ARGV = 'file1.txt'; # Ignored.
70              
71             'ARGV::OrDATA'->unimport;
72              
73             @ARGV = 'file2.txt'; # Works.
74              
75             my $from_file2 = <>;
76              
77             Calling C after C would restore the DATA handle, but
78             B, i.e. it would continue from where you stopped
79             (see t/04-unimport.t).
80              
81             =head2 Why?
82              
83             I use this technique when solving programming contests. The sample
84             input is usually small and I don't want to waste time by saving it
85             into a file.
86              
87             =head1 EXPORT
88              
89             Nothing.
90              
91             =head1 AUTHOR
92              
93             E. Choroba, C<< >>
94              
95             =head1 BUGS
96              
97             Please report any bugs or feature requests to the GitHub repository at
98             L.
99              
100             =head1 SUPPORT
101              
102             You can find documentation for this module with the perldoc command.
103              
104             perldoc ARGV::OrDATA
105              
106              
107             You can also look for information at:
108              
109             =over 4
110              
111             =item * MetaCPAN
112              
113             L
114              
115             =item * GitHub
116              
117             L
118              
119             =back
120              
121              
122             =head1 ACKNOWLEDGEMENTS
123              
124              
125             =head1 LICENSE AND COPYRIGHT
126              
127             Copyright 2017 E. Choroba.
128              
129             This program is free software; you can redistribute it and/or modify it
130             under the terms of the the Artistic License (2.0). You may obtain a
131             copy of the full license at:
132              
133             L
134              
135             Any use, modification, and distribution of the Standard or Modified
136             Versions is governed by this Artistic License. By using, modifying or
137             distributing the Package, you accept this license. Do not use, modify,
138             or distribute the Package, if you do not accept this license.
139              
140             If your Modified Version has been derived from a Modified Version made
141             by someone other than you, you are nevertheless required to ensure that
142             your Modified Version complies with the requirements of this license.
143              
144             This license does not grant you the right to use any trademark, service
145             mark, tradename, or logo of the Copyright Holder.
146              
147             This license includes the non-exclusive, worldwide, free-of-charge
148             patent license to make, have made, use, offer to sell, sell, import and
149             otherwise transfer the Package with respect to any patent claims
150             licensable by the Copyright Holder that are necessarily infringed by the
151             Package. If you institute patent litigation (including a cross-claim or
152             counterclaim) against any party alleging that the Package constitutes
153             direct or contributory patent infringement, then this Artistic License
154             to you shall terminate on the date that such litigation is filed.
155              
156             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
157             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
158             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
159             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
160             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
161             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
162             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
163             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
164              
165              
166             =cut
167              
168             __PACKAGE__