tinita has asked for the wisdom of the Perl Monks concerning the following question:

hello monks,

today i was trying to get Image::Size::imgsize working. i'm using a framework that does a use encoding 'utf8' at the beginning. imgsize() called on a jpeg didn't return the size but said it couldn't find out the image type.
i made up a simple reproducing script which shows that removing the use encoding fixes this:

# image.pm: package image; use strict; use warnings; use Image::Size (); my $img = "/tmp/test.jpg"; no encoding; # this doesn't help my ($x, $y, $i) = Image::Size::imgsize($img); print "$x,$y, $i\n"; 1; # image.pl: #!/usr/bin/perl use strict; use warnings; # comment out next line to get it working use encoding "utf8"; use image;

next i found out was that Image::Size is using AutoLoader.pm, and moving the jpegsize.al code into the module fixed it too. so this only seems to happen with files loaded with AutoLoader. i tried debugging jpegsize.al a bit and found out that there is a call of ord() in the subroutine that returns the correct value normally, but with use encoding 'utf8' it returns 65533 always.

so i'm not sure what happens there, but encoding.pm seems to change the way how .al files work with filehandles.
i tried Devel::Peek to get more information but the strings all looked okay.
now i wonder if this is a bug in AutoLoader.pm, or if it's the way encoding.pm is used by me.

i would be glad for any hints.

update: moving the use encoding after use image fixes it, too (but this can't be a general solution).

update2: tried with perl 5.8.4 and 5.8.8

Replies are listed 'Best First'.
Re: use encoding 'utf8' and AutoLoader
by Joost (Canon) on Oct 02, 2007 at 20:27 UTC
    I've never used "encoding" myself, but you are not the first person I've come across here that has problems with encoding/utf8.

    I would recommend switching from "use encoding 'utf8'" to "use utf8;". *) since it's

    a) lexically scoped (instead of script-scoped) which makes a lot more sense. *)

    b) doesn't appear to have some of the bugs that encoding has that I've seen on this site.

    *) update: this means that you must use utf8; in all source files that are utf-8 encoded, which may be annoying. Perl core- and CPAN modules are usually 7-bit ASCII (or if they're utf-8 encoded they should use utf8), so it should only affect your own modules.

      thanks, i'll try to switch to use utf8. good thing is, the whole app i'm working on is being refactored, so it will be tested anyway.
      still, i wonder which module causes the bug here.