Perl Examples

Published on Author gryzli

The main idea of posting this post is for my own comfort (so I can find these things when I need them).

The code examples may not be suitable for direct insertion inside your script. 

 

Find the files location of a perl module

perldoc -l Some::Module

 

Check if perl module is part of the core modules with Module::CoreList 

There is a very handy module called “Module::CoreList” which resides inside perl core from version v5.8.9. 

The module comes with pre-installed binary as well called “corelist”, which is handy for use: 

You could also use it inside perl script. 

The following example will list you all core modules for a given perl version (ex: 5.10.0) . 

 

Working with perl lib directories and modules 

 

Loading modules during runtime (not compile time)

If you are using “use XXX;” this loads the module during script compile time. 

You are also able to load a module during the run time, by using “require“.

 

Manually installing module from CPAN

If you can’t install a package from CPAN with cpan or cpnam for some reason, you can compile it manually. The process is simple: 

The example above shows 2 different ways of compiling a module, depending on whether it has Makefile.PL or Build.PL. 

Also there is additional argument “–install_base and INSTALL_BASE env” , which are telling make to install the module locally in my /home/gryzli/perl5 directory.

 

Adding additional lib directories to perl

Sometimes you may need to load perl module from a custom directory, not mentioned in your default perl @INC. 

There are several ways to accomplish this, here are some examples: 

Inside your perl script

 

Outside your perl script

 

Reading file content

Some interesting method with do {} 

 

 

 

Dynamic Function List Execution With GetOpts And Dispatcher

Sometime you want to associate each new argument to your perl script, with separate function. If you write a lot of functions during the development, adding each function as an argument option (inside GetOptions) and also defining separate If() statement for checking it, could be boring. 

In this case you can try executing the function dynamically, by using something like this: 

 

 

 

Generating Usage/Man like help with Pod::Usage

If you are tired of writing usage() or help() functions for showing usage notes about your program, you may wish to consider Pod::Usage. 

By using it, you will have consistent formatting through your help messages, and also will save some code. 

 

Here it is a simple example: 

 

Showing the short version of the help 

By running it with “–man” you could go to a man-like page , which will visualize all of the information in your POD. 

 

 

 

Using DateTime To Work With Dates

 

 

Changine the umask of your script

Some time you may need to change the default umask in order to affect newly created files by your perl script. This could be easily done by adding the following to your script:

More about how umask works under Linux, could be read here:

https://wiki.archlinux.org/index.php/umask

 

Changing PATH environment variable

Sometimes you may need to execute binaries from path which is not currently inside your environment variable.

Also if your script is running as a CRON job, most probably your PATH would be much limited.

Adding new directories to PATH inside perl script is as easy as:

It is good to append your current PATH to the end of the newly modified PATH string.

 

Adding random sleep / wait

If you need to add sleep for a period less than a second , it would be good idea to use Time::HiRes perl module.

 

 

Check if script is running under screen session

Sometimes we write scripts that are intended to be run inside screen sessions for the sake of safety.

You could add check inside your script to make sure it is run inside screen session:

 

Escaping shell commands

If you need to escape certain shell commands, you could use String::ShellQuote  .

 

Storing and Loading object with Storable

If you need to store your object and later re-load it with all of it’s data , you could use “Storable” module.

You could actually store your object and reload it in another script run and you will be able to access it’s functions and variables.

 

Simple and safe logging to a file for multiple concurrent processes using a lock

Let say you want to log events to a log and you have multiple scripts running in the same time and trying to save events to the same log file.

There is a pretty big chance the different processes to overlap lines in the log.

One of the ways you can achieve this is by  Log::Log4perl::Appender::Synchronized .

If you want to make it less complicated, you could use function like this:

 

Resources

Perl Simple Web Hit Function Using LWP 

https://xdg.me/blog/