habit_forming has asked for the wisdom of the Perl Monks concerning the following question:

Oh great and erudite Monks. A question if you please...

I have a pm, let's say Foo.pm. And Foo.pm contians the package statement package Foo::Bar; However if I try to use Foo::Bar perl comes back with the "I can't find Foo/Bar.pm". I thought that perl was suppose to search the Foo.pm before searching the directory tree for an actual file.

If this is not the case what are intra-file sub-packages good for?

--habit

Replies are listed 'Best First'.
Re: sub-package question
by dragonchild (Archbishop) on Jul 06, 2004 at 19:09 UTC
    use Foo::Bar @stuff; means exactly the following:
    BEGIN { require 'Foo/Bar.pm'; Foo::Bar->import( @stuff ); }
    where @stuff is if you do something like use Foo::Bar baz => 1;

    Nowhere in Perl are the packages Foo and Foo::Bar related, except by convention.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: sub-package question
by chromatic (Archbishop) on Jul 06, 2004 at 19:13 UTC
    If this is not the case what are intra-file sub-packages good for?

    Dividing programs up into separate namespaces. You don't always need it, but sometimes it comes in handy.

      The only other benefit I can think of is syntactic sugar. But, that sugar is better solved using factories. *shrugs*

      ------
      We are the carpenters and bricklayers of the Information Age.

      Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: sub-package question
by antirice (Priest) on Jul 07, 2004 at 01:47 UTC

    A way around this is to actually define the entry in %INC. For instance:

    package Foo::Bar; BEGIN { $INC{"Foo/Bar.pm"} = $0; use base "Exporter"; @EXPORT = "hi"; } sub hi { print "Hi from Foo::Bar" } package main; use Foo::Bar; hi();

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1