Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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
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

Replies are listed 'Best First'.
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 ?

      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?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2016-07-23 13:46 GMT
Find Nodes?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?

    Results (220 votes). Check out past polls.