Perl application installation - perl

I have written a small perl application for deployment on a few servers. It consists of some scripts, some modules and some data files. It will be used by multiple users. I'd like to keep all these files together in one directory rather than moving the app modules to the site_perl directory.
For example, lets say the application consists of jump.pl swim.pl Common.pm and messages.txt
The application (either of jump.pl or swim.pl) will be invoked from another directory. The installation location of the application may not be on $PATH so the user (or another app) might invoke it by an absolute pathname such as /some/path/swim.pl or a relative pathname such as ../path/swim.pl
I'd like relocating the application to be as simple as possible, this means having the path to the application in as few places as possible.
At the moment swim.pl and jump.pl start like this
#!/usr/bin/perl
use strict;
use warnings;
use lib '/some/path';
use Common;
#
my $basedir = '/some/path';
...
open my $fh, '<', "$basedir/messages.txt" or die ... ;
So I have /some/path in several locations in several scripts and modules.
I've considered the following as possible means to minimise the number of different places to store /some/path
Set environment variable PERL5LIB=/some/path and do
open my $fh, '<', "$ENV{PERL5LIB}/messages.txt"
Set the shebang line to #!/usr/bin/perl -I/some/path - but then how find messages.txt?
Try use Cwd qw(abs_path);
and find a way to use basename abs_path($0) for use lib in BEGIN?
Any suggestions to maximise flexibility of location, keep everything together, minimise multiple hard-coding of installation location?

FindBin:
use FindBin qw( $Bin );
use File::Spec::Functions qw( catfile updir );
use lib catfile( updir($Bin), 'lib');

Related

Including a module from another module [duplicate]

I don't know how to do one thing in Perl and I feel I am doing something fundamentally wrong.
I am doing a larger project, so I split the task into different modules. I put the modules into the project directory, in the "modules/" subdirectory, and added this directory to PERL5LIB and PERLLIB.
All of these modules use some configuration, saved in external file in the main project directory - "../configure.yaml" if you look at it from the module file perspective.
But, right now, when I use module through "use", all relative paths in the module are taken as from the current directory of the script using these modules, not from the directory of the module itself. Not even when I use FindBin or anything.
How do I load a file, relative from the module path? Is that even possible / advisable?
Perl stores where modules are loaded from in the %INC hash. You can load things relative to that:
package Module::Foo;
use File::Spec;
use strict;
use warnings;
my ($volume, $directory) = File::Spec->splitpath( $INC{'Module/Foo.pm'} );
my $config_file = File::Spec->catpath( $volume, $directory, '../configure.yaml' );
%INC's keys are based on a strict translation of :: to / with .pm appended, even on
Windows, VMS, etc.
Note that the values in %INC may be relative to the current directory if you put relative directories in #INC, so be careful if you change directories between the require/use and checking %INC.
The global %INC table contains an entry for every module you have use'd or require'd, associated with the place that Perl found that module.
use YAML;
print $INC{"YAML.pm"};
>> /usr/lib/perl5/site_perl/5.8/YAML.pm
Is that more helpful?
There's a module called File::ShareDir that exists to solve this problem. You were on the right track trying FindBin, but FindBin always finds the running program, not the module that's using it. ShareDir does something quite similar to ysth's solution, except wrapped up in a nice interface.
Usage is as simple as
my $filename = File::ShareDir::module_file(__PACKAGE__,
'my/data.txt');
# and then open $filename or whatever else.
or
my $dirname = File::ShareDir::module_dir(__PACKAGE__);
# Play ball!
Change your use Module call to require Module (or require Module; Module->import(LIST)). Then use the debugger to step through the module loading process and see where Perl thinks it is loading the files from.

Perl - Relative path of a file inside a module

I am building up a website using Perl. I organized my files as follow:
/index.cgi
/perl/modules/databaseFunctions.pm
/perl/indexCheck.cgi
/database/database.xml
Inside databaseFunctions.pm I have a function X() that reads and writes on database.xml. In X() I have specified relative path of the database as follow:
sub X{
my $db_path='../../database/database.xml';
my $parser=XML::LibXML->new();
my $doc=$parser->parse_file($db_path);
....
....
}
Here is the problem:
I have to call X() from index.cgi and indexCheck.cgi but I get an error the following error:
Could not create file parser context for file "../../database/database.xml": No such file or directory at perl/modules/databaseFunctions.pm line 21.
I think the problem is that when I call X() inside index.cgi or inside /perl/indexCheck.cgi the relative path of the database is different but I don't know how to set a path that works for index.cgi and /perl/indexCheck.cgi.
I think the problem boils down to "How to find out the path of the current script (*.pl)?" and
"How to find out the path of the current module (*.pm)?".
Scripts
For scripts, there is a very convenient module, FindBin, that offers 4 variables for the current script's name and path with
either symlinks resolved or not. Usually $FindBin::Bin is what you are looking for. It's the path of the current script.
I often use it to enhance the #INC path so that my scripts find additional (own) modules like so:
use FindBin;
use lib "$FindBin::Bin/my_mod_path";
use MyModule;
In this case MyModule.pm is searched for in the directory my_mod_path below the current script's path. Very convenient.
The module is part of the core distribution, i.e. no further installation is neccessary.
Modules
FindBin may not safely be used from inside modules because then it depends who (script or module) makes the first use FindBin;.
So if you don't want to care about the order, don't use FindBin; in modules, only in scripts.
For modules, there is some trick. Use the perl function caller().
Depending on the context called in, it returns the $filename of the file where it actually was called.
Thus, in modules you can safely use the following to get the module's path:
use File::Basename;
my $path_of_this_module = File::Basename::dirname( eval { ( caller() )[1] } );
Given that path you can navigate relative to it in order to find the other files you need, e.g. "$path_of_this_module/../.." and so on.
EDIT
index.cgi:
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/perl/modules";
use databaseFunctions;
databaseFunctions::X( "called from index.cgi\n" );
perl/indexCheck.cgi:
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/modules";
use databaseFunctions;
databaseFunctions::X( "called from indexCheck.cgi\n" );
perl/modules/databaseFunctions.pm:
package databaseFunctions;
use File::Basename;
my $path_of_this_module = File::Basename::dirname( eval { ( caller() )[1] } );
sub X {
my $arg = shift;
my $db_path="$path_of_this_module/../../database/database.xml";
open(my $fh, '>>', $db_path) or die "cannot open $db_path: $!\n";
print $fh $arg;
close($fh);
}
1;
When I now call ./index.cgi and then ./perl/indexCheck.cgi, then I get the following:
database/database.xml:
called from index.cgi
called from indexCheck.cgi
Exactly, what I thought you were looking for.

I want to create a word count module and I want to reuse it further

I want to create a module in Perl. The below code is not working properly. I want to create a word count module and I want to reuse it further. Can anyone help me out to create this module? This is my first attempt to create a module so kindly help me out.
package My::count
use Exporter qw(import);
our #Export_ok = qw(line_count);
sub line_count {
my $line = #_;
return $line;
}
I saved the above code in count.pm
use My::count qw(line_count);
open INPUT, "<filename.txt";
$line++;
print line count is $line \n";
I saved the above script in .pi extension.
This code is showing error when I run it on an Ubuntu platform. Kindly help me to fix this errors.
Perl scripts are stored with .pl extension. As you say use My::count qw(line_count); Perl tries to search the modules from the directories stored in #INC variable. You can run it with the -I flag to specify the directory to search the custom packages. Refer to this question for more info.
By convention Perl packages usually have a capitalized first letter, so My::count is more in keeping with convention if you call it package My::Count;. Typically lower-cased module names are reserved for pragmas such as 'strict' and 'warnings'. So go ahead and change the name to My::Count.
Next, save the module in a path such as lib/My/Count.pm. lib is by convention as well.
Then you have to tell your script where to find package My::Count.
Let's assume you're storing your module and your executable like this:
~/project/lib/My/Count.pm
~/project/bin/count.pl
Notice I also used a .pl extension for the executable. This is another convention. Often on Unix-like systems people omit the .pl extension altogether.
Finally, in your count.pl file you need to tell perl where to find the library. Often that is done like this:
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin qw($Bin);
use lib "$Bin/../lib";
use My::Count 'line_count';
# The rest goes here...
As you can see, we're using FindBin to locate where the executable is stored, and then telling perl that it should look (among other places) in the lib folder stored in a relative location to the executable.
Naturally, as this is Perl, this is not the only way to do it. But it's one common idiom.
You need to move your count.pm file into a directory called My. So you have the following.
./count.pl
./My/count.pm

How to have conditional libraries with Perl? [duplicate]

This question already has answers here:
Perl - Include package from relative path, which includes another package from a relative path?
(4 answers)
Closed 8 years ago.
I have homemade #Perl libraries that I'm moving from machine to machine, and the paths are not the same from place to place. Specifically, on the old machines, they existed in /home/foo/lib/, while they're moving to /group/bar/apps/lib on the new machines, and I now want to have them in something like /home/me/dev/Tools/lib.
What we did was have multiple use lib lines. /home/foo isn't available on the new machine, and /group/bar isn't a directory on the old machine, so when it sees this --
use lib '/home/foo/lib/' ;
use lib '/group/bar/apps/lib' ;
use Tools::Foo ;
-- everything is fine.
The problem is, they link to each other, and I'd rather not have something in /home/me/dev/Tools/lib load a program from /group/bar/apps/lib, and when I move this stuff to production, I don't want to have anything pointing back to ~/me/dev. Preferrably, I would want to not have to modify the code when I move it into production, so that, when everything is deployed, diff /group/bar/apps/lib/Tools/Foo.pm /home/me/dev/Tools/lib/Tools/Foo.pm would be empty.
So, how do I set things for multiple conditional library locations?
Options:
Properly install the modules.
Place your modules relative to the script
use FindBin qw( $RealBin );
use lib "$RealBin/../lib"; # Or whatever.
Use environment variable PERL5LIB rather than use lib.
The statements could be placed in sitecustomize.pl (if support for sitecustomize.pl was enabled when perl was built).
Using the following pragma
package lib_first_of;
use lib ();
use strict;
use warnings;
use Carp;
sub import {
foreach my $path (#_) {
if (-d $path) {
lib->import($path);
return 1;
}
}
croak "$0: no suitable library path found";
}
1;
lets your main program have the form
#! /usr/bin/env perl
use strict;
use warnings;
use lib_first_of (
"/home/foo/lib",
"/group/bar/apps/lib",
);
use MyModule;
print "done.\n";
If neither path is present, the program fails with errors resembling
my-program: no suitable library path found at my-program line 7
BEGIN failed--compilation aborted at my-program line 9.

How do I load libraries relative to the script location in Perl?

How can you get current script directory in Perl?
This has to work even if the script is imported from another script (require).
This is not the current directory
Example:
#/aaa/foo.pl
require "../bbb/foo.pl"
#/bbb/bar.pl
# I want to obtain my directory (`/bbb/`)
print($mydir)
The script foo.pl could be executed in any ways and from any directory, like perl /aaa/foo.pl, or ./foo.pl.
What people usually do is
use FindBin '$Bin';
and then use $Bin as the base-directory of the running script. However, this won't work if you do things like
do '/some/other/file.pl';
and then expect $Bin to contain /some/other/ within that. I'm sure someone thought of something incredibly clever to work this around and you'll find it on CPAN somewhere, but a better approach might be to not include a program within a program, but to use Perl's wonderful ways of code-reuse that are much nicer than do and similar constructs. Modules, for example.
Those generally shouldn't care about what directory they were loaded from. If they really need to operate on some path, you can just pass that path to them.
See Dir::Self CPAN module. This adds pseudo-constant __DIR__ to compliment __FILE__ & __LINE__.
use Dir::Self;
use lib __DIR__ . '/lib';
I use this snippet very often:
use Cwd qw(realpath);
use File::Basename;
my $cwd = dirname(realpath($0));
This will give you the real path to the directory containing the currently running script. "real path" means all symlinks, "." and ".." resolved.
Sorry for the other 4 responses but none of them worked, here is a solution that really works.
In below example that adds the lib directory to include path the $dirname will contain the path to the current script. This will work even if this script is included using require from another directory.
BEGIN {
use File::Spec;
use File::Basename;
$dirname = dirname(File::Spec->rel2abs( __FILE__ )) . "/lib/";
}
use lib $dirname;
From perlfaq8's answer to How do I add the directory my program lives in to the module/library search path?
(contributed by brian d foy)
If you know the directory already, you can add it to #INC as you would for any other directory. You might if you know the directory at compile time:
use lib $directory;
The trick in this task is to find the directory. Before your script does anything else (such as a chdir), you can get the current working directory with the Cwd module, which comes with Perl:
BEGIN {
use Cwd;
our $directory = cwd;
}
use lib $directory;
You can do a similar thing with the value of $0, which holds the script name. That might hold a relative path, but rel2abs can turn it into an absolute path. Once you have the
BEGIN {
use File::Spec::Functions qw(rel2abs);
use File::Basename qw(dirname);
my $path = rel2abs( $0 );
our $directory = dirname( $path );
}
use lib $directory;
The FindBin module, which comes with Perl, might work. It finds the directory of the currently running script and puts it in $Bin, which you can then use to construct the right library path:
use FindBin qw($Bin);
You can also use local::lib to do much of the same thing. Install modules using local::lib's settings then use the module in your program:
use local::lib; # sets up a local lib at ~/perl5
See the local::lib documentation for more details.
Let's say you're looking for script.pl. You may be running it, or you may have included it. You don't know. So it either lies in the %INC table in the first case or as $PROGRAM_NAME (aka $0) in the second.
use strict;
use warnings;
use English qw<$PROGRAM_NAME>;
use File::Basename qw<dirname>;
use File::Spec;
use List::Util qw<first>;
# Here we get the first entry that ends with 'script.pl'
my $key = first { defined && m/\bscript\.pl$/ } keys %INC, $PROGRAM_NAME;
die "Could not find script.pl!" unless $key;
# Here we get the absolute path of the indicated path.
print File::Spec->rel2abs( dirname( $INC{ $key } || $key )), "\n";
Link to File::Basename, File::Spec, and List::Util