I want to use an obj->log($msg) function to store strings, and then later dump them
UPDATED:
use warnings;
use strict;
use diagnostics;
package obj;
sub log{
my($self,$log_info) = @_;
push(@{$self->{log}},$log_info);
}
sub showlog{
my($self) = @_;
my $counter = 0;
my @log= @{$self->{log}};
print '<div id="log" class="_view _debug"><h1>Log Data</h1>';
foreach my $i (@log) {
$counter++;
print '<div class="item',(($counter%2)?' alt':''),'"><em class="tag">',$counter,'</em><pre>';
print(Dumper($i));
print $i;
print '</pre></div>';
}
print '</div>';
}
but I can’t figure out how to append new items to $self->{log} — I come from PHP land so this is a bit painful.
It would be nice if I could have log be any sort of data, and just dump it out, array, hash or scalar… is there a way to do this?
Hashes values must be scalars. If you want to store multiple values in hash value, you’ll need to find a way of placing multiple values in a scalar. A way that would work well here involves storing a reference to an array in the hash value:
Then place the multiple values in the referenced array:
One would iterate over the values as follows:
If you want to use index for
$i % 2, you can use:Note: Since 5.14.0,
is mostly equivalent to
Perl 5.14 is pretty new, so you might want to stay away from that for unless you’re just coding for yourself.