My problem is that
lvalue has issues with temporary values which is what an element of a tied hash of a hash-blessed class attribute returns when accessed. So the following code is fine
{
package foo;
# not tied so all is well
my %h = qw(one two three four);
sub new { bless { val => \%h } }
sub val : lvalue { $_[0]->{val}->{one} }
}
use Devel::Peek;
my $obj = foo->new;
$obj->val = "foo";
Dump( $obj->{val}->{one} );
__output__
SV = PV(0x80f9c40) at 0x80f9964
REFCNT = 1
FLAGS = (POK,pPOK)
PV = 0x80f9018 "foo"\0
CUR = 3
LEN = 4
Now if I
tie() my hash then the
lvalueed method
val() will die when called
{
package foo;
use Tie::IxHash;
tie(my %h, 'Tie::IxHash', qw(one two three four));
sub new { bless { val => \%h } }
sub val : lvalue { $_[0]->{val}->{one} }
}
use Devel::Peek;
my $obj = foo->new;
Dump( $obj->{val}->{one} );
# dies here
$obj->val = "foo";
__output__
SV = PVMG(0x8131170) at 0x8154b68
REFCNT = 1
FLAGS = (TEMP,GMG,SMG,RMG)
IV = 0
NV = 0
PV = 0
... many more lines of output
Can't return a temporary from lvalue subroutine at - line 9
Firstly why does this happen, and secondly is there a way around it?
TIA
_________
broquaint
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.