Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Testing package membership

by FreakyGreenLeaky (Sexton)
on Nov 21, 2008 at 08:25 UTC ( #725066=perlquestion: print w/ replies, xml ) Need Help??
FreakyGreenLeaky has asked for the wisdom of the Perl Monks concerning the following question:

Greetings,

I'm looking for a cleaner way to test if a package member exists/is valid.

I'm currently testing it thusly:
package Util::Stuff::abc; sub attrib1 { 1 } sub attrib2 { 10 } ... eval { $symref->attrib1; }; if (!$@) { ... $symref->attrib1 is OK ... }
Is there a cleaner way?

Regards
Henry

UPDATE: forgive me if I'm not using the correct Perl nomenclature. I'm an old procedural boy an' all this OO'ism makes me sleepy...

Comment on Testing package membership
Download Code
Re: Testing package membership
by GrandFather (Sage) on Nov 21, 2008 at 08:45 UTC

    If you are testing for methods you can:

    use strict; use warnings; use strict; package Abc; sub new { return bless {}, shift; } sub attrib1 { 1 } sub attrib2 { 10 } package main; my $abc = Abc->new (); for my $methName (qw(attrib1 attrib2 attrib3)) { next unless my $meth = $abc->can ($methName); print "Can $methName: ", $meth-> (), "\n"; }

    Prints:

    Can attrib1: 1 Can attrib2: 10

    Perl reduces RSI - it saves typing
      Thanks - but I need to step through almost a hundred packages (which is existing code) extracting those attribute values. So modifying the code and adding sub new() for each is problematic.

        There is a theme running through your last three questions, but it's not clear to me just what you are trying to achieve (although you could be trying to auto-document some legacy code). We may be able to help more if you let us in on the bigger problem.


        Perl reduces RSI - it saves typing
Re: Testing package membership
by moritz (Cardinal) on Nov 21, 2008 at 08:46 UTC
    You can ask with $ref->can('methodname') or yourpackage->can('methodname') if such a method exists (which will not work if AUTOLOAD is in effect, but otherwise it's fine).

    can is defined and documented in UNIVERSAL.

      Hmm, looks like AUTOLOAD is in effect since ->can() is failing.

      Looks like I'm stuck with eval{}, unless there's another way.

      Got it! Using ->can() wrapped in eval{} does the trick nicely:
      package Util::Stuff::abc; sub attrib1 { 1 } sub attrib2 { 10 } ... eval { if ($symref->can('attrib1')) { ... $symref->attrib1 is OK ... }};
      Thanks to GrandFather and moritz.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2015-07-04 07:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (58 votes), past polls