in reply to Re: Multiple Packages in a Module?
in thread Multiple Packages in a Module?

Anonymous Monk:   Note also that in a file containing multiple package namespaces, a block structure can be very useful to achieve complete lexical isolation within each package:
    package Foo { my $private; ...; ... }  # perl version 5.14+
    package Bar { my $private; ...; ... }
or (note required semicolon after package statement)
    { package Foo;  my $private; ...; ... }  # pre-5.14 perl
    { package Bar;  my $private; ...; ... }
(The pre-5.14 scoping will, of course, continue to work with version 5.14+.) And definitely make use of the life-simplifying parent module!

Update: And in any version of Perl 5, this scoping can be used to implement absolutely private package/class functions/methods:

package Foo { ... my @stuff = ( ... ); # initialized upon inclusion ... my $private_function = sub { ... }; ... my $private_class_method = sub { my $class = shift; ...; }; my $private_object_method = sub { my $obj_ref = shift; ...; }; ... $private_function->(@stuff); ... $class_name->$private_class_method(...); $object_reference->$private_object_method(...); ... }
(This assumes that all these separate packages are contained in a standard .pm module that is use-ed or require-ed in the, well, usual way so that phasing problems are eliminated — or at least minimized.) The naming is a bit misleading in that both the  $private_class_method and  $private_object_method methods can be invoked with either a class name or an object reference, although what you do with the class name/object reference therein is another story! But they're still private.

All this, it must be said, starts to sound like a real OO system, of which there are many thorough-going implementations in CPAN.


Give a man a fish:  <%-{-{-{-<