summaryrefslogtreecommitdiff
path: root/027
diff options
context:
space:
mode:
authorGuillermo Ramos2019-10-04 14:45:53 +0200
committerGuillermo Ramos2019-10-04 14:46:03 +0200
commit06fb0dfd62ec5d712e34d960b2cee9eddbf90336 (patch)
tree9894bdc2c4f788d9122265af19107efb69938c63 /027
parent7382a075847f760d7dceec3fee497c2d4506ae43 (diff)
downloadperlweekly-06fb0dfd62ec5d712e34d960b2cee9eddbf90336.tar.gz
[027#2]
Diffstat (limited to '027')
-rwxr-xr-x027/ch2.pl45
1 files changed, 45 insertions, 0 deletions
diff --git a/027/ch2.pl b/027/ch2.pl
new file mode 100755
index 0000000..25bd645
--- /dev/null
+++ b/027/ch2.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/env perl
+#
+# Write a script that allows you to capture/display historical data. It could be
+# an object or a scalar. For example
+#
+# my $x = 10; $x = 20; $x -= 5;
+#
+# After the above operations, it should list $x historical value in order.
+################################################################################
+
+use strict;
+use warnings;
+
+{
+ package Hist;
+
+ require Tie::Scalar;
+ our @ISA = qw<Tie::Scalar>;
+
+ sub TIESCALAR {
+ return bless \my @histogram, shift;
+ }
+ sub FETCH {
+ my @self = @{shift()};
+ printf STDERR "History of values: %s\n", join(", ", @self);
+ return @self[@self-1]
+ }
+ sub STORE {
+ my $self = shift;
+ my $value = shift;
+ push @$self, $value;
+ }
+}
+
+tie my $x, 'Hist';
+
+$x = 10;
+$x = 20;
+$x = -5;
+
+my $y = $x;
+print "y=$y\n";
+
+$x = 200;
+print "x=$x\n";