As side notes:
#!/usr/bin/perl -w use strict;
Notice that nowadays (unless you have serious backward compatibility issues) it is recommended to
use warnings;
instead. Since when I made a similar remark somebody popped out asking why I thought the latter was a better alternative, I'll point out in advance that no, I'm not explaining it. perldoc warnings and perldoc perlrun give some clues about it.
my $jpg = shift or die "$!\n";
Huh?!? $! is meaningful only after a failure of a system call (well, "system library"). At this point it may be anything...
my $data = get_file($jpg); my @res = sizeJPG($data); print "$jpg width $res[0] height $res[1]\n";
Please, do not misunderstand me: I know that this is a minimal example, but why do you put the return values of your sub into an array only to later use its entries separately? I.e., wouldn't have
my $data = get_file($jpg); my ($h,$v) = sizeJPG($data); print "$jpg width $h height $v\n";
been clearer?

Also, in case the regex below is not matched, the return values will be undef, and this will issue a warning in the print statement above. And if you locally disable 'uninitialized' warnings, then you'll get an IMHO inconsistent output. You may want to treat this case in a different way...

sub sizeJPG { return unless $_[0]; my ( $width, $height ); ( $height, $width ) = unpack( "nn", $1 ) if $_[0] =~ /\xFF\xC0...(....)/; return ( $width, $height ); }
I like to do
local $_=shift;
if possible, but that's just a personal preference.

As a general remark you can also use the return value of the match operator, but indeed in this case it can be slightly awkward:

my ($width,$height)=unpack "nn", (/\xFF\xC0...(....)/)[0];
and it will also issue a warning if the regex is not matched. So a possibly better solution may be:
my ($width,$height)=unpack "nn", /\xFF\xC0...(....)/ ? $1 : ''; # or "\0" x 4, maybe?
Let's go on...
sub get_file { open FILE, $_[0] or die $!; binmode FILE; local $/; my $data = <FILE>; close FILE; return $data; }
Why not
sub get_file { local $/; open my $fh, '<:raw', shift or die $!; <$fh>; }
instead?

(But then I'd probably use a do block. In the meantime we continue to wait for Perl6's .slurp!!)


In reply to Re^2: getting the dimensions of a picture (jpeg) by blazar
in thread getting the dimensions of a picture (jpeg) by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.