Code Comments
Programming Forum and web based access to our favorite programming groups.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.
Post Follow-up to this messagethank 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.
>
>
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.