in reply to Help understanding object constructors

$this is being assigned the value of a hashref (see perlref), and is NOT a loop. It is the same as doing:
my %thisHash = ( _server => undef, _database => undef, _hSession => undef, _hDatabase => undef, ); my $this = \%thisHash;
In both cases (yours and the above), the "variables in this loop" (again, note it is not a loop) are the default key-value pairs of the hash (see perldata).

As for the method sub new { ... } and the bless $this, $type, this is a example of a common way to write a constructor method for an object. For more information, start with perlobj and also there is a specifical Object-Oriented Perl section in the Tutorials here on pm.

Replies are listed 'Best First'.
Re^2: Help understanding object constructors
by jbrugger (Parson) on Nov 07, 2005 at 06:18 UTC
    Correct.
    But for standards (and to make it a bit more readable), it might be better to write it like this, although it's also a matter of taste :)
    sub new { my $classname = shift; my %arg= ( _server => undef, _database => undef, _hSession => undef, _hDatabase} => undef, @_, ); my $self = {}; # note this corresponds whith $this in your code. bless $self, $classname; $self->{_server} = $arg{_server}; $self->{_database} = $arg{_database}; $self->{_hSession} = $arg{_hSession}; $self->{_hDatabase} = $arg{_hDatabase}; return $self; }

    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re^2: Help understanding object constructors
by nisha (Sexton) on Nov 07, 2005 at 05:47 UTC
    Thank U very much