CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Identify the modules used in our program

 



DivyaG
Novice

Nov 5, 2009, 2:01 AM

Post #1 of 7 (2769 views)
Identify the modules used in our program Can't Post

Hi,
I want to print the module names that are being used by my perl program.
I want to print it in the middle of the program.
@INC contains only the path of the modules but not the names.
Please help me out in this.


shawnhcorey
Enthusiast


Nov 5, 2009, 5:56 AM

Post #2 of 7 (2764 views)
Re: [DivyaG] Identify the modules used in our program [In reply to] Can't Post

%INC contains the modules and their paths.

Code
#!/usr/bin/perl 

use strict;
use warnings;

use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
$Data::Dumper::Maxdepth = 0;

use File::Find;

for my $k ( sort keys %INC ){
printf "%30s %s\n", $k, $INC{$k};
}


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


DivyaG
Novice

Nov 5, 2009, 9:26 PM

Post #3 of 7 (2742 views)
Re: [shawnhcorey] Identify the modules used in our program [In reply to] Can't Post

Thanks Shawn,
But the above code will give all the modules present in %INC.
I need only those module names that are used by my program itself.


bichonfrise74
Novice

Nov 8, 2009, 9:55 PM

Post #4 of 7 (2724 views)
Re: [DivyaG] Identify the modules used in our program [In reply to] Can't Post

I'm not sure why you actually want to do this. But this should help you get started.


Code
#!/usr/bin/perl 

use warnings;
use strict;
use Data::Dumper;

my $file = 'my_script.pl';

open( my $script, '<', $file )
or die "Cannot open $0";

print "Modules used in the program:\n\n";
while (<$script>) {
my ($module) = /^use\s(.*)\;/i;
print "$module\n" if ( $module );
}

close( $script );



--bichonfrise74


DivyaG
Novice

Nov 9, 2009, 4:25 AM

Post #5 of 7 (2714 views)
Re: [bichonfrise74] Identify the modules used in our program [In reply to] Can't Post

Thanks a lot, but this code will read the input from another file (my_script.pl).
Is there any way to print the module names used by our own script itself.
Eg, script name is test.pl and it is using certain modules, now we want to print the module names in between test.pl code.

This ques was actually being asked in one of the interview, where I got stuck.
So eager to know the solution.


FishMonger
Veteran / Moderator

Nov 9, 2009, 4:32 AM

Post #6 of 7 (2712 views)
Re: [DivyaG] Identify the modules used in our program [In reply to] Can't Post

The only valid reason I can think of for doing this is that it's a homework assignment.

What have you tried?


bichonfrise74
Novice

Nov 9, 2009, 9:55 AM

Post #7 of 7 (2701 views)
Re: [DivyaG] Identify the modules used in our program [In reply to] Can't Post

The answer to your question is obvious. And I agree with FishMonger that this sounds like a homework.


--bichonfrise74

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives