Perl构造函数和析构函数用法指南

2021年3月12日14:25:06 发表评论 819 次浏览

build

Perl子例程中的构造方法返回一个对象, 该对象是该类的实例。在Perl中, 约定是命名构造函数

new

。不像其他

面向对象

, Perl不提供任何特殊的语法来构造对象。它使用已与该特定类显式关联的数据结构(哈希, 数组, 标量)。

构造函数使用

bless

哈希引用和类名(包的名称)上的函数。

让我们设计一些代码以获得更好的解释:

注意:

由于使用了程序包, 因此以下代码无法在Online IDE上运行。下面的代码是Perl类或模块文件。将以下文件另存为(* .pm)扩展名。

# Declaring the Package
package Area; 
  
# Declaring the Constructor method
sub new 
{
     return bless {}, shift ; # blessing on the hashed 
                             # reference (which is empty).
}
  
1;

调用构造方法时, 包名称" Area"存储在默认数组中" @_"。的"转移"关键字用于从中获取软件包名称" @_"并将其传递给"保佑"功能。

package Area;
  
sub new
{
     my $class = shift ; # defining shift in $myclass
     my $self = {}; # the hashed reference
     return bless $self , $class ;
}
1;
Note: "my" restricts the scope of a variable.

Perl中的属性存储为散列引用中的键值对。此外, 向代码添加一些属性。

package Area;
  
sub new 
{
     my $class = shift ;
     my $self = 
     {
         length => 2, # storing length
         width  => 3, # storing width 
     };
     return bless $self , $class ;
}
1;

上面的代码(区类)具有两个属性:长度和宽度。为了访问这些属性, 另一个Perl程序被设计为使用它们。

use strict;
use warnings;
use Area;
use feature qw/say/ ;
  
# creating a new Area object
my $area = Area->new;
  
say $area ->{ length }; #print the length
say $area ->{width}; # print the width

运行代码的步骤:

  1. 将"带有程序包区域的程序"保存到名为的文本文件中Area.pm
    注意:文件名应始终与包名相同。
  2. 现在, 保存用于访问名称为* .pl的程序包中定义的属性的程序。在这里, *可以是任何名称(在本例中为test.pl)。
  3. 使用以下命令在Perl命令行中运行另存为test.pl的代码
    perl test.pl

输出如下:

Perl |构造函数和析构函数1

传递动态属性:

使用动态属性更新现有文件:

Areas.pm:

package Area;
  
sub new 
{
     my ( $class , $args ) = @_ ; # since the values will be 
                              # passed dynamically
     my $self = 
     {
         length => $args ->{ length } || 1, # by default the value is 1 (stored)
         width  => $args ->{width} || 1, # by default the value is 1 (stored)
     };
     return bless $self , $class ;
}
  
# we have added the get_area function to
# calculate the area as well
sub get_area 
{
     my $self = shift ;
      
     # getting the area by multiplication
     my $area = $self ->{ length } * $self ->{width}; 
     return $area ;
}
1;

test.pl:

use strict;
use warnings;
use feature qw/say/ ;
use Area;
  
# pass length and width arguments 
# to the constructor
my $area = Area->new(
             {
                 length => 2, # passing '2' as param of length
                 width => 2, # passing '2' as param of width
             });
  
say $area ->get_area;

现在, 参数

length = 2, width = 2

被传递到包Area, 以计算Square的面积。

当调用任何子例程时, 参数包含在默认数组变量@_中

注意:

现在, 按照与上述相同的方式运行代码。

输出如下:

Perl |构造函数和析构函数2

destroy

当对对象的所有引用超出范围时, Perl会自动调用析构函数方法。如果该类创建了线程或临时文件, 如果对象被销毁, 则需要清除这些线程或临时文件, 则使用析构函数方法。 Perl包含一个特殊的方法名称, 破坏", 则必须在声明析构函数时使用。

语法如下:

sub DESTROY 
{ 
     # DEFINE Destructors
     my $self = shift ;
     print "Constructor Destroyed :P" ; 
}
Once this snippet is added to existing file Area.pm.

输出将如下所示:

Perl |构造函数和析构函数3

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: