|
|
|
|
|
by eCa
4163 days ago
|
|
I think it is correct to view them as related, but definitely not the same language. A short example from the recent Perl6 calendar[1]: Perl5: package Timer;
use Time::HiRes qw(gettimeofday tv_interval);
use Mojo::Base -base;
use experimental 'signatures', 'postderef';
has _event_log => sub { [] };
has _start_time => sub { [ gettimeofday ] };
sub record ($self, $event_description) {
# record the event and timestamp
my $event = {
timestamp => sprintf('%.3f', tv_interval($self->_start_time)),
description => $event_description,
};
push($self->_event_log->@*, $event);
}
sub report ($self) {
# render a full report
my $report = '';
foreach my $event ($self->_event_log->@*) {
$report .= sprintf "[%s] %s\n", $event->{timestamp}, $event->{description};
}
return $report;
}
1;
Perl6: class Timer {
has $!start-time;
has @!event-log;
method record ($event_description) {
# initialise the start-time if this is the first time through
$!start-time //= now;
@!event-log.push({
timestamp => now - $!start-time,
description => $event_description
});
}
method report {
my @report_lines;
for @!event-log -> %event {
@report_lines.push("[%event<timestamp>.fmt("%.3f")] %event<description>");
}
return @report_lines.join("\n");
}
}
[1] https://perl6advent.wordpress.com/2014/12/13/day-14-a-perl-6... |
|