0

I'm working on an example in perl in oops concept.I'm unable to install the perl package modules.

This file is saved with 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;  

This file is saves with person.pl

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

I'm getting error message like this Can't locate Student.pm in @INC (you may need to install the Student module) (@INC contains: C:/Perl64/site/lib C:/Perl64/lib)........

Ekanvitha
  • 25
  • 3
  • Try include the current directory in the module search path: `use lib '.'`. See also [Doesn't Perl include current directory in @INC by default?](https://stackoverflow.com/q/46549671/2173773) – Håkon Hægland Jun 18 '19 at 14:08
  • 1
    @HåkonHægland: You should expand that into an answer. I bet it's the correct solution. – Dave Cross Jun 18 '19 at 16:16
  • 1
    @Håkon Hægland, You should read the page you linked, seeing as it explains that `use lib '.';` is wrong. – ikegami Jun 18 '19 at 19:43

1 Answers1

2

To load a module in the current directory, add the current directory to the module search path @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");

Also note that the module name should be Student.pm and not student.pm. Case is significant.

 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;  

NOTES:

  • new Student is the old way of instantiating an object (using so-called indirect object notation). The recommended way is now Student->new(...)

  • our @EXPORT =('new'): You do not need to export new and it is usually a mistake to do so. Since object creation should be qualified by the module name, it does not make sense to export new.

  • require Exporter; use vars qw(@ISA @EXPORT); @ISA = qw(Exporter); is old style. Better to write use Exporter qw(import).

ikegami
  • 367,544
  • 15
  • 269
  • 518
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
  • 2
    Re "*Case is significant.*", Technically, `use Student;` + `package Student;` + 'student.pm` will work on a case-insensitive file system. But I agree that the file *should* be named `Student.pm` even if it's not required. – ikegami Jun 18 '19 at 19:41
  • If both files are in different directories, is there any changes to be made from the above program. – Ekanvitha Jun 20 '19 at 10:04
  • @Ekanvitha Yes if for example `Student.pm` is in directory `lib` relative to `person.pl` you need `use lib "${RealBin}/lib"` – Håkon Hægland Jun 20 '19 at 10:07
  • I have saved student.pm in D:\Ekanvitha\modules and Student.pl in D:\Ekanvitha\perl. I have added like this use lib " ${RealBin}\Ekanvitha\modules"; error is displaying as Useless use of \E at D:\Ekanvitha\perl\student.pl line 6. Unrecognized escape \m passed through at D:\Ekanvitha\perl\student.pl line 6. Global symbol "$RealBin" requires explicit package name (did you forget to declare "my $RealBin"?) at D:\Ekanvitha\perl\student.pl line 6. BEGIN not safe after errors--compilation aborted at D:\Ekanvitha\perl\student.pl line 6. – Ekanvitha Jun 20 '19 at 11:14
  • @Ekanvitha If you use backslashes in the path, you would need to escape them. Alternatively, you could try `use lib ($RealBin . '\..\modules');` since single quotes do not interpret backslash escape sequences – Håkon Hægland Jun 20 '19 at 11:34
  • I'm facing this error -Global symbol "$RealBin" requires explicit package name (did you forget to declare "my $RealBin"?) at D:\Ekanvitha\perl\student.pl line 6. BEGIN not safe after errors--compilation aborted at D:\Ekanvitha\perl\student.pl line 6. – Ekanvitha Jun 20 '19 at 11:51
  • @Ekanvitha Could you send me the code you are working on? Then I will have a look at it. You can find my email in my profile. – Håkon Hægland Jun 20 '19 at 11:55