Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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

Comment on multiple packages in one file
Download Code
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 scrutinizing the Monastery: (10)
As of 2015-11-30 06:10 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (758 votes), past polls