lathropr has asked for the wisdom of the Perl Monks concerning the following question:

Creating a new class that inherits from Config::IniFiles, I encountered a problem using "my" vs. "our". This new class differs from Config::IniFile in that it creates a INI file if one doesn't exist and sets default values. The other difference is that the INI file is rewritten when the script terminates. Here is what I have:

prefs.pm
package prefs; use strict; use Config::IniFiles; our @ISA = qw( Config::IniFiles ); sub new { my $self = shift; my $class = (ref $self) || $self; my $fname = 'fubar.ini'; my $obj; if (-f $fname) { $obj = $self->SUPER::new( -file => $fname ); } else { $obj = $self->SUPER::new(); $obj->SetFileName( $fname ); $obj->AddSection( 'fubar1' ); $obj->newval('var1=default'); } return bless($obj, $class); } sub DESTROY { my $self = shift; $self->RewriteConfig(); $self->SUPER::DESTROY(@_); } 1;
t1.pl
#!/usr/bin/perl -I. use strict; use prefs; my $inifile = new prefs;
t2.pl
#!/usr/bin/perl -I. use strict; use prefs; our $inifile = new prefs;
t1.pl works. t2.pl fails with a file called "fubar.ini-new". The only difference between the two is the creation of $inifile. One uses "my" and the other users "our". Any suggestions? Thanks, Randal

Replies are listed 'Best First'.
Re: my VS. our
by trammell (Priest) on May 26, 2005 at 19:48 UTC
    I'm unable to reproduce this behavior:
    perl 5.6.1 (Debian stable)
    Config::IniFiles 2.27
    
    both scripts create file 'fubar.ini' with one line: [fubar1]
      There is a typo in the prefs.pm I posted. See the reply to the rlucas I posted with the correction to prefs.pm. The newval statement is incorrect. t2.pl works only if "fubar.ini" does not exist. t1.pl always works.
Re: my VS. our
by rlucas (Scribe) on May 26, 2005 at 19:45 UTC
    We need more than this. What does it tell you while failing? Have you tried running them in different order (i.e., does t2 fail because t1 has taken the filename)?
      Neither script produces error messages. I added some debugging statements inside prefs.pm so I could see the execution flow (and fixed the newval statement):

      pref.pm
      package prefs; use strict; use Config::IniFiles; our @ISA = qw( Config::IniFiles ); sub new { my $self = shift; my $class = (ref $self) || $self; print "creating $class\n"; my $fname = 'fubar.ini'; my $obj; if (-f $fname) { $obj = $self->SUPER::new( -file => $fname ); } else { $obj = $self->SUPER::new(); $obj->SetFileName( $fname ); $obj->newval('fubar', 'var1', 'default'); } return bless($obj, $class); } sub DESTROY { my $self = shift; $self->RewriteConfig(); print "destroying $self\n"; $self->SUPER::DESTROY(@_); } 1;
      Here is the output:
      I am using Perl 5.8.6 on Win32 (Windows XP) with Config::IniFiles 2.38. I am able to reproduce the same problem on Solaris 8 with Perl 5.8.0 and Config::IniFiles 2.38.