in reply to creating 2 file writing objects

You are not storing your user object data in your object and you are not calling methods as methods but as subroutines, using the &routine syntax that has some really nasty side-effects.

Some pointers:

use strict; would have kicked your ass :-)

Your constructor should be something like:

use Symbol; sub new { my $class_name = shift; $classname = ref($classname) || $classname; my $self = { "output_file" => shift, "append" => shift, "file_handle" => gensym(), } bless ($self,$class_name); return $self; }

use Symbol; and gensym() are put in here for backward compatibility to perls before 5.6 - see the Symbol manpage

And methods should be called and constructed like:

$object->method($arg1,$arg2); sub method { my $self = shift; my $arg1 = shift; my $arg2 = shift; print "append is ",$self->{"append"},"\n"; $self->other_method($arg2); }

Take a look at perldoc perltoot and/or get your hands on the 'programming perl' book

Update: changed $append and friends to "append" - that should actually work :-)

also added some code to show the retrieval of object data.

-- Joost downtime n. The period during which a system is error-free and immune from user input.

Replies are listed 'Best First'.
Re: Re: creating 2 file writing objects
by postman (Acolyte) on Jun 27, 2002 at 15:27 UTC
    Many thanks to you ... and the others who replied so quickly, for pointing out the error of my ways which stems in part I think from trying to write Java in Perl. I could just use a cpan module, but I had the feeling that I was misunderstanding something fundamental that I needed to sort out. Next on my list is to re-read perltoot. Meanwhile ... one more question if I may, I'm trying to
    print $self->{file_handle} "some text \n" ;
    which is wrong, but can't figure out what it should be. Thanks again
      On top of my OO confusion, I was getting led further astray by my overlooking the global nature of filehandles, and my mistaken way of trying to store them in a hash. Got there in the end ... by storing a reference to the file handle in a hash
      use FileHandle; my $FH = FileHandle->new(">>$self->{output_file}"); $self->{file_handle} = \$FH;
      and used in other methods like this
      my $FH = ${$self->{file_handle}}; print $FH "some text\n" ;
      Thanks again for all the pointers.