0

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";      
}

I want to import function.pl in this new perl script.

#!/usr/bin/perl    
package main; 

myfunction;
myfunciton2;
Arunesh Singh
  • 3,489
  • 18
  • 26

2 Answers2

4

Remove that package main; - it's not needed.

Best practice way (but not the easiest one):

Create a new directory MyApp (replace by some unique name for your application) and place a file Global.pm into this directory:

package MyApp::Global; # Same name as Directory/File.pm!
use strict;
use warnings;

use Exporter;
use Term::ANSIColor qw(:constants);

our @ISA       = ('Exporter');
our @EXPORT_OK = qw(myfunction);

sub myfunction {
    print RED, " Deleting...", RESET;
    system("rm –f /file_location/*.");
    print "deleted.\n";  
}

1; # Important!

Insert into both files (function.pl and the newone.pl) right after the use lines:

use MyApp::Global qw(myfunction);

Basic way (PHP-like: Easier, but not "Best Practice"):

Create a file global.pl (or any other name):

use strict;
use warnings;

use Term::ANSIColor qw(:constants);

sub myfunction {
    print RED, " Deleting...", RESET;
    system("rm –f /file_location/*.");
    print "deleted.\n";  
}

1; # Important!

Insert into both files (function.pl and the newone.pl) right after the use lines:

require 'global.pl';

See also:

Sebastian
  • 2,472
  • 1
  • 18
  • 31
  • 1
    Also, at least please have a look at [`Module Tutorial`](http://www.perlmonks.org/?node_id=102347). – Arunesh Singh Apr 26 '16 at 13:30
  • 1
    I don't think you should post something that's ***not "Best Practice"*** just because the OP seems to be familiar with the way PHP works. Every language is different, and it's often clear when people are writing, for instance, C or PHP programs in Perl. While new concepts must be based on something, it's wrong to encourage people to write as if they were using a more familiar language, especially when that results in bad-practice code – Borodin Apr 26 '16 at 14:05
  • It's easier than creating a module, fully functional and Perl is TIMTOWTDY. Using `require` to load a library file is not uncommon, that's why I included it. But I'll move it down. – Sebastian Apr 26 '16 at 14:41
  • 1
    @Sebastian: It's all fine as long as those who copy your code know what it does and why it works. That almost never happens. People who ask how to create a library are looking for a formula. They will adopt the first thing they read that works and put it into their code, and copy it into hundreds of other programs without question. When your helpful suggestion doesn't work it could be causing a drought in a poor uncivilised location. Please don't be so blasé: missing semicolons can kill people. All because it's "easier" – Borodin Apr 26 '16 at 18:57
  • @Sebastian Thanks for everything, I did by the way I cant delete to package main because I m using in my project... – Musa Yıldırım Apr 26 '16 at 22:51
  • @Musa Yıldırım: "package main" is default. No matter if you explicit write it or not - it's there. Try to remove it an you'll notice that everything will still be functioning as before. – Sebastian Apr 27 '16 at 05:21
  • @Sebastian I Tried and yes It worked you're right thanks again :) – Musa Yıldırım Apr 27 '16 at 10:59
1

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

Functions.pm

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;

program.pl

#!/usr/bin/perl    

use strict;
use warnings 'all';

use Functions qw/ my_function /;

my_function();
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • @Borodin I did but I got the error message like: Undefined subroutine &main::my_function called at ./program.pl line 8. – Musa Yıldırım Apr 26 '16 at 14:09
  • @MusaYıldırım: If you copy my code exactly you will find that it works. You must be running something slightly different – Borodin Apr 26 '16 at 14:11
  • @Borodin okay ı got it, How do I do if a remote file,? – Musa Yıldırım Apr 26 '16 at 14:16
  • @MusaYıldırım: That is a different question. It's unclear exactly *what* file is remote, and you should listen to the advice posted in comments beneath your question about making some effort to find the solution yourself. It sounds like the real problem is that you don't know how to write code, and Stack Overflow can't help you with that – Borodin Apr 26 '16 at 14:17
  • I did, how can ı get if it remote file? Can I explain in this subject ? – Musa Yıldırım Apr 27 '16 at 11:00