For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2004 > using Class::Struct in a module









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 using Class::Struct in a module
Christopher J. Bottaro

2004-08-04, 8:55 pm

package My::Module;
use Class::Struct;

struct Blah => {
field1 => '$',
field2 => '$'
};

sub new {
# normal constructor
}

sub f {
my $s = new Blah; # this calls new() defined above
$s->field1 = 1;
$s->field2 = 2;
return $s;
}

package main;

my $obj = new My::Module;
my $s = $obj->f();
print "$s->field1\n$s->field2\n";

---------------------------------------------

ok, the problem is that in My::Module::f(), the statement my '$s = new Blah'
calls My::Module::new() instead of constructing a struct of type Blah. how
can i get this code to perform the way i want?

thank you for the help.

Christopher J. Bottaro

2004-08-05, 3:56 pm

thank you. i guess when i was saying 'my $s = new FHEAD', it was calling
the new() in the current package. changing it to 'my $s = FHEAD->new()'
forces it to use FHEAD's new() method.

NYIMI Jose (BMB) wrote:

>
>
>
> use strict;
> use warnings;
>
>
> I suppose you have some code in the above new() :-)
>
>
> The syntax should be:
> $s->field1(1);
> $s->field2(2);
>
>
>
> Better putting $s outside of double quote:
> print $s->field1."\n".$s->field2."\n";
>
>
> Below is your code reviewed:
>
> package My::Module;
> use strict;
> use warnings;
> use Class::Struct;
>
> struct( Blah => { field1 => '$', field2 => '$'} );
>
> sub new {
> my($caller)=@_;
> print "normal constructor called!\n";
> my $class=ref $caller || $caller;
> my $self = {};
> bless $self => $class;
> }
>
> sub f {
> my $s = Blah->new();
> $s->field1(1);
> $s->field2(2);
> return $s;
> }
>
> package main;
>
> my $obj =My::Module->new();
> my $s = $obj->f();
>
> print $s->field1."\n".$s->field2."\n";
>
>
> OUTPUT:
>
> normal constructor called!
> 1
> 2
>
>
> HTH,
>
> José.
>
>
> **** DISCLAIMER ****
>
> "This e-mail and any attachment thereto may contain information which is
> confidential and/or protected by intellectual property rights and are
> intended for the sole use of the recipient(s) named above. Any use of the
> information contained herein (including, but not limited to, total or
> partial reproduction, communication or distribution in any form) by other
> persons than the designated recipient(s) is prohibited. If you have
> received this e-mail in error, please notify the sender either by
> telephone or by e-mail and delete the material from any computer".
>
> Thank you for your cooperation.
>
> For further information about Proximus mobile phone services please see
> our website at http://www.proximus.be or refer to any Proximus agent.
>
>



Sponsored Links







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

Copyright 2008 codecomments.com