October 2014 Archives

Which modules are currently loaded by process?

Have you ever wondered which modules are currently loaded by current process? With Perl it's very simple to know this piece of info. Perl saves loaded modules in a hash %INC.

Lets have some dirty hands:

use Data::Dumper;
print STDERR Dumper \%INC;

The output is as follows:

$VAR1 = {
          'warnings/register.pm' => '/usr/share/perl5/warnings/register.pm',
          'bytes.pm' => '/usr/share/perl5/bytes.pm',
          'XSLoader.pm' => '/usr/lib64/perl5/XSLoader.pm',
          'Carp.pm' => '/usr/share/perl5/Carp.pm',
          'Exporter.pm' => '/usr/share/perl5/Exporter.pm',
          'warnings.pm' => '/usr/share/perl5/warnings.pm',
          'overload.pm' => '/usr/share/perl5/overload.pm',
          'Data/Dumper.pm' => '/usr/lib64/perl5/Data/Dumper.pm'

The hash simply contains the module path as value.

So this can be used for cleaning up too, with it you can figure out why you're not getting the version of the module you're expecting, for example.

Getting IP address with Perl

A common situation which you will usually come across, is to retrieve the IP.

Using WWW::curlmyip, this task is very easy.

use WWW::curlmyip;
my $ip = get_ip();

Lets inspect the module on https://metacpan.org/pod/WWW::curlmyip closely to understand how it works.

="font-family: Menlo, Monaco, 'DejaVu Sans Mono', Consolas, 'Bitstream Vera Sans Mono', Courier, 'Courier New', monospace; font-size: 12px; white-space: …

About Ashraf Bashir

user-pic Perling, Perling .... and Perling!