
POE::Component::App::PNGCrush - non-blocking wrapper around App::PNGCrush

use strict;
use warnings;
use POE qw(Component::App::PNGCrush);
my $poco = POE::Component::App::PNGCrush->spawn;
POE::Session->create(
package_states => [ main => [qw(_start crushed)] ],
);
$poe_kernel->run;
sub _start {
$poco->run( {
in => [ qw(file1.png file2.png file3.png) ],
options => [
qw( -d OUT_DIR -brute 1 ),
remove => [ qw( gAMA cHRM sRGB iCCP ) ],
],
event => 'crushed',
}
);
}
sub crushed {
my $in_ref = $_[ARG0];
my $proc_ref = $in_ref->{out};
for ( keys %$proc_ref ) {
if ( exists $proc_ref->{$_}{error} ) {
print "Got error on file $_ : $proc_ref->{$_}{error}\n";
}
else {
printf "Stats for file %s\n\tSize reduction: %.2f%%\n\t"
. "IDAT reduction: %.2f%%\n",
$_, @{ $proc_ref->{$_} }{ qw(size idat) };
}
}
$poco->shutdown;
}
Using event based interface is also possible.

The module is a non-blocking wrapper around App::PNGCrush which provides interface to ``pngcrush'' program. See documentation for App::PNGCrush regarding information on how to obtain ``pngcrush'' program.
You should familiarize yourself with new(), run() and set_options() methods of App::PNGCrush module to fully understand the workings of this POE wrapper (although, I'll point you to those in the right place throughout this document)

spawn my $poco = POE::Component::App::PNGCrush->spawn;
POE::Component::App::PNGCrush->spawn(
alias => 'crush',
obj_args => {
max_time => 600,
},
options => {
debug => 1,
trace => 1,
# POE::Session arguments for the component
},
debug => 1, # output some debug info
);
The spawn method returns a POE::Component::App::PNGCrush object. It takes a few arguments, all of which are optional. The possible arguments are as follows:
alias POE::Component::App::PNGCrush->spawn(
alias => 'crush'
);
Optional. Specifies a POE Kernel alias for the component.
obj_args POE::Component::App::PNGCrush->spawn(
obj_args => { max_time => 600 },
);
Optional. Takes a hashref as an argument. If specified that hashref will be directly dereferenced into App::PNGCrush's constructor (new() method). See documentation for App::PNGCrush regarding valid values.
options my $poco = POE::Component::App::PNGCrush->spawn(
options => {
trace => 1,
default => 1,
},
);
Optional. A hashref of POE Session options to pass to the component's session.
debug my $poco = POE::Component::App::PNGCrush->spawn(
debug => 1
);
When set to a true value turns on output of debug messages. Defaults to: 0.

run $poco->run( {
event => 'event_for_output',
in => [ qw(file1.png file2.png file3.png) ],
options => [
qw( -d OUT_DIR -brute 1 ),
remove => [ qw( gAMA cHRM sRGB iCCP )
],
_blah => 'pooh!',
session => 'other',
}
);
Takes a hashref as an argument, does not return a sensible return value. See run event's description for more information.
session_idmy $poco_id = $poco->session_id;
Takes no arguments. Returns component's session ID.
shutdown$poco->shutdown;
Takes no arguments. Shuts down the component.

run $poe_kernel->post( crush => run => {
event => 'event_for_output',
in => [ qw(file1.png file2.png file3.png) ],
options => [
qw( -d OUT_DIR -brute 1 ),
remove => [ qw( gAMA cHRM sRGB iCCP )
],
_blah => 'pooh!',
session => 'other',
}
);
Instructs the component to perform "crushing" of png files. Takes a hashref as an argument, the possible keys/value of that hashref are as follows:
event { event => 'results_event', }
Mandatory. Specifies the name of the event to emit when results are ready. See OUTPUT section for more information.
in { in => 'file.png' }
{ in => [ qw(file1.png file2.png file3.png) ] }
Mandatory. Takes either a scalar containing a filename of an image you want to "crush" or an arrayref of filenames (if more than one).
options {
options => [
qw( -d OUT_DIR -brute 1 ),
remove => [ qw( gAMA cHRM sRGB iCCP )
],
}
Mandatory. Takes an arrayref as a value which will be directly dereferenced into set_options() method of App::PNGCrush. See documentation for App::PNGCrush's set_options() method for possible values.
session { session => 'other' }
{ session => $other_session_reference }
{ session => $other_session_ID }
Optional. Takes either an alias, reference or an ID of an alternative session to send output to.
{
_user => 'random',
_another => 'more',
}
Optional. Any keys starting with _ (underscore) will not affect the component and will be passed back in the result intact.
shutdown$poe_kernel->post( EXAMPLE => 'shutdown' );
Takes no arguments. Tells the component to shut itself down.

$VAR1 = {
'out' => {
'p2.png' => {
'msg' => undef,
'total_idat_length' => '1880',
'stderr' => '',
'cpu' => {
'decoding' => '0.030',
'other' => '0.090',
'total' => '0.200',
'encoding' => '0.080'
},
'status' => '0',
'idat' => '0.80',
'stdout' => 'stdout from pngcrush will be here',
'size' => '1.56'
},
'p.png' => {
'msg' => undef,
'total_idat_length' => '1880',
'stderr' => '',
'cpu' => {
'decoding' => '0.040',
'other' => '0.030',
'total' => '0.200',
'encoding' => '0.130'
},
'status' => '0',
'idat' => '0.80',
'stdout' => 'pngcrush 1.6.4 blah blah',
'size' => '1.56'
}
},
'options' => [
'-d',
'OUT_DIR',
'-brute',
'1',
'remove',
[
'gAMA',
'cHRM',
'sRGB',
'iCCP'
]
],
'in' => [
'p.png',
'p2.png'
],
'_blah' => 'pooh!',
};
The event handler set up to handle the event which you've specified in the event argument to run() method/event will recieve input in the $_[ARG0] in a form of a hashref. The possible keys/value of that hashref are as follows:
outThe out key will have a hashref as a value. The keys of that hashref will be filenames of the files which you gave as in argument to run() event/method. The values for each of these keys will either be a scalar containing an error message (if an error occured) or a hashref containing the exact same hashref run() method of App::PNGCrush module would return. For explanation of keys/values of that hashref see run() method in documentation for App::PNGCrush module.
run() 'options' => [
'-d',
'OUT_DIR',
'-brute',
'1',
'remove',
[
'gAMA',
'cHRM',
'sRGB',
'iCCP'
]
],
'in' => [
'p.png',
'p2.png'
],
Valid arguments to run() event/method (that is the options and in arguments) will be present in the output as well.
{ '_blah' => 'foos' }
Any arguments beginning with _ (underscore) passed into the EXAMPLE() event/method will be present intact in the result.


Zoffix Znet, <zoffix at cpan.org> (http://zoffix.com, http://haslayout.net)

Please report any bugs or feature requests to bug-poe-component-app-pngcrush at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=POE-Component-App-PNGCrush. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

You can find documentation for this module with the perldoc command.
perldoc POE::Component::App::PNGCrush
You can also look for information at:
http://rt.cpan.org/NoAuth/Bugs.html?Dist=POE-Component-App-PNGCrush

Copyright 2008 Zoffix Znet, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.