Skip to content

Instantly share code, notes, and snippets.

@haukex
Last active Feb 24, 2018
Embed
What would you like to do?
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