oop - Object Oriented In Perl Script -


sub new {     $class = shift; #off first element     $self = { };     bless $self, $class;     return $self; } 

could explain that? use of following 3 lines of code?

my $self = { };     bless $self, $class;     return $self; 

  1. my $self = { }; creates anonymous hash reference , stores in the lexical variable $self.

  2. bless $self, $class; tells perl $self not reference object of class stored in $class. see bless in perldoc. bless $x, $y returns $x, , subroutine returns value of last executed statement unless explicetly told otherwise return statement, next line optional, readability.

  3. return $self; hands value in $self (our special object reference) calling function. see return in perldoc.

edit:

to clarify, if don't bless reference, won't able call methods on it. bless tell perl, "look, on, associate reference in $self class in $class, can use methods in class on reference."


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -