You may want to seperate your initialization from instantiation.

sub new {
        
        my ( $class, $data ) = @_;
        
        my $self = bless {}, $class;
        $self->init();

       return $self;
        
}

sub init {
    my ($self) = @_;
    $self->{image_magick_object} = Image::Magick::new();

    my $error = $data->{image_magick_object}->Read( $data->{path} );
    croak $error if $error;
}

Future subclasses won't need a new sub and supply there own init sub:

sub init {
    my ($self) = @_;
    $self->SUPER::init()

    $self->{'more_data'} = [];
}




On May 2, 2007, at 5:27 PM, Nigel Peck wrote:


Hi all,

I'm new to writing Object Oriented Perl and am hoping for some advice?

I found the need to use Image::Magick tonight and in order to reuse the code in future I put it in a package of subs.

I then thought it seemed like a good opportunity to try writing an OO module so I did.

However I'm not sure that I couldn't do it better by making use of inheritance although I'm not sure how to do it and also looking for any other suggestions.

A cut down version of my code goes like this...

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#!/usr/bin/perl

use strict;
use warnings;

use Carp;

my $error;
my $obj_image;

my $obj_image = MIS::Common::Image_magick->new( { path => '/home/ nigel/scripts/taylor1.jpg' } );
        
$obj_image->resize ( { geometry => '360' } );
$obj_image->crop ( { geometry => '360x480' } );
        
$obj_image->output ( { path => '/home/nigel/scripts/taylor/ thumbnail.jpg' } );

######################################################################

package MIS::Common::Image_magick;

use Image::Magick;

sub new {
        
        my ( $class, $data ) = @_;
        
        $data->{image_magick_object} = Image::Magick::new();
        
        my $error = $data->{image_magick_object}->Read( $data->{path} );
        croak $error if $error;
        
        return bless $data, $class;
        
}

sub output {
        
        my ( $self, $args ) = @_;
        
        my $error = $self->{image_magick_object}->Write( $args->{path} );
        croak $error if $error;
        
}

sub resize {
        
        my ( $self, $args ) = @_;
        
$error = $self->{image_magick_object}->Resize( geometry => $args-> {geometry} );
        croak $error if $error;
        
}

sub crop {
        
        my ( $self, $args ) = @_;
        
$error = $self->{image_magick_object}->Crop( geometry => $args-> {geometry} );
        croak $error if $error;
        
}
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

What could I do better?

TIA.

Cheers,
Nigel


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to