Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: How to get at perl's options in a program

by kennethk (Monsignor)
on Nov 06, 2012 at 21:25 UTC ( #1002578=note: print w/ replies, xml ) Need Help??


in reply to Re^2: How to get at perl's options in a program
in thread How to get at perl's options in a program

A fairly reliable way to test if a module was loaded is to check for an associated entry in the Symbol Tables:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; package Foo; 1; package main; print "Data::Dumper was ", defined $Data::{'Dumper::'} ? "loaded\n" : +"not loaded\n"; print "Foo was ", defined $::{'Foo::'} ? "loaded\n" : "not loaded\n"; print "Eata::Dumper was ", defined $Eata::{'Dumper::'} ? "loaded\n" : +"not loaded\n";

This solution can be applied for any -M or -d. But, to the best of my understanding, the arguments themselves are lost to the ether.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


Comment on Re^3: How to get at perl's options in a program
Select or Download Code
Re^4: How to get at perl's options in a program
by tobyink (Abbot) on Nov 06, 2012 at 22:09 UTC

    That's actually a pretty flaky technique.

    use strict; use warnings; use Data::Dumper; package Foo; 1; package main; print "Data::Dumper was ", defined $Data::{'Dumper::'} ? "loaded\n" : +"not loaded\n"; print "Foo was ", defined $::{'Foo::'} ? "loaded\n" : "not loaded\n"; print "Eata::Dumper was ", defined $Eata::{'Dumper::'} ? "loaded\n" : +"not loaded\n"; # Here's a false positive! # "Data::Dumper" was loaded, and that makes it look like # "Data" has also been loaded... # print "Data was ", defined $::{'Data::'} ? "loaded\n" : "not loaded\n" +; # Here's another false positive! # When we checked if "Eata::Dumper" was loaded, # autovivification makes it look like "Eata" was loaded. # print "Eata was ", defined $::{'Eata::'} ? "loaded\n" : "not loaded\n" +;

    It's much better to check for the existence of the actual functions/methods you wish to use:

    my $Dumper = UNIVERSAL::can('Data::Dumper', 'can') && 'Data::Dumper'->can('Dumper'); print $Dumper->($hashref) if defined $Dumper;
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1002578]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2014-09-22 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (176 votes), past polls