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

    OK, the problem was that I was experimenting with a class variables and made $file a class variables in the File::Sections module. So I think when a new File object got created, it was stomping on the previous value of the File::Sections class variable. Change $file into an attribute of File::Sections solved the problem.

    $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

Re: [SOLVED]: Creation of object having weird side effect on my code
by InfiniteSilence (Curate) on Jan 20, 2024 at 04:57 UTC

    So what you want with testing is a general strategy that helps you eliminate possibilities like class instantiation errors, data type problems, etc. before moving on to so-called use cases. Cheesy example:

    perl -e 'package foo; sub new { return bless {}} package main; my $new + = new foo(); use Test::More qw~no_plan~; diag(qq~High level prelimin +ary testing...~); ok(1==1,qq~basic~); ok($new->isa(qq~foo~), qq~Is a +foo~); diag(qq~Use Case 1: Editing customers...~);'

    If you follow a methodology where you start by eliminating the high level possibilities like dependencies, environment, and establishing that objects are in the proper states you will find that your tests themselves answer most (if not all) of your questions.

    Celebrate Intellectual Diversity