Hi Monks,
I am facing a weird problem due to unexpected behaviour of Perl OO ( I'm talking about mine expectations :P )
Please have a look at this test script :
use strict;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
my $object = bless {}, "MyMain";
$object->{a} = bless {}, "A";
$object->{b} = bless {}, "B";
$object->{c} = bless {}, "C";
$object->{a}->{var1} = "Pratik";
$object->{a}->{var2} = bless {}, "A1";
$object->{a}->{var2}->{name} = "Hello";
$object->{b}->{var1} = $object->{a}->{var2};
$object->{c}->{var1} = $object->{a}->{var2};
print Dumper($object);
#
# Start the magic.
#
my $dummy = bless {}, "A1";
$dummy->{name} = "CamelNeedsWater";
$object->{a}->{var2} = $dummy;
print Dumper($object);
It generates output as :
$VAR1 = bless( {
'a' => bless( {
'var1' => 'Pratik',
'var2' => bless( {
'name' => 'Hello'
}, 'A1' )
}, 'A' ),
'b' => bless( {
'var1' => $VAR1->{'a'}{'var2'}
}, 'B' ),
'c' => bless( {
'var1' => $VAR1->{'a'}{'var2'}
}, 'C' )
}, 'MyMain' );
$VAR1 = bless( {
'a' => bless( {
'var1' => 'Pratik',
'var2' => bless( {
'name' => 'CamelNe
+edsWater'
}, 'A1' )
}, 'A' ),
'b' => bless( {
'var1' => bless( {
'name' => 'Hello'
}, 'A1' )
}, 'B' ),
'c' => bless( {
'var1' => $VAR1->{'b'}{'var1'}
}, 'C' )
}, 'MyMain' );
Problem here is, assignments are behaving like COW, where as I'd expect them to behave like reference.
In my code, when I do $object->{a}->{var2} = $dummy;, I'd want $object->{b}->{var1} & $object->{c}->{var1} to still point to $object->{a}->{var2}, instead of getting it's own copy of old values.
Any inputs or work arounds ?
Thanks.
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.