Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

multiple packages in one file

by jeanluca (Deacon)
on Mar 12, 2006 at 11:08 UTC ( #536070=perlquestion: print w/ replies, xml ) Need Help??
jeanluca has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

Here is an example of my module A.pm:
package A ; sub new { my ($class, $type) = @_ ; "A:B$type"->new() ; } package A::B001 ; sub new { ... }
This works great. But what if A::B001 does not exist ?
So my questions is how do I test this ?

Thanks a lot
Luca

Comment on multiple packages in one file
Download Code
Re: multiple package in one file
by friedo (Prior) on Mar 12, 2006 at 11:16 UTC
    You can see if a package exists by checking if its symbol table is defined.

    package Foo; $a = 1; package main; print "foo\n" if defined %Foo::; print "bar\n" if defined %Bar::;

    Note that the symbol table won't be defined until you put a symbol in it; a mere package declaration doesn't seem to do it.

      So my problems is not solved then, because I need to check if the package exists before I use it ?
      Thnx
      Luca

      update: I see it, it works, but this doesn't seem to work:
      if ( defined %A::B$type ) { }
      Any suggestions ?
        You'll need to use curlies to tell Perl that you want a variable as part of the package name.

        Try %{ 'A::B' . $type . '::' }

        Remember to include the extra '::' on the end.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2015-01-30 09:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My top resolution in 2015 is:

















    Results (248 votes), past polls