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

Here is part of my Games::QuizTaker module:
package Games::QuizTaker; use strict; use Carp; sub new{ my($self,%arg)=@_; bless{ _FileName => $arg{FileName}||croak"No FileName Given", _Delimitor => $arg{Delimitor}|| "|", _FileLenght => "0", } } sub _Print_Object{ my $self=shift; foreach my $key(keys %$self){ print"$key = $$self{$key}\n"; } }
Now if I create a new object with the following code:
#!/opt/perl5/bin/perl -w use strict; use Games::QuizTaker; my $Q=Games::QuizTaker->new(FileName="File",Delimitor=","); $Q->_Print_Object();
All of the variables in the object print out. However, if I remove the Delimitor from the new function, the only thing that prints out is the name of the file. Needless to say I am rather confused. I would be grateful if any one could shed some light on this.

TStanley
--------
There's an infinite number of monkeys outside who want to talk to us
about this script for Hamlet they've worked out
-- Douglas Adams/Hitchhiker's Guide to the Galaxy

Replies are listed 'Best First'.
(Ovid) Re: Unable to put default value in object hash
by Ovid (Cardinal) on Nov 14, 2001 at 23:13 UTC

    You don't want an equals sign there.

    my $Q=Games::QuizTaker->new(FileName => "File", Delimitor => ",");

    If anyone else is going to use your code, you probably should correct your spelling. Both 'delimiter' and 'filelength' are misspelled.

    I can also see the _Print_Object method breaking if any values are references. Try this:

    sub _Print_Object{ my $self=shift; require Data::Dumper; print Data::Dumper::Dumper $self; }

    I also noticed that you weren't using the two argument form of bless. Why is that? If you don't bless your referent into the class passed as the first argument, you're going to break inheritance, which you will find very difficult to debug later if you want to use this as a base class. Also, the first argument should probably not be called $self because this is probably not an object method.

    sub new { my ($class,%arg)=@_; bless{ _FileName => $arg{FileName}||croak"No FileName Given", _Delimiter => $arg{Delimiter}|| "|", _FileLength => "0", }, $class }

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      After some modifications, the code now looks like this:
      package Games::QuizTaker; use strict; use Data::Dumper; use Carp; sub new{ my($class,%args)=@_; bless{ _FileName => $args{FileName}||croak"No FileName given", _Delimiter => $args{Delimiter}|| "|", _FileLength => "0", },$class; } sub Print_Object{ my $self=shift; print Dumper($self); } 1;
      And my testing program is as follows:
      #!/opt/perl5/bin/perl -w use strict; use Games::QuizTaker; my $Q=Quiz->new("FileName"=>"File"); $Q->Print_Object();
      However, when I do run the above script, the Print_Object shows only the _FileName parameter within the object. I am trying to get the _Delimiter parameter to be set to a pipe character if the Delimiter isn't passed in.

      TStanley
      --------
      There's an infinite number of monkeys outside who want to talk to us
      about this script for Hamlet they've worked out
      -- Douglas Adams/Hitchhiker's Guide to the Galaxy

        Your code works fine for me if I make the following change:

        #my $Q=Quiz->new("FileName"=>"File"); my $Q=Games::QuizTaker->new("FileName"=>"File");

        Cheers,
        Ovid

        Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.