The do construct was something shown to me awhile back by the venerable aristotle. The difference is this, if my mind serves me correctly:-):

With the "_from_file" method, you need to be reading from a disk file.

With the loader method, you can load image data which is in a scalar string, or slurped in from a disk file, often referred to as blobs in ImageMagick terminolgy. ( See Convert Gnome2::Canvas::Pixbuf to Image::Magick Array ). This is often quite handy, like when you pull an image in from the web, and want to display it, without first writing it to a disk file. Like:

my $graph_image = get($http_request_string); $graph_image at this point is a string, with a PNG-looking header. my $loader = Gtk2::Gdk::PixbufLoader->new; $loader->write($png_data); $loader->close; my $pixbuf = $loader->get_pixbuf;

Even though there is Gtk2::Gdk::Pixbuf->new_from_data, it does not automatically handle autodetection of file type as well as the Gtk2::Gdk::PixbufLoader. In other words, the Pixbufloader will autodetect from it's list of recognized types, making it the best method to use if you want an easy load.

Here is a simple way to list the autodetected file types which your Gtk2 build recognizes.

#!/usr/bin/perl use strict; use warnings; use Gtk2; my @formats = Gtk2::Gdk::Pixbuf->get_formats(); my @exts; foreach my $format ( @formats ) { foreach my $key ( keys( %$format ) ) { next unless $key eq 'extensions'; foreach my $elem ( @{ $format->{ $key } } ) { push @exts, $elem; } } } print "@exts\n";
And finally, [the original message from [aristotle]: <p><quote aristotle> OT: I like to partition things like into smaller scopes: my $pixbuf = do { my $loader = Gtk2::Gdk::PixbufLoader->new(); $loader->write( $image_data ); $loader->close(); $loader->get_pixbuf(); }; So the next guy who looks at it doesn't have to wonder if $loader is used anywhere later, and can see that the entire point of this mumbo jumbo is to load something into $pixbuf. Regards, #Aristotle </quote aristotle>
So in summary, if you want to learn one good method for foolproof loading of images, use the pixbufloader construct, it will seldom let you down.

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku

In reply to Re^4: Tk-Karoake Player-w-timidity by zentara
in thread Tk-Karoake Player-w-timidity by zentara

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.