Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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
[marto]: Corion I've been toying with this as a background task
[marto]: along with the pod for perlmodinstall

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2017-08-21 09:38 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (319 votes). Check out past polls.