File Coverage

blib/lib/Term/Clear.pm
Criterion Covered Total %
statement 32 32 100.0
branch 8 10 80.0
condition 4 6 66.6
subroutine 6 6 100.0
pod 1 1 100.0
total 51 55 92.7


line stmt bran cond sub pod time code
1             package Term::Clear;
2              
3 2     2   283181 use strict;
  2         13  
  2         55  
4 2     2   10 use warnings;
  2         4  
  2         666  
5              
6             our $VERSION = '0.01';
7             our $POSIX = 0; # off by default since some folks like to avoid loading POSIX
8             our $_clear_str; # our for testing; _ for don’t use this directly
9              
10             sub import {
11 4 100   4   6275 $POSIX = 1 if grep { $_ eq "POSIX" } @_;
  5         22  
12 4         415 return 1;
13             }
14              
15             sub clear {
16 7   66 7 1 24113 $_clear_str //= _get_clear_str();
17 7         283 print $_clear_str;
18             }
19              
20             sub _get_clear_str {
21 6     6   18 local $@;
22              
23 6         13 eval { require Term::Cap };
  6         692  
24 6 50       3002 return _get_from_system_call() if $@; # kind of gross but works a lot of places; patches welcome :)
25              
26             # blatently stolen and slightly modified from PerlPowerTools v1.016 bin/clear
27 6         10 my $OSPEED = 9600;
28 6 50 66     30 if ( $POSIX || $INC{"POSIX.pm"} ) { # only do this if they want it or have already loaded POSIX
29 6         9 eval {
30 6         20 require POSIX;
31 6         34 my $termios = POSIX::Termios->new();
32 6         42 $termios->getattr;
33 6         32 $OSPEED = $termios->getospeed;
34             };
35             }
36              
37 6         28 my $cl = "";
38 6         10 eval {
39 6         33 my $terminal = Term::Cap->Tgetent( { OSPEED => $OSPEED } );
40 4         32 $terminal->Trequire("cl");
41 4         14 $cl = $terminal->Tputs( 'cl', 1 );
42             };
43              
44 6 100       995 return _get_from_system_call() if $cl eq ""; # kind of gross but works a lot of places; patches welcome :)
45              
46 3         20 return $cl;
47             }
48              
49             sub _get_from_system_call {
50 5 100   5   3012 if ( $^O eq 'MSWin32' ) {
51 1         4 return scalar(`cls`);
52             }
53              
54 4         10 return scalar(`/usr/bin/clear`);
55             }
56              
57             1;
58              
59             __END__
60              
61             =encoding utf-8
62              
63             =head1 NAME
64              
65             Term::Clear - `clear` the terminal via a perl function
66              
67             =head1 VERSION
68              
69             This document describes Term::Clear version 0.01
70              
71             =head1 SYNOPSIS
72              
73             use Term::Clear ();
74              
75             Term::Clear::clear();
76              
77             =head1 DESCRIPTION
78              
79             Perl function to replace C<system("clear")>.
80              
81             =head1 INTERFACE
82              
83             =head2 clear()
84              
85             Takes no arguments and clears the terminal screen in as portable way as possible.
86              
87             Once it does all the work to determine the characters for the system the value is cached in memory so subsequent calls will be faster.
88              
89             =head2 POSIX
90              
91             By default it does not try to determine C<OSPEED> from POSIX.
92              
93             This is because some prefer to avoid loading POSIX for various reasons.
94              
95             If you want it to try to do that you have two options:
96              
97             =over 4
98              
99             =item load POSIX.pm before your first call to C<clear()>.
100              
101             =item set C<$Term::Clear::POSIX> to true before your first call to C<clear()>.
102              
103             =item set C<$Term::Clear::POSIX> to true via C<import()>:
104              
105             use Term::Clear 'POSIX';
106              
107             =back
108              
109             =head1 DIAGNOSTICS
110              
111             Throws no warnings or errors of its own.
112              
113             =head1 CONFIGURATION AND ENVIRONMENT
114              
115             Term::Clear requires no configuration files or environment variables.
116              
117             =head1 DEPENDENCIES
118              
119             It will use L<Term::Cap> and L<POSIX::Termios> if available.
120              
121             =head1 INCOMPATIBILITIES AND LIMITATIONS
122              
123             None reported.
124              
125             =head1 BUGS AND FEATURES
126              
127             Please report any bugs or feature requests (and a pull request for bonus points)
128             through the issue tracker at L<https://github.com/drmuey/p5-Term-Clear/issues>.
129              
130             =head1 AUTHOR
131              
132             Daniel Muey C<< <http://drmuey.com/cpan_contact.pl> >>
133              
134             =head1 LICENCE AND COPYRIGHT
135              
136             Copyright (c) 2020, Daniel Muey C<< <http://drmuey.com/cpan_contact.pl> >>. All rights reserved.
137              
138             This module is free software; you can redistribute it and/or
139             modify it under the same terms as Perl itself. See L<perlartistic>.
140              
141             =head1 DISCLAIMER OF WARRANTY
142              
143             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
144             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
145             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
146             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
147             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
148             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
149             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
150             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
151             NECESSARY SERVICING, REPAIR, OR CORRECTION.
152              
153             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
154             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
155             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
156             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
157             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
158             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
159             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
160             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
161             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
162             SUCH DAMAGES.