nysus has asked for the wisdom of the Perl Monks concerning the following question:
I'm doing old school OO and I'm seeing behavior I am utterly baffled by. I have this test:
use 5.014; use Test; use Data::Dumper qw(Dumper); use_ok('File'); my $f_from = File->new(); #my $f_to = File->new(); <--------- Note this line is commented out $f_from->section_add('BLAH'); print Dumper $f_from;
The dump of the File object:
ok 1 - use File; # # ### Dump from: 00-sections_copy_to_file.t line: 9 # $VAR1 = bless( # { # '_comment_chars' => bless( # { # 'begin_comment_char' => '#', # 'comment_char' => '#', # 'end_comment_char' => '#' # }, # 'File::CommentChars' # ), # '_content' => [ # bless( # { # '_prefix' => '# File::Section: BLAH', # '_suffix' => '# End File::Section: BLAH', # 'content' => [], # 'title' => 'BLAH' # }, # 'File::Section' # ) # ], # '_dir' => '', # '_line_count' => 0, # '_name' => '', # '_path' => '', # '_sections' => # bless( { 'BLAH' => $VAR1->{'_content'}[0] }, 'File::Sectio +ns' ), # '_type' => '' # }, # 'File' # ); # ### Dump from: 00-sections_copy_to_file.t line: 9
This is the desired structure of the object. However, when I uncomment out the line above, creating a new object File object in $f_to, things break and I get a much different dump from the same test file:
# ### Dump from: 00-sections_copy_to_file.t line: 9 # $VAR1 = bless( # { # '_comment_chars' => bless( # { # 'begin_comment_char' => '#', # 'comment_char' => '#', # 'end_comment_char' => '#' # }, # 'File::CommentChars' # ), # '_content' => [], # '_dir' => '', # '_line_count' => 0, # '_name' => '', # '_path' => '', # '_sections' => bless( # { # 'BLAH' => bless( # { # '_prefix' => '# File::Section: BLAH', # '_suffix' => '# End File::Section: BLAH', # 'content' => [], # 'title' => 'BLAH' # }, # 'File::Section' # ) # }, # 'File::Sections' # ), # '_type' => '' # }, # 'File' # ); # ### Dump from: 00-sections_copy_to_file.t line: 9
Notice how the content is an empty array reference now where as before it was populated. I have no idea why creating this new object, that does nothing, would affect how the first object is populated.
Here are the module files in pastebin:
Thanks!
Minor update: I simplified the test by getting rid of my custom boilerplate so it's easier to reproduce.
$PM = "Perl Monk's";
$MC = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar Parson";
$nysus = $PM . ' ' . $MC;
Click here if you love Perl Monks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
[SOLVED] Re: Creation of object having weird side effect on my code
by nysus (Parson) on Jan 19, 2024 at 02:13 UTC | |
|
Re: [SOLVED]: Creation of object having weird side effect on my code
by InfiniteSilence (Curate) on Jan 20, 2024 at 04:57 UTC |