Perl Module

Modules are useful to separating task, Which are commonly used in specific purpose. Inside a module there are possible to define classes and methods which used when that is import. There are many predefined modules are available in Perl programming. This post are introduced how to create custom package.

Defining a package

#This is name of package
#here Test is folder name and Operation is Filename (Operation.pm)
package Test::Operation;
use strict;
use warnings;


use base 'Exporter';

our @EXPORT = qw(sum); #exeport single method

#subroutine which are access outside this package
sub sum{
  #get parameter values
  my($num1,$num2) = @_;

  #return a result
  return $num1 + $num2;

}

sub subtract{
  #get parameter values
  my($num1,$num2) = @_;

  #return a result
  return $num1 - $num2;

}

1;#end with a true value
Create a package in perl
#!/usr/bin/perl
package Regular;
use strict;
use warnings;
#use module
use Test::Operation;

our $result1 = sum(1,2);
our $result2 = sum(3,4);

print $result1,"\n";  # prints 3
print $result2,"\n";  # prints 7
Using package method in perl


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved