I want to import a library into my perl script. Below is my attempt:
function.pl
#!/usr/bin/perl
package main;
use strict;
use warnings;
use 5.005;
use Term::ANSIColor qw(:constants);
use LWP::Simple;
use diagnostics;
use File::Spec;
use Getopt::Long;
use File::Basename;
use Cwd 'abs_path';
sub myfunction {
print RED, " Deleting...", RESET;
system("rm –f /file_location/*.");
print "deleted.\n";
}
#!/usr/bin/perl
package main;
myfunction;
myfunciton2;
If you just want a container for a number of utility subroutines then you should create a library module using Exporter
Name your package and your module file something other than main
, which is the default package used by your main program. In the code below I have written module file Functions.pm
which contains package Functions
. The names must match
package Functions;
use strict;
use warnings;
use Exporter 'import';
our @EXPORT_OK = qw/ my_function /;
use Term::ANSIColor qw(:constants);
sub my_function {
print RED, " Deleting...", RESET;
system("rm –f /file_location/*.");
print "deleted.\n";
}
1;
#!/usr/bin/perl
use strict;
use warnings 'all';
use Functions qw/ my_function /;
my_function();