For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > December 2004 > subclassing (?) package









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author subclassing (?) package
JupiterHost.Net

2004-12-19, 8:55 pm

Hello group,

I'm trying to subclass a module (at least I think its what is called
subclassing...)

This example below works but I'd like to package GD::Image::Foo in its
own module, how would I go about doing that? perldoc perlmod and
perlmodlib didn't seem to address that issue unless of course I'm
which is most likely the case ;p

#!/usr/bin/perl

use strict;
use warnings;
use GD;

my $image = GD::Image->new(42,21);
my $white = $image->colorAllocate(255,255,255);
$image->transparent($white);


$image->Foo(1,5,9);

sub GD::Image::Foo {
my $im = shift;
# use @_ and $im-> here
}

I'd like that to be:

#!/usr/bin/perl

use strict;
use warnings;
use GD;
use GD::Image::Foo; # has sub GD::Image::Foo {} in it ...

my $image = GD::Image->new(42,21);
my $white = $image->colorAllocate(255,255,255);
$image->transparent($white);

$image->Foo(1,5,9);

Any docs/ideas most appreciated!

TIA
Bob Showalter

2004-12-20, 3:55 am

JupiterHost.Net wrote:
> Hello group,
>
> I'm trying to subclass a module (at least I think its what is called
> subclassing...)
>
> This example below works but I'd like to package GD::Image::Foo in its
> own module, how would I go about doing that? perldoc perlmod and
> perlmodlib didn't seem to address that issue unless of course I'm
> which is most likely the case ;p
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
> use GD;
>
> my $image = GD::Image->new(42,21);
> my $white = $image->colorAllocate(255,255,255);
> $image->transparent($white);
>
>
> $image->Foo(1,5,9);
>
> sub GD::Image::Foo {
> my $im = shift;
> # use @_ and $im-> here
> }
>
> I'd like that to be:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
> use GD;
> use GD::Image::Foo; # has sub GD::Image::Foo {} in it ...
>
> my $image = GD::Image->new(42,21);
> my $white = $image->colorAllocate(255,255,255);
> $image->transparent($white);
>
> $image->Foo(1,5,9);


Well, you can't do it exactly like that. What you want is to create a new
class (say, My::GD::Image) that extends GD::Image like this:

package My::GD::Image;

use base qw(GD::Image);

sub Foo {
my $image = shift;
...blah...
}

1;

Then in the main program...

use My::GD::Image;

my $image = My::GD::Image->new(42,21);
my $white = $image->colorAllocate(255,255,255);
$image->transparent($white);
$image->Foo(1,5,9);

HTH

Philipp Traeder

2004-12-20, 3:55 am

On Sunday 19 December 2004 20:48, JupiterHost.Net wrote:
> Hello group,


Hi,

> I'm trying to subclass a module (at least I think its what is called
> subclassing...)


I'm not sure, but I think subclassing is the appropriate term for this ;-)

> This example below works but I'd like to package GD::Image::Foo in its
> own module, how would I go about doing that? perldoc perlmod and
> perlmodlib didn't seem to address that issue unless of course I'm
> which is most likely the case ;p
>


I think what you want to do is to create an own class that inherits from
GD::Image. In Perl (5), a class is basically a package, so you've been on the
right track when you wrote this sub starting with

sub GD::Image::Foo {
...

Now just create a new file called Foo which you put into the folder GD/Image,
so that it can be found by perl.

This class should probably look similar to this (UNTESTED):

########################################
######
#!/usr/bin/perl
# file: GD/Image/Foo.pm

# the package name is the class name
package GD::Image::Foo;

use strict;
use warnings;

use GD;

# this declares that you want to inherit from GD::Image
our @ISA = qw(GD::Image);

sub Foo {
my $self = shift;

# work with the image
}

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

Now you should be able to use it instead of the usual image:

[..]
> I'd like that to be:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
> #use GD;

I think you don't need this line any more because of the next one.

> use GD::Image::Foo; # has sub GD::Image::Foo {} in it ...
>
> # my $image = GD::Image->new(42,21);

You've got to instantiate an object of your new class instead - try:

my $image = GD::Image::Foo -> new(42,21);

> my $white = $image->colorAllocate(255,255,255);
> $image->transparent($white);
>
> $image->Foo(1,5,9);
>
> Any docs/ideas most appreciated!
>
> TIA


I'm not very experienced with OOP in perl, but I think this should be it - if
you got the Camel book at hand, take a look into the OOP chapter. It's very
comprehensible (and funny). If you do not have the camel book, give it to
yourself as a christmas present ;-)

HTH,

Philipp
JupiterHost.Net

2004-12-20, 3:55 am

Thanks Bob and Philipp for the info, very very helpful and gives me the
perfect place to start looking and playing further!

You guys rock as much as Perl itself ;p
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com