in reply to Unable to put default value in object hash

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.

Replies are listed 'Best First'.
Re: (Ovid) Re: Unable to put default value in object hash
by TStanley (Canon) on Nov 15, 2001 at 04:24 UTC
    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.