in reply to Regarding Perl Class Object

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re^2: Regarding Perl Class Object
by davorg (Chancellor) on Feb 22, 2007 at 13:38 UTC

    That works. But only because you have two errors that (fortuitously) pretty much cancel each other out.

    sub new() { my $self={}; $self->{FORM_VAR}=undef; bless($self); return $self; }

    You should always use the two-argument version of bless. Your constructor should look like this:

    sub new { my $class = shift; my $self = {}; # some initialisation bless($self, $class); return $self; }

    But that doesn't work here because you have this:

    my $obj = main::new();

    You're calling new() as a function, not as a class method, so it doesn't get passed the class name.

    You should invoke the constructor as a class method.

    my $obj = main->new();

    Perl doesn't strictly differentiate between functions and methods. But the programmer should.

A reply falls below the community's threshold of quality. You may see it by logging in.