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 :)

Perl – 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

 

Perl – 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.

 

Perl – 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.

 

 

Perl – 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:

 

Perl – Escaping shell commands

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

 

Leave a Reply

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

Captcha * Time limit is exhausted. Please reload CAPTCHA.