File Coverage

blib/lib/Perl6/Say.pm
Criterion Covered Total %
statement 40 42 95.2
branch 5 6 83.3
condition n/a
subroutine 12 12 100.0
pod 1 1 100.0
total 58 61 95.0


line stmt bran cond sub pod time code
1             package Perl6::Say;
2 10     10   342289 use strict;
  10         22  
  10         361  
3 10     10   54 use warnings;
  10         24  
  10         1375  
4             require 5.006_002;
5             our $VERSION = '0.16';
6 10     10   12988 use IO::Handle;
  10         84008  
  10         636  
7 10     10   74 use Scalar::Util 'openhandle';
  10         20  
  10         1042  
8 10     10   75 use Carp;
  10         26  
  10         892  
9            
10             sub say {
11 38     38 1 144217 my $currfh = select();
12 38         76 my $handle;
13             {
14 10     10   90 no strict 'refs';
  10         20  
  10         509  
  38         50  
15 38 100       190 $handle = openhandle($_[0]) ? shift : \*$currfh;
16 10     10   45 use strict 'refs';
  10         17  
  10         1798  
17             }
18 38 100       113 @_ = $_ unless @_;
19 38         51 my $warning;
20 38     1   299 local $SIG{__WARN__} = sub { $warning = join q{}, @_ };
  1         5  
21 38         59 my $res = print {$handle} @_, "\n";
  38         332  
22 38 50       303 return $res if $res;
23 0         0 $warning =~ s/[ ]at[ ].*//xms;
24 0         0 croak $warning;
25             }
26            
27             # Handle direct calls...
28            
29 10     10   53 no strict 'refs';
  10         19  
  10         658  
30 9     9   87 sub import { *{caller() . '::say'} = \&say; }
  9         167  
31 10     10   62 use strict 'refs';
  10         20  
  10         913  
32            
33             # Handle OO calls:
34            
35             *IO::Handle::say = \&say if ! defined &IO::Handle::say;
36            
37             1;
38            
39             #################### DOCUMENTATION ####################
40            
41             =head1 NAME
42            
43             Perl6::Say - C, but no newline needed
44            
45             =head1 SYNOPSIS
46            
47             # Perl 5 code...
48            
49             use Perl6::Say;
50            
51             say 'boo'; # same as: print 'boo', "\n"
52            
53             say STDERR 'boo'; # same as: print STDERR 'boo', "\n"
54            
55             STDERR->say('boo'); # same as: print STDERR 'boo', \n"
56            
57             $fh->say('boo'); # same as: print $fh 'boo', "\n";
58            
59             say(); # same as: print "$_\n";
60            
61             say undef; # same as: print "\n";
62            
63             =head1 DESCRIPTION
64            
65             =head2 Note for Users of Perl 5.10
66            
67             You don't need this module. The Perl 6 C function is available in Perl
68             5.10 by saying C. Hence, this module is of interest only
69             to users of Perl 5.6 and 5.8.
70            
71             If you have Perl 5.10 installed, see the F<510/> directory in this
72             distribution for some elementary examples of C taken from C
73             feature>.
74            
75             =head2 General
76            
77             Implements a close simulation of the C function in Perl 6,
78             which acts like C but automatically appends a newline.
79            
80             Use it just like C (except that it only supports the indirect object
81             syntax when the stream is a bareword). That is, assuming the relevant
82             filehandles are open for output, you can use any of these:
83            
84             say @data;
85             say FH @data;
86             FH->say(@data);
87             *FH->say(@data);
88             (\*FH)->say(@data);
89             say $fh, @data;
90             $fh->say(@data);
91            
92             but not any of these:
93            
94             say {FH} @data;
95             say {*FH} @data;
96             say {\*FH} @data;
97             say $fh @data;
98             say {$fh} @data;
99            
100             =head2 Additional Permitted Usages
101            
102             As demonstrated in the test suite accompanying this distribution,
103             C can be used in all the following situations.
104            
105             $string = q{};
106             open FH, ">", \$string;
107             say FH qq{Hello World}; # print to a string
108             close FH; # requires Perl 5.8.0 or later
109            
110             use FileHandle;
111             $fh = FileHandle->new($file, 'w');
112             if (defined $fh) {
113             say $fh, qq{Hello World};
114             $fh->close;
115             }
116            
117             use IO::File;
118             $fh = IO::File->new($file, 'w');
119             if (defined $fh) {
120             say $fh, qq{Hello World};
121             $fh->close;
122             }
123            
124             $string = q{};
125             open FH, ">", \$string; # requires Perl 5.8.0 or later
126             select(FH);
127             say qq{Hello World};
128             close FH;
129            
130             =head2 Interaction with Output Record Separator
131            
132             In Perl 6, S> is exactly equivalent to
133             S>.
134            
135             That means that a call to C appends any output record separator (ORS)
136             I the added newline (though in Perl 6, the ORS is an attribute of
137             the filehandle being used, rather than a global C<$/> variable).
138            
139             =head2 C
140            
141             IO::Handle version 1.27 or later (which, confusingly, is
142             found in IO distribution 1.23 and later) also implements a C
143             method. Perl6::Say provides its own C method to IO::Handle
144             if C is not available.
145            
146             =head2 Usage with Older Perls
147            
148             As noted above, some aspects of C will not work with
149             versions of Perl earlier than 5.8.0. This is not due to any problem with this
150             module; it is simply that Perl did not support printing to an in-memory file
151             (C) prior to that point. (Thanks to a CPAN testers
152             report from David Cantrell for identifying this limitation.)
153            
154             =head1 WARNING
155            
156             The syntax and semantics of Perl 6 is still being finalized
157             and consequently is at any time subject to change. That means the
158             same caveat applies to this module.
159            
160             =head1 DEPENDENCIES
161            
162             No dependencies other than on modules included with the Perl core as of
163             version 5.8.0.
164            
165             Some of the files in the test suite accompanying this distribution use
166             non-core CPAN module IO::Capture::Stdout. Tests calling IO::Capture::Stdout
167             methods are enclosed in C blocks and so should pose no obstacle to
168             installation of the distribution on systems lacking IO::Capture. (However,
169             the maintainer strongly recommends IO::Capture for developers who write a lot
170             of test code. So please consider installing it!)
171            
172             =head1 AUTHOR and MAINTAINER
173            
174             =head2 AUTHOR
175            
176             Damian Conway (damian@conway.org).
177            
178             =head2 MAINTAINER
179            
180             Alexandr Ciornii (alexchorny@gmail.com)
181            
182             =head1 ACKNOWLEDGMENTS
183            
184             Thanks to Damian Conway for dreaming this up. Thanks to David A Golden for a
185             close review of the documentation. Thanks to CPAN tester Jost Krieger for
186             reporting an error in my SKIP block count in one test file.
187            
188             =head1 BUGS AND IRRITATIONS
189            
190             As far as we can determine, Perl 5 doesn't allow us to create a subroutine
191             that truly acts like C. That is, one that can simultaneously be
192             used like so:
193            
194             say @data;
195            
196             and like so:
197            
198             say {$fh} @data;
199            
200             Comments, suggestions, and patches welcome.
201            
202             =head1 COPYRIGHT
203            
204             Copyright (c) 2004, Damian Conway. All Rights Reserved.
205             This module is free software. It may be used, redistributed
206             and/or modified under the same terms as Perl itself.
207            
208             =cut