develooper Front page | perl.moose | Postings from July 2015

PerlMagick and Moo

Thread Next
From:
Rick Leir
Date:
July 20, 2015 15:35
Subject:
PerlMagick and Moo
Message ID:
CAGitpvb4+Wy6OzuSgLj+z8Rf9bEFYawhNfJm+0uE7Cg-j_9qXw@mail.gmail.com
Hi everyone,

I have been working on a web service for GM PerlMagick, and Moo was
suggested. The GM object is kept across requests, but the image memory is
freed (question below). Here is what it looks like:
package Something::Magick;

use Graphics::Magick;

use Dancer::Core::Types; # brings helper for types
use MooX::Types::MooseLike::Base; # InstanceOf
use MooX::Types::MooseLike;

# Moo def must follow other defs.
use Moo;
with 'MooX::Singleton';

has image => (
    is => 'rw',
    isa => InstanceOf['Graphics::Magick'],
    lazy => 1,
    builder => '_build_image'
);
# The lazy attribute says to Moo that this attribute will be built
(initialized)
# only when called the first time. It means that the connection to GmImage
won't be opened until necessary.

sub _build_image {
    my ($self) = @_;
    Graphics::Magick->new( );
}
sub BUILD {
    my ($self) = @_;
    $self->image( Graphics::Magick->new( ));
}

sub do_sequence {
    my $self = shift;
    my @seq = shift || 1;

    my $Arrayofhashes = \$seq[0][0];
    my $sizeminus1 = @$$Arrayofhashes - 1; #++++++++++++++ 3

    for my $i ( 0 .. $sizeminus1 ) {
        my $href = $seq[0][0][$i];

        my $opname;
        my $filepath;
        my $gmparms;
        while( my ($k, $v) = each %$href ) {
.... sanitize input ..
           if( $k eq 'op') {
                $opname = $v;
            }
            elsif( $k eq 'file') {
                $v =~ s/^\/+//;  # remove any leading /
                $filepath = $v;
            }
            elsif( $k eq 'parms') {
                $gmparms = $v;
            }
        }
..
               if( $opname eq 'Read') {
                    my $status = $self->image->Read( $absolute);
                    warn "$status" if "$status";
                } elsif( $opname eq 'Write') {
                    my $status = $self->image->Write( $absolute);
                    warn "$status" if "$status";
                }
  ...
           # some op other than read,write
               # invoke it
                my $status = $self->image->$opname( $gmparms);
                warn "$status" if "$status";
            }

    # delete all the images but retain the Graphics::Magick object
    # @$image = ();  no
    # @self->image = ();  no
    # @{self->image} = ();  no

    my $i = 0;
#    while (defined ($self->image->[$i])) {
    while ( $i < 10) {
        if (defined ($self->image->[$i])) {
            undef ($self->image->[$i]);
            print "======== undef $i\n";
        }
        $i++;
    }
}
My question is how can you delete all the images but retain the
Graphics::Magick object? I do not want to use the undef loop above. With
normal perlmagick, you would say
  @$image = ();
( http://www.graphicsmagick.org/perl.html )

Should I learn more about Perl XS?  TIA
-- 
Rick

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About