如何在ActivePerl中手动安装软件包模块

时间:2019-06-18 13:46:01

标签: perl

我正在用oops概念制作perl的示例。无法安装perl软件包模块。

此文件保存在student.pm中

 package Student;
 require Exporter;
 use vars qw(@ISA @EXPORT);
 @ISA = qw(Exporter);
 our @EXPORT =('new'); 
 sub new   
 {  
     my $class = shift;  
     my $self = {  
         _name => shift,  
         _rank  => shift,     
     };  

     print "Student's name is $self->{_name}\n";  
     print "Student's rank is $self->{_rank}\n";  

     bless $self, $class;  
     return $self;  
 }  

 1;  

此文件与person.pl保存在一起

  use Student; 
  $object = new Student( "Ram", "3th");  

我收到这样的错误消息 在@INC中找不到Student.pm(您可能需要安装Student模块)(@ INC包含:C:/ Perl64 / site / lib C:/ Perl64 / lib)........

1 个答案:

答案 0 :(得分:2)

要将模块加载到当前目录中,请将当前目录添加到模块搜索路径@INC

use strict;
use warnings;
use FindBin qw( $RealBin );  # The script's directory.
use lib $RealBin;            # Look for modules there.
use Student; 
my $object = Student->new( "Ram", "3th");

还要注意,模块名称应为Student.pm,而不是student.pm。情况很重要。

 package Student;
 use Exporter qw(import);
 our @EXPORT = qw(); 
 sub new   {  
     my $class = shift;  
     my $self = {  
         _name => shift,  
         _rank  => shift,     
     };  

     print "Student's name is $self->{_name}\n";  
     print "Student's rank is $self->{_rank}\n";  

     bless $self, $class;  
     return $self;  
 }  

 1;  

注释

  • new Student是实例化对象的旧方法(使用所谓的间接对象表示法)。现在推荐的方法是Student->new(...)

  • our @EXPORT =('new'):您不需要导出new,这样做通常是错误的。由于对象创建应使用模块名称来限定,因此导出new毫无意义。

  • require Exporter; use vars qw(@ISA @EXPORT); @ISA = qw(Exporter);是旧样式。最好写use Exporter qw(import)