A module I am creating has a blessed hash ref as is common. One of the hashes is a reference to an array of anonymous hashes created like so:
Later on I want to push another anonymous hash onto @products.sub new { my $class = shift; my %attrs = @_; my @products = ({ 'id' => 0, 'name' => 'Test', 'description' => 'Some test data', 'qty' => 1, 'price' => 1000, }); $attrs{'trolley'} = \@products; return bless \%attrs, $class; }
What is the best way to do this?
Both of these push lines appear to work identically in testing
However, I suspect there is some subtle difference between the two which might trip me up in the future!sub add_product { my ($self, $product_data) = @_; # create $new_product hasfref push $self->{'trolley'}, $new_product; push @{$self->{'trolley'}}, $new_product; }
Is there a practical difference and is there a 'best' option to use?
In reply to Pushing hash ref onto array ref by Bod
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |