aboutsummaryrefslogtreecommitdiff
path: root/challenge-027
diff options
context:
space:
mode:
authorJoelle Maslak <jmaslak@antelope.net>2019-09-29 12:43:49 -0600
committerJoelle Maslak <jmaslak@antelope.net>2019-09-29 12:43:49 -0600
commit43a7dbf7b937ef2a5f596f5e881d37752c2035c7 (patch)
treeacc8929a76535eef5296cdc55ce9f8815346645a /challenge-027
parente37ccf211816267d70de883935ab0d42c501c8bd (diff)
downloadperlweeklychallenge-club-43a7dbf7b937ef2a5f596f5e881d37752c2035c7.tar.gz
perlweeklychallenge-club-43a7dbf7b937ef2a5f596f5e881d37752c2035c7.tar.bz2
perlweeklychallenge-club-43a7dbf7b937ef2a5f596f5e881d37752c2035c7.zip
Solution to 27.2 in Perl 5
Diffstat (limited to 'challenge-027')
-rwxr-xr-xchallenge-027/joelle-maslak/perl5/ch-2.pl66
1 files changed, 66 insertions, 0 deletions
diff --git a/challenge-027/joelle-maslak/perl5/ch-2.pl b/challenge-027/joelle-maslak/perl5/ch-2.pl
new file mode 100755
index 0000000000..e45a5cdd15
--- /dev/null
+++ b/challenge-027/joelle-maslak/perl5/ch-2.pl
@@ -0,0 +1,66 @@
+#!/usr/bin/env perl
+use v5.22;
+use strict;
+use warnings;
+
+# Turn on method signatures
+use feature 'signatures';
+no warnings 'experimental::signatures';
+
+# Limitation - this only tracks the scalar container, not changes to
+# nested objects.
+
+package History {
+ use Moose;
+ use feature 'signatures';
+ no warnings 'experimental::signatures';
+
+ has hist => (
+ is => 'rw',
+ isa => 'ArrayRef',
+ default => sub { [] },
+ );
+ has data => ( is => 'rw', );
+
+ sub FETCH($self) {
+ return $self->data;
+ }
+
+ sub STORE ( $self, $value ) {
+ $self->data($value);
+ push $self->hist->@*, $value;
+
+ return $value;
+ }
+
+ sub TIESCALAR($self) {
+ return $self->new();
+ }
+
+ sub history($self) {
+ return $self->hist->@*;
+ }
+}
+
+MAIN: {
+ my $x;
+ my $hist = tie $x, 'History';
+
+ $x = 10;
+ $x = 20;
+ $x -= 5;
+
+ $x = 'Foo!';
+
+ # A new instance of history should be independnet.
+ my $z;
+ my $hist2 = tie $z, 'History';
+ $z = 3; # Won't show up in history for $x.
+
+ # And we just set the original history, the one we log, to a new
+ # value
+ $x = 'Baz.';
+
+ say join( "\n", $hist->history );
+}
+