Skip to content

Instantly share code, notes, and snippets.

@haukex
Last active February 24, 2018 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haukex/7da1940256716ec1ff6bd3f9c89814e5 to your computer and use it in GitHub Desktop.
Save haukex/7da1940256716ec1ff6bd3f9c89814e5 to your computer and use it in GitHub Desktop.
use warnings;
use strict;
use Data::Dumper;
use Memoize::Expire;
tie my %cache => 'Memoize::Expire';
my $self = { cache => [ \%cache ] }; # self is not tied nor an object
eval { print Dumper($self); 1 } or warn $@;
#=> Can't locate object method "FIRSTKEY" via package "Memoize::Expire" at ...
$self = $self->{cache}[0]; # $self is now a ref to the tied hash
print Dumper(tied $self); #=> $VAR1 = undef;
print Dumper(tied %$self);
#=> $VAR1 = bless( {
# 'LIFETIME' => 0,
# 'C' => {},
# 'NUM_USES' => 0
# }, 'Memoize::Expire' );
{ package MyTie;
BEGIN { require Tie::Scalar; our @ISA = qw(Tie::StdScalar); }
use overload bool => sub {} }
my $plain = 'plain';
tie my $tied, 'MyTie';
$tied = 'tied';
print Dumper(tied $plain); #=> $VAR1 = undef;
print Dumper(tied $plain || $plain); #=> $VAR1 = 'plain';
print Dumper(tied $plain // $plain); #=> $VAR1 = 'plain';
print Dumper(tied $tied); #=> $VAR1 = bless( do{\(my $o = 'tied')}, 'MyTie' );
print Dumper(tied $tied || $tied); #=> $VAR1 = 'tied'; (!!)
print Dumper(tied $tied // $tied); #=> $VAR1 = bless( do{\(my $o = 'tied')}, 'MyTie' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment