Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

testFunction

by nate (Monk)
on Mar 21, 2000 at 03:37 UTC ( [id://5759]=perlcraft: print w/replies, xml ) Need Help??

   1: ####################################################################
   2: #  sub
   3: #       testFunction
   4: #
   5: #  purpose
   6: #       takes a module name and function name, returns true if the
   7: #       function exists in the module
   8: #	
   9: #  syntax: testFunction("Mine::Module", "myFunction");
  10: #
  11: sub testFunction {
  12:     my ($modname, $funcname) = @_;
  13:    
  14:     my $pkgname = $modname.".pm";
  15:     $pkgname =~ s/\:\:/\//g; #this is UNIX Specific
  16: 
  17:     #we use eval to avoid requiring a pkg which doesn't exist
  18:     eval '
  19:         no strict;
  20:         require($pkgname);
  21:         local *stash = *{ "${modname}::" };
  22: 
  23:         return unless defined &{ $stash{$funcname} };
  24:         1;
  25:         ';
  26: }

Replies are listed 'Best First'.
RE: testFunction
by johannz (Hermit) on Mar 23, 2000 at 23:05 UTC
    Perl already provides a built-in function to do this. It is the 'can' function which in contained in the UNIVERSAL package which every module inherits from. From the Advanced Perl Programming Book(O'Rielly):
    Chapter 7.3 UNIVERSAL
    
    All modules implicitly inherit from a built-in module
    called UNIVERSAL and inherit the following three methods:
    
    isa (package name)
    
         For example, Rectangle->isa('Shape') returns true if
    the Rectangle module inherits (however indirectly) from
    the Shape module. 
     
    can (function name)
     
         Rectangle->can('draw') returns true if the Rectangle
    or any of its base packages contain a function called draw. 
     
    VERSION (need version)
     
          If you say, 
    
              package Bank;
              $VERSION = 5.1;
     
          and the user of this module says,
     
              use Bank 5.2;
    
          Perl automatically calls Bank->VERSION(5.2), which
    can, for instance, make sure that all libraries required
    for version 5.2 are loaded. The default VERSION method
    provided by UNIVERSAL simply dies if the Bank's $VERSION
    variable has a lower value than that needed by the user of
    the module.
     
    Because Perl allows a package to shamelessly trample on
    other namespaces, some packages use the UNIVERSAL module
    as a holding area for some global subroutines that they
    wish to export to everyone. I recommend that you do not
    use this "feature" yourself (or at least not in those that
    you contribute to CPAN!).
    
RE: testFunction
by Anonymous Monk on Mar 24, 2000 at 08:57 UTC
    #
    sub method { }
    
    package Pkg;
    sub method_pkg { }
    
    package main;
    print defined '&method', "\n",
     main->can('method'), "\n",
     Pkg->can('method'), "\n",
     Pkg->can('method_pkg'), "\n";
    
    #

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-05-28 18:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found