File Coverage

blib/lib/Win32/PrintBox.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Win32::PrintBox;
2            
3 1     1   14934 use warnings;
  1         1  
  1         33  
4 1     1   4 use strict;
  1         1  
  1         33  
5 1     1   242 use Win32;
  0            
  0            
6             use Win32::Console::ANSI qw/ Cursor /;
7             require Exporter;
8             our @ISA = qw( Exporter );
9             our @EXPORT_OK = qw(set_page_size get_page_size);
10            
11             our $VERSION = sprintf "%d.%03d", q$Revision: 0.002 $ =~ /: (\d+)\.(\d+)/;
12            
13             our @COLLECTED_TEXT = ();
14             our $DEFAULT_PAGE_SIZE = 25;
15             our $PAGE_SIZE = $DEFAULT_PAGE_SIZE;
16             our $PAGE_NUM = 1;
17             our ($OLD_WARN, $OLD_DIE);
18             our ($WinMode, $CMDExists);
19             init();
20            
21             END {
22             pause();
23             }
24            
25             ##################################
26             # Put module initialization in a sub, so it can be called in testing
27             sub init {
28             $WinMode = $^X =~ m/wperl/i;
29            
30             # Use fileno(STDOUT) to see if STDOUT is open (-1 for wperl)
31            
32             $CMDExists = CMD_Cursor();
33             # DIE handler
34             # display the error message, and let END block pause, if needed
35             # Use __WARN__ handler, if it has been created for windows messages
36             $OLD_DIE = $SIG{__DIE__};
37             $SIG{__DIE__} = sub {
38             warn $_[0];
39             $OLD_DIE->(@_) if $OLD_DIE; # invoke the old handler
40             exit;
41             };
42             if ($WinMode) {
43             tie *MSG_FH, 'Win32::PrintBox::IO';
44             select MSG_FH;
45             }
46             }
47             ##################################
48             sub pause {
49             return if $WinMode || $CMDExists;
50             # Otherwise, ask the user for permission to close, so any output is acknowledged
51             $|++; # Flush any pending output
52             print STDERR "Press Enter to continue\n";
53             pause_in();
54             }
55             ##################################
56             # put this in a subroutine, so we can override it for testing
57             sub pause_in {
58             ;
59             }
60             ############]######################
61             sub CMD_Cursor {
62             # If a new command prompt window is opened, then the 'y' cursor position will be at the top
63             # This can be faked by running cls & my_perl_script.pl
64             my ($x, $y) = Cursor(); # reads cursor position
65             return $y>1; # Home is 1,1
66             }
67            
68             ##################################
69             # Accessor for number or elements to cache in @COLLECTED_TEXT
70             sub get_page_size {
71             return $PAGE_SIZE;
72             }
73             ##################################
74             # Mutator for number or elements to cache in @COLLECTED_TEXT
75             sub set_page_size {
76             my $temp = shift // $DEFAULT_PAGE_SIZE;
77             $PAGE_SIZE = $temp;
78             }
79            
80             ##################################
81             ##################################
82             package Win32::PrintBox::IO;
83            
84             use constant VB_OK => 0;
85             use constant VB_OK_CANCEL => 1;
86             use constant VB_CANCEL => 2;
87            
88             ##################################
89             sub new {
90             my $self = bless {}, shift;
91             # Using a "__WARN__" handler provides the way to re-direct warn statements to a MsgBox
92             $OLD_WARN = $SIG{__WARN__};
93             $SIG{__WARN__} = sub { WarnBox(@_) };
94             return $self;
95             }
96            
97             ##################################
98             sub WarnBox { # Send Warnings immediately
99             my $msg = join("\n", @_);
100             return unless $msg ne '';
101             my $answer = Win32::MsgBox($msg, VB_OK_CANCEL, 'Warning!');
102             $OLD_WARN->(@_) if $OLD_WARN; # invoke the old handler
103             die if $answer == VB_CANCEL;
104             }
105            
106             ##################################
107             sub PrintMsgBox {
108             my $self = shift;
109             push (@COLLECTED_TEXT, @_);
110             if ($#COLLECTED_TEXT >= $PAGE_SIZE - 1) {
111             my $answer = Win32::MsgBox( join('', @COLLECTED_TEXT), VB_OK_CANCEL, 'Page ' . $PAGE_NUM++);
112             @COLLECTED_TEXT = ();
113             die if $answer == VB_CANCEL;
114             }
115             }
116            
117             sub TIEHANDLE { shift->new(@_) }
118             sub PRINT { shift->PrintMsgBox(@_) }
119             sub PRINTF { shift->PrintMsgBox(sprintf(@_)) }
120             # DESTROY only applies in WinMode; in DOS mode, we didnt tie anything, so nothing to destroy
121             sub DESTROY { Win32::MsgBox( join('', @COLLECTED_TEXT), VB_OK, 'Done') if @COLLECTED_TEXT}
122            
123             1;
124             __END__