develooper Front page | perl.perl5.porters | Postings from July 2016

[perl #128588] Tied hash magic ignored in references the first time'round

Thread Previous
From:
Sanko Robinson
Date:
July 9, 2016 21:46
Subject:
[perl #128588] Tied hash magic ignored in references the first time'round
Message ID:
rt-4.0.18-27872-1468100768-1382.128588-75-0@perl.org
# New Ticket Created by  Sanko Robinson 
# Please include the string:  [perl #128588]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org/Ticket/Display.html?id=128588 >


A few years ago, I reported a tie related bug where the magic was being ignored on scalar values until they're accessed. Now, I'm back with a similar bug: tie magic is ignored by ref() (and likely others but ref() makes it obvious) when handed references to parts of a tied structure unless that reference is accessed (not modified) by other means first. Consider the following...

use strict;
use warnings;
use Tie::Hash;
$|++;

sub _debug {
    my ($ref) = @_;
    warn ref $ref;
    warn ${$ref}->{title};    # Accessing part of it == all better
    warn ref $ref;
}

# Test with tied hash ref
tie my %tied, 'Tie::StdHash';
$tied{record} = {id    => 1,
                 title => 'Record'
};
_debug(\$tied{record});

# Verify with simple hashref
my %simple;
$simple{record} = {id    => 1,
                   title => 'Record'
};
_debug(\$simple{record});

...the output from this reads this way:

SCALAR at debug.pl line 11.
Record at debug.pl line 12.
REF at debug.pl line 13.
REF at debug.pl line 11.
Record at debug.pl line 12.
REF at debug.pl line 13.

With the tied hash, ref() incorrectly returns SCALAR until we access any of the reference's contents. After that, we get the correct REF reftype. Just dumping the structure or printing any of the values is enough to wake perl's internals up to the tied magic.

All code written/tested on perl v5.20.1 built for MSWin32-x64-multi-thread.

Previous (only minimally related) ticket: https://rt.perl.org/Ticket/Display.html?id=120102
Downstream bug that got me here: https://github.com/sanko/readonly/issues/25


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About