CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Moose and arrays

 



waldm
New User

May 30, 2014, 5:17 AM

Post #1 of 3 (4748 views)
Moose and arrays Can't Post

Hi,

I have previous experience in other programming languages (Java, C#, Python), and just started learning Perl a couple of days ago.

I'm trying to write a basic calculator, and so far have the following code:

main.pl

Code
  
use Calculator;



my $calculator = Calculator->new();

$calculator->add(1, 2);

$calculator->add(3, 2);

my $calculations = $calculator->calculations;



print "The calculator has [@$calculations].\n";


Calculation.pm

Code
package Calculation; 

use Moose;



has 'expression' => (

is => 'rw',

isa => 'Str',

required => 1,

);



has 'result' => (

is => 'rw',

isa => 'Num',

required => 1,

);



1;


Calculator.pm

Code
package Calculator; 

use Moose;

use Calculation;





has 'calculations' => (

is => 'rw',

isa => 'ArrayRef[Calculation]',

);



sub add {

my $self = shift;

my @calculations = $self->calculations;



my $x = $_[0];

my $y = $_[1];



my $calculation = Calculation->new(

expression => "$x+$y",

result => $x + $y);



push(@calculations, $calculation);

$self->calculations(\@calculations);

}



1;


but the following error occurs:

Code
Attribute (calculations) does not pass the type constraint because: Validation failed for 'ArrayRef[Calculation]' with value [ undef, Calculation={ expression: "1+2", result: 3 } ] at accessor Calculator::calculations (defined at Calculator.pm line 6) line 4. 
Calculator::calculations('Calculator=HASH(0x198ebd0)', 'ARRAY(0x25bc740)') called at Calculator.pm line 23
Calculator::add('Calculator=HASH(0x198ebd0)', 1, 2) called at main.pl line 4


If I change the line

Code
push(@calculations, $calculation);

to

Code
$calculations[0] = $calculation;

, then it works, but obviously this will only allow me to store one calculation.

Does anyone know what the problem is here?

Thanks

p.s. I tried to use the [perl] tag instead of the code tag, but it didn't appear to work.


waldm
New User

May 30, 2014, 6:04 AM

Post #2 of 3 (4743 views)
Re: [waldm] Moose and arrays [In reply to] Can't Post

Changing Calculator.pm to be:


Code
package Calculator;   

use Moose;

use Calculation;







has 'calculations' => (

is => 'rw',

isa => 'ArrayRef[Calculation]',

default => sub {my @c = (); return \@c;},

);







sub add {

my $self = shift;



my $x = $_[0];

my $y = $_[1];



my $calculation = Calculation->new(

expression => "$x+$y",

result => $x + $y);



push(@{$self->calculations}, $calculation);

}



1;


fixes it, with the main change being the line:

Code
default => sub {my @c = (); return \@c;},


Is this the correct way to initialise an array in a class?


Zhris
Enthusiast

May 30, 2014, 3:25 PM

Post #3 of 3 (4731 views)
Re: [waldm] Moose and arrays [In reply to] Can't Post

It would be nicer to return an anonymous array reference instead:


Code
my @c = (); return \@c; 
return [];


Chris

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives