I see this a lot in bash scripts and I cannot see this in the manual and other sites.
What does the -I in running a perl script mean?
It is run like this:
perl -I$prod_dir $prod_dir/script.pl <parameter1> <parameter2>
Can someone explain it to me?
-Idirectory
Directories specified by -I are prepended to the search path for modules (#INC ).
Source: perlrun documentation
It means perl will include the modules available under specified directory following -I which is $prod_dir in your case.
By default Perl picks up modules from #INC. If you want to use a module which is not available in #INC then you can specify the directory using -I. This specified directory will be appended to #INC at run time.
Also read:
How is Perl's #INC constructed? (aka What are all the ways of affecting where Perl modules are searched for?)
How to change #INC to find Perl modules in non-standard locations
Related
I have a .pl script in which starts by:
#!/usr/bin/perl
use XML::XPath;
use Getopt::Long;
I can't seem to run that via perl myScript.pl, having this error:
(#INC contains: /usr/share/ /usr/local/lib64/perl5 /usr/local/share/perl5/usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at most_generic_wrapper.pl line 3.
BEGIN failed--compilation aborted at myScript.pl line 3.
1- I tried to locate the XPath.pm file and export that as:
export PERL5LIB=/usr/share/perl5/XML/Twig
and
export PERL5LIB=/usr/share/perl5/XML
2- Installed perl -MCPAN -e 'install XML::Parser'
3- Used -I to explicitly define the path as:
perl -I perl -MCPAN -e 'install XML::Parser' myScript.pl
4- changing the line 3 to use XML::Twig::XPath; led to:
cannot use XML::Twig::XPath: neither XML::XPathEngine 0.09+ nor XML::XPath are available at /usr/share/perl5/XML/Twig/XPath.pm line 11.
BEGIN failed--compilation aborted at /usr/share/perl5/XML/Twig/XPath.pm line 13.`
But none of them solved the issue and I keep receiving the same error at line.3.
P.S: Running on CentOS 6.2 with the kernel 2.6.32-358 and perl --version=v5.10.1 (*) built for x86_64-linux-thread-multi
Any helps would be appreciated,
Your title says XML::XPath can't be found, but your question indicates you tried to install XML::Parser. Did you try to install XML::XPath?
From man perlrun: "If PERL5LIB is not defined, PERLLIB". You seem to have tried setting PERLIB5 (notice the spelling difference: the var is PERL5LIB (or PERLLIB), not PERLIB5).
From man perlrun: "PERL5LIB -- A list of directories in which to look for Perl library files before looking in the standard library and the current directory." You seem to have tried setting it to the full path to a .pm file, rather than a directory.
The file you assigned would be XML::Twig::XPath, not XML::XPath; those are two different Perl modules.
Edit: After looking at your revised question:
I'm not sure if your script requires XML::Twig::XPath or XML::XPath, or if either one can provide the API you need. However, XML::Twig::XPath seems to depend on XML::XPath so you will need XML::XPath no matter what, and it looks like XML::XPath is not installed on your system. I think that's probably the main problem. Please try to install XML::XPath using CPAN.
The value of the PERL5LIB variable (or the argument to the -I option) should be the directory that sits at the base of the package-qualified module file. For example, if XML::XPath is located at ~/perl_custom_modules/XML/XPath.pm, then you need to set PERL5LIB (or the -I argument) to ~/perl_custom_modules. The XML directory is part of the package qualification of the module, so does not need to be included in the include path.
I am trying my hands for the first time in -> Apache - Perl (CGI) on Ubuntu.
The Apache server is working fine in the folder /var/www for the default index.html file. But since I want to run a CGI script, I also installed (just in case) DBI, as suggested by some forums. I set the permissions for complete access to my cgi-bin folder in which the select.cgi script resides. I tried tweaking the select.cgi script multiple times redirecting the library to the Perl library, but to no avail. I modified the httpd.conf file and set the directory path to the select.cgi folder. That didn't work. I also defined ScriptAlias, and set it to the working directory. That didn't work either.
Does anyone have any helpful pointers. Thanks.
This is not exact solution that you want but the overall idea is like below.
How to resolve Cant locate xxxx.pm in #INC path problem
By default perl looks for modules in the standard library path and the current directory.
Sometimes you need to use some modules that are installed in non standard locations; there are several ways to deal with this situation:
To check whether your module is in #INC path use.
Example:
perl -e 'use SOAP::Lite;'
perl -e 'use Error;'
If you run these commands on a system that has SOAP::Lite and Error installed, Perl will simply return from these commands without printing any output.
To check current standard library path use:
perl -le 'print foreach #INC'
If you have the administrative privileges, then best solution is to install the module in any of the system defined library path.
Set the environment variable PERL5LIB
Perl will look for modules in the directories specified in PERL5LIB environment variable before looking in the standard library and current directory.
So you can set this variable to locate your modules.
Example:
# For unix like systems
PERL5LIB=/home/path/lib:/usr/another/path/lib; export PERL5LIB
Note: separate the directories with colons on unix and with a semicolon on Windows.
IF you are running your code from command line then use -I parameter. The syntax should be something like.
perl -I /home/path/lib -I /usr/another/lib script.pl
And you can also Add the library path in your script
The command for including the path in your script is: use lib path.
Example:
#!/usr/bin/perl
use lib "/home/path/lib";
use lib "/usr/another/lib";
use MyCustomModule;
In a comment, you gave information that contradicts what you gave originally. The latter information appears to be more reliable, so I'll use it.
I believe you said the path to the module is
/usr/lib/perl5/vendor_perl/5.8.5/foo/bar/Connection.pm
and that the error message you got (with line breaks added) is:
Can't locate foo/bar/Connection.pm in #INC (#INC contains:
/etc/perl
/usr/local/lib/perl/5.14.2
/usr/local/share/perl/5.14.2
/usr/lib/perl5
/usr/share/perl5
/usr/lib/perl/5.14
/usr/share/perl/5.14
/usr/local/lib/site_perl
.
/usr/lib/perl5/vendor_perl/5.8.5/foo/bar/
) at ...
You did something like
use foo::bar::Connection;
or
require "foo/bar/Connection.pm";
Perl looked for
/etc/perl/foo/bar/Connection.pm
/usr/local/lib/perl/5.14.2/foo/bar/Connection.pm
...
/usr/lib/perl5/vendor_perl/5.8.5/foo/bar/foo/bar/Connection.pm
But none of those are
/usr/lib/perl5/vendor_perl/5.8.5/foo/bar/Connection.pm
It's simple to fix. Add the following to your script:
use lib '/usr/lib/perl5/vendor_perl/5.8.5';
The other possible fix is to use
use Connection;
instead of
use foo::bar::Connection;
Which fix is the correct fix depends on what that package line in side the module looks like. If you find package foo::bar::Connection;, you need to modify #INC (as shown with use lib, for example). If you findpackage Connection;, you need to change theuse` directive.
I'm a perl noob and I have a very basic question regarding the #INC post: How is Perl's #INC constructed? (aka What are all the ways of affecting where Perl modules are searched for?)
Does the "otherlibdirs" Configure option completely rewrite the directories that are part of #INC, or merely add another directory? It's not clear to me from that answer what it does, and I don't want to screw up the whole #INC path.
Thanks!
Perusing source of the Perl Configure script, we find the following usage information:
case "$otherlibdirs" in
''|' ') dflt='none' ;;
*) dflt="$otherlibdirs" ;;
esac
$cat <<EOM
Enter a colon-separated set of extra paths to include in perl's #INC
search path, or enter 'none' for no extra paths.
EOM
So if you compile Perl with something like
Configure -Dotherlibdirs=/usr/foo/bar:/usr/foo/bar/baz
Then the directories /usr/foo/bar and /usr/foo/bar/baz will be appended to the normal #INC built-in to the perl binary.
Im trying to find perl modules, such as strict and warnings, but i cant find them... btw im actually using archlinux, i tried using
whereis
but it throws nothing.
If the module has POD documentation embedded (which most do), the following will display its location:
perldoc -l Some::Module (Lowercase "L" for "location")
Otherwise, you can use
perl -E'use Some::Module; say $INC{"Some/Module.pm"};'
You might be interested in identifying all the locations in which your Perl searches for modules. If so, look at the contents of #INC. You can use
perl -V (Uppercase "V")
or
perl -E'say for #INC;'
You may also be interested in Devel::Modlist. The following will lists the path to all the modules used (directly or indirectly) by a script or module:
perl -d:Modlist=path some_script.pl
perl -d:Modlist=path -e'use Some::Module;'
Without =path, it returns the versions of all the modules.
To find an individual module:
perldoc -l warnings
All modules are under #INC directories:
perl -V
See also: Find installed Perl modules matching a regular expression
The %INC hash holds the on-disk locations of loaded modules, keyed by the package name. You can step through the keys of %INC and print out the associated value. For example:
$ perl -MData::Dump -e 'print "$_: $INC{$_}\n" foreach keys %INC'
(I loaded Data::Dump so that at least one module would be pulled in for sure. You don't have to load that specific module yourself.)
Also, the #INC array holds the include paths that perl searches for modules in, so you can always do:
$ perl -E 'say foreach #INC'
To find all the default include paths.
Since you are using a Linux distribution, the native package manager is the most suitable tool. In this case, it's highly recommend to use pacman for such a task:
pacman -Ql perl | egrep '(strict|warnings).pm'
recently I was installing some perl modules on my RHEL 5 with perl version 5.8.8 and all instalations went fine. I can see that the modules exist in the #INC but my TWiki site claims that it can't find them returning an error: Can't locate Net/LDAP.pm in #INC (a lot of paths which contain the modules) at TWiki.pm line xx. When I do perl -e 'use Net::LDAP'; it doesn't return anything which means perl can find that module. Also TWiki was configured corectly and works fine except the plugins that use specific modules I had to install, I've even added the paths to setLib.cfg just in case.
Edit:
which perl returns /usr/bin/perl
the shebang line of twiki/cgi-bin/view is #!/usr/bin/perl -wT
perl -MNet::LDAP -e 'print $INC{"Net/LDAP.pm"}, "\n";' returns:
/usr/lib/perl5/site_perl/5.8.8/Net/LDAP.pm
apache error logs show: [Tue Nov 16 10:53:47 2010] [error] [client 10.76.14.170] [Tue Nov 16 10:53:47 2010] view: INC /usr/lib/perl5/site_perl/5.8.8 at /usr/local/apache2/htdocs/twiki5_pdc/bin/view line 44. So it use's the correct path.
#INC is possibly different from your command line - either because you're using a different Perl interpreter binary, or other factors affecting #INC.
Check command line's #INC: perl -e 'print join(",", sort #INC);' - and compare to #INC printed in your Wiki's error that you mentioned.
You might have to add directories to your web server's Perl's #INC which are present in command line's ("how" depends on whether you're running under mod_perl or not).
What Apache thinks your INC path is and what your command-line Perl thinks your INC path are usually two completely different things. You may have to set the environment variable PERL5LIB in your Apache configuration.
I'm a big fan of TWiki/FOSwiki and have encountered this issue several times in the past.
Do this (it works in a good number of cases):
perldoc -l Net::LDAP
If there is POD in the file, it will point you to the .pm. Even so, if it points you to a .pod file, the .pm is usually in the same directory.
Then check that list after the #INC( and see if the path is in there. You will probably need to modify the environment that Apache runs in, because it probably will not be in there.
Since, that didn't work for you, this should:
perl -MNet::LDAP -e 'print $INC{"Net/LDAP.pm"}, "\n";'
If perl is actually loading it, that will tell you where it's loading it from.
Horses: Have you reviewed the installation guide to make sure that everything's configured correctly?
Zebras: edit bin/twiki_cgi and add right before $TWiki::engine->run():
warn "ENV $_ => $ENV{$_}" for sort keys %ENV;
warn "INC $_" for #INC;
grep your apache error log for the rows with ENV and INC. Look for ENV rows with PERL5LIB or PERLLIB. Check INC rows to make sure the directories containing your CPAN libraries are listed.
Ok the reason was most probably that I was installing the perl modules as root and they were not set executable for others so apache couldn't execute/use them.