blib/lib/Printer/HP/Display.pm

filter: Statementstmt 32.6% 17 / 52 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 5 / 10 Podpod 100% 4 / 4 totaltotal 35.1% 26 / 74 CC 7 SCAR 17.2
CC
7
Cov
28%
get_display
12.0
new
6.0
set_display
6.0
CRAP 5.6
↑ Summary
1 package Printer::HP::Display;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 PJL_PORT => 9100,
10 ESC => "\033",
11 2 };
12  
13 =head1 NAME
14  
15 Printer::HP::Display - Change the default ready message on your HP laser printer
16  
17 =head1 VERSION
18  
19 Version 0.01
20  
21 =cut
22  
23 2 our $VERSION = '0.01';
24  
25  
26 =head1 SYNOPSIS
27  
28 This module allows you to change the value of the ready message (usually 'Ready') on the tiny LCD display that practically all HP laser printers have. You can also retrieve the value of the currently set message. The module communicates with the printer using Printer Job Language (PJL). See: http://en.wikipedia.org/wiki/Printer_Job_Language
29  
30 At the moment this module is just a fun project; somewhat on the lines of ACME::LOLCAT. For example, at Cricinfo we use it to show cricket scores on our printer screen (http://twitpic.com/26yt2d). You should be careful with what you do to the printers at your office - not all IT managers have a funny bone :-).
31  
32 Here's how you'd use it in you code:
33  
34 use Printer::HP::Display;
35  
36 my $printer_ip = "192.168.0.1";
37 my $printer = Printer::HP::Display->new($printer_ip);
38  
39 my $message = "I am ready. Are you?";
40  
41 $printer->set_display($message);
42  
43 print $printer->get_display; #currently set message
44 print $printer->get_status; #complete dump of PJL INFO STATUS command
45  
46 =head1 SUBROUTINES/METHODS
47  
48 =head2 new()
49  
50 Create a Printer::HP::Display object.
51  
52 =cut
53  
54 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
55 0 ▶ die 'Usage: Printer::HP::Display->new($printer_host_or_ip)' unless $#_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $#_ == 1 0 0 0
56  
57 0 my $class = shift;
58 0 my ($host) = @_;
59 0 bless { _host => $host }, $class;
60 }
61  
62 =head2 set_display($message)
63  
64 Set the ready message on the printer's display to something of your choice. The string must be pure ASCII - you'll get ? in place of characters that are not ASCII. At the moment set_display doesn't check the length of the string. Anything between 20-50 is a good idea but check your printer's display and tweak accordingly. Some models will truncate the string to fit the available space others will simply refuse to set it.
65  
66 =cut
67  
68 0 ▶sub set_display {
Subroutine set_display: not called CC=2 SCAR=17.9
Pod documented
69 0 ▶ die 'Usage: $obj->set_display("string")' unless $#_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $#_ == 1 0 0 0
70  
71 0 my $self = shift;
72 0 my ($message) = @_;
73  
74 0 my $send_string = ESC . '%-12345X@PJL RDYMSG DISPLAY = "' . $message . "\"\r\n";
75 0 $send_string = $send_string . ESC . '%-12345X' . "\r\n";
76  
77 0 my $printer_string = encode("ascii", $send_string);
78  
79 0 my $sock = _socket($self->{_host});
80 0 $sock->send($printer_string);
81 0 $sock->close;
82 }
83  
84 =head2 get_display()
85  
86 Get the currently set ready message.
87  
88 =cut
89  
90 0 ▶sub get_display {
Subroutine get_display: not called CC=3 SCAR=24.8
Pod documented
91  
92 0 my $self = shift;
93 0 my @status = $self->get_status;
94  
95 0 my $display = "";
96
97 0 for my $status (@status) {
98 0 ▶ if($status =~ /DISPLAY=\"(.*)\"/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status =~ /DISPLAY=\"(.*)\"/g) 0 0 0
99 0 $display = $1;
100 0 last;
101 }
102 }
103  
104 0 return $display;
105 }
106  
107 =head2 get_status()
108  
109 Get a raw dump of the PJL INFO STATUS command. Returns an array with one element per line of message received from the printer.
110  
111 =cut
112  
113 0 ▶sub get_status {
Subroutine get_status: not called CC=2 SCAR=17.9
Pod documented
114  
115 0 my $self = shift;
116 0 my $send_string = "\@PJL INFO STATUS\r\n";
117  
118 0 my $printer_string = encode("ascii", $send_string);
119  
120 0 my $sock = _socket($self->{_host});
121 0 $sock->send($printer_string);
122
123 0 my @status = ();
124  
125 0 for (0..3) {
126 0 my $read = <$sock>;
127 0 push @status, $read;
128 }
129  
130 0 $sock->close;
131  
132 0 return @status;
133 }
134  
135 0 ▶sub _socket {
Subroutine _socket: not called CC=2 SCAR=17.9
136 0 my $host = shift;
137  
138 0 ▶ my $sock = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
my $sock = "IO::Socket::INET"->new("PeerAddr", $host, "PeerPort", 9100, "Proto", "tcp") or die $! 0 0 0
139 PeerAddr => $host,
140 PeerPort => PJL_PORT,
141 Proto => 'tcp'
142 ) or die $!;
143  
144 0 return $sock;
145 }
146 =head1 AUTHOR
147  
148 Deepak Gulati, C<< <deepak.gulati at gmail.com> >>
149  
150 =head1 BUGS
151  
152 Please report any bugs or feature requests to C<bug-printer-hp-display at rt.cpan.org>, or through
153 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Printer-HP-Display>. I will be notified, and then you'll
154 automatically be notified of progress on your bug as I make changes.
155  
156  
157  
158  
159 =head1 SUPPORT
160  
161 You can find documentation for this module with the perldoc command.
162  
163 perldoc Printer::HP::Display
164  
165  
166 You can also look for information at:
167  
168 =over 4
169  
170 =item * RT: CPAN's request tracker
171  
172 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Printer-HP-Display>
173  
174 =item * AnnoCPAN: Annotated CPAN documentation
175  
176 L<http://annocpan.org/dist/Printer-HP-Display>
177  
178 =item * CPAN Ratings
179  
180 L<http://cpanratings.perl.org/d/Printer-HP-Display>
181  
182 =item * Search CPAN
183  
184 L<http://search.cpan.org/dist/Printer-HP-Display/>
185  
186 =back
187  
188  
189 =head1 ACKNOWLEDGEMENTS
190  
191 Inspired by Scott Allen's article and C# code at: http://odetocode.com/humor/68.aspx
192  
193 =head1 LICENSE AND COPYRIGHT
194  
195 Copyright 2010 Deepak Gulati.
196  
197 This program is free software; you can redistribute it and/or modify it
198 under the terms of either: the GNU General Public License as published
199 by the Free Software Foundation; or the Artistic License.
200  
201 See http://dev.perl.org/licenses/ for more information.
202  
203  
204 =cut
205  
206 2 1; # End of Printer::HP::Display
↑ Summary