J
Justin C
I've been reading various OO tutorials and other references on the web
(I found <URL: http://www.perlmonks.org/?node_id=218778> more useful
than perlboot and perltoo - though they are more clear to me now). To
see if I'm understanding correctly I'm writing something that has little
use other than educating me.
I have a module:
package My_Product;
use strict;
sub new {
my $class = shift;
my $item = shift;
my $style = shift;
my $price = shift;
my @sizes = @_;
my $self = {
ITEM => $item,
STYLE => $style,
PRICE => $price,
SIZES => @sizes,
};
return bless $self, $class;
}
I'm creating a new object with:
my $item = MM_Product->new($fields[0], $fields[1], $fields[2], @sizes);
But when I later do
foreach my $size ($item->{SIZES}) {
...
}
I only get the first item of the array.
I know that there are several items in the array when it's passed to the
new object, but I don't know how to be sure that it arrived at the
object, and that, therefore, it's there when I want to access it.
Here's a working version of the program (if you create the above module
too) it's as short as I could make it.
#!/usr/bin/perl
use warnings;
use strict;
use My_Product;
my @items;
foreach (<DATA>) {
chomp;
my @fields = split /:/;
my $size_count = length $fields[3];
my @sizes;
for (my $i = 0; $i < $size_count; $i++) {
my $size = substr($fields[3], $i, 1);
push @sizes, $size;
}
my $item = My_Product->new($fields[0], $fields[1], $fields[2], @sizes);
push @items, $item;
}
foreach my $item (@items) {
foreach my $size ( $item->{SIZES} ) {
printf("%-4s %-20s %-20s %7.2f \n", $size, $item->{ITEM}, $item->{STYLE}, $item->{PRICE});
}
}
__DATA__
AVS1234:TS:6.99:678ABCD
DMBT007:HS:7.99:BCD
G12128C:TS:8.99:ABCD
Thank you for any help you can give with this.
Justin.
(I found <URL: http://www.perlmonks.org/?node_id=218778> more useful
than perlboot and perltoo - though they are more clear to me now). To
see if I'm understanding correctly I'm writing something that has little
use other than educating me.
I have a module:
package My_Product;
use strict;
sub new {
my $class = shift;
my $item = shift;
my $style = shift;
my $price = shift;
my @sizes = @_;
my $self = {
ITEM => $item,
STYLE => $style,
PRICE => $price,
SIZES => @sizes,
};
return bless $self, $class;
}
I'm creating a new object with:
my $item = MM_Product->new($fields[0], $fields[1], $fields[2], @sizes);
But when I later do
foreach my $size ($item->{SIZES}) {
...
}
I only get the first item of the array.
I know that there are several items in the array when it's passed to the
new object, but I don't know how to be sure that it arrived at the
object, and that, therefore, it's there when I want to access it.
Here's a working version of the program (if you create the above module
too) it's as short as I could make it.
#!/usr/bin/perl
use warnings;
use strict;
use My_Product;
my @items;
foreach (<DATA>) {
chomp;
my @fields = split /:/;
my $size_count = length $fields[3];
my @sizes;
for (my $i = 0; $i < $size_count; $i++) {
my $size = substr($fields[3], $i, 1);
push @sizes, $size;
}
my $item = My_Product->new($fields[0], $fields[1], $fields[2], @sizes);
push @items, $item;
}
foreach my $item (@items) {
foreach my $size ( $item->{SIZES} ) {
printf("%-4s %-20s %-20s %7.2f \n", $size, $item->{ITEM}, $item->{STYLE}, $item->{PRICE});
}
}
__DATA__
AVS1234:TS:6.99:678ABCD
DMBT007:HS:7.99:BCD
G12128C:TS:8.99:ABCD
Thank you for any help you can give with this.
Justin.