Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by kennethk (Abbot)
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.

Replies are listed 'Best First'.
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?
[Corion]: Hmm - is anybody aware of a "protocol-only" HTTP module? I'd like to teach Prima non-blocking HTTP ideally without using threads, but that means reimplementing/ rewriting something like HTTP::Tiny (except asynchronous) or AnyEvent::HTTP
[Corion]: Ideally, the HTTP state machine but with a way to feed it HTTP data whenever I get it instead of it actively doing stuff.
[Corion]: Now that I talk about this, maybe Net::Inspect::L7:: HTTP would be the right kind of thing for the responses at least.

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2016-12-07 15:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (128 votes). Check out past polls.