Playing with perl

Ok, so I got bored and wound up with a new toy 😛

Hmm, now to hunt down some snacks…


1: #!/usr/bin/perl
2:
3: use strict;
4: use warnings;
5:
6: use Getopt::Long;
7: use Image::Size;
8: use Pod::Usage;
9:
10: =pod
11: =head1 NAME
12:
13: image-size.pl
14:
15: =head1 SYNOPSIS
16:
17: image-size.pl [options] [file ...]
18:
19: Options:
20: -f, --ask-file include output from file(1)
21: -h, --help print usage help info
22: --man read manual page
23: -s, --silent ignore errors
24: -v, --verbose more verbose output
25: -d, --die blow up on first error
26:
27: =head1 DESCRIPTION
28:
29: A quick perl script to do what I've always wanted, tell me the height and width
30: of an image file. Without having to open a graphical program (X11) just for the
31: sake of finding out! File formats supported are based on the Image::Size module
32: which is required for this script to function.
33:
34: Special thanks to the creators of the llama book for mentioning perls
35: Image::Size module and thanks to the creators of that module!
36:
37: =head1 OPTIONS
38:
39: =over 8
40:
41: =item B<-f, --ask-file>
42:
43: Politely ask the systems file utility about the files format. This option
44: requires the file program installed and accessible through your environments
45: PATH.
46:
47: =item B<-h, --help>
48:
49: Print out a summery of command line options and exit.
50:
51: =item B<--man>
52:
53: Displays this manual page using the provided Plain Old Documentation.
54:
55: =item B<-s, --silent>
56:
57: Ignore failed files and continue, combine with -v or --verbose to include the
58: file name but still skip printing error messages.
59:
60: =item B<-v, --verbose>
61:
62: Print the file name along with it's width, height, and type (if known). Each
63: field is also separated by a new line and ordered in a more elaborate format.
64:
65: =item B<-d, --die>
66:
67: Default behavior for image-size.pl is to print a simple warning message if any
68: specified file can not be operated on.
69:
70: When the the -d or --die switches are given, the program will halt execution
71: with an appropriate exit status instead of continuing.
72:
73: This is useful for when you do not wish to continue after an error when
74: processing a list of files
75:
76: Refer to the perl documentation for details about how the exit status is
77: affected.
78:
79: =back
80:
81: =head1 EXIT STATUS
82:
83: The image-size.pl utility exits 0 on success or returns via perls die() if -d or
84: --die was passed on the command line.
85:
86: =head1 SEE ALSO
87:
88: L<perl(1)>, L<perldoc(1)>, L<file(1)>
89:
90: =head1 LICENSE
91:
92: Copyright (c) CLASS=integer>2008, TerryP <snip>
93:
94: Permission to use, copy, modify, and distribute this software for any purpose
95: with or without fee is hereby granted, provided that the above copyright notice
96: and this permission notice appear in all copies.
97:
98: THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
99: REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
100: FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
101: INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
102: OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
103: TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
104: THIS SOFTWARE.
105:
106: =cut
107:
108:
109: # message for pod2usage()
110: my $usage_msg = "$0 -- figure out the height and width of image filesn";
111:
112: # message to display on error getting the image size
113: my $warn_msg = "File does not exist or cannot be opened: ";
114:
115: my ($deadly, $help, $verbose, $man, $silent, $ask) = undef;
116:
117: {
118: Getopt::Long::Configure('bundling');
119: GetOptions(
120: 'f|ask-file' => $ask,
121: 'h|help|?' => $help,
122: 'man' => $man,
123: 's|silent' => $silent,
124: 'v|verbose' => $verbose,
125: 'd|die' => $deadly,
126: ) or $help++;
127:
128: pod2usage(-msg => $usage_msg, -output => *STDOUT,
129: -exitval => 1, -verbose => 0 ) if $help;
130: pod2usage(-verbose => 2, -exitval => 1) if $man;
131:
132: exit 1 unless @ARGV;
133:
134: # check if we are reading file names off stdin
135: if ($ARGV[0] eq '-') {
136: while (<>) {
137: chomp;
138: &print_size(imgsize($_), $_)
139: if -f $_ or $silent ? next : &handle_error and next;
140: }
141: } else {
142: foreach (@ARGV) {
143: &print_size(imgsize($_), $_)
144: if -f $_ or $silent ? next : warn $warn_msg."$_n" and next;
145: }
146: }
147: }
148:
149: sub print_size() {
150: my ($x, $y, $type, $file) = @_;
151:
152: $x = 'unkown' unless $x;
153: $y = 'unkown' unless $y;
154:
155: # keep it simple stupid
156: my $std_msg = "width-x: $xtheight-y: $ytfile type: $typen";
157:
158: # unless asked to shoot off your mouth
159: my $verb_msg = "file name: $filen" .
160: "width-x: $xnheight-y: $yn" .
161: "file type: $typenn";
162:
163: $verbose ? print $verb_msg : print $std_msg;
164:
165: print "running file(1) ...nn",`file $_`,"n" if $ask;
166: }
167:
168: sub handle_error() {
169: $deadly ? die $! : warn $warn_msg."$_n";
170: }
171: