Perl – Useful samples …

Published on Author gryzliLeave a comment

The samples I’m giving below are things that I frequently use for this or that script. 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 – this is totally problem of yours :)



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:


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:



Perl Simple Web Hit Function Using LWP 

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha * Time limit is exhausted. Please reload CAPTCHA.