|
protect - allows the declaration of protected subs |
protect - allows the declaration of protected subs
package FooPackage; use protect;
members qw ( Bar Baz );
sub foo {
is private;
print "Only FooPackage can use me\n";
}
sub foz {
is member;
print "Only packages FooPackage, Bar, and Baz can use me.\n";
}
sub fail {
is public;
print "Anybody can use me!\n";
}
This module allows you to C<protect> your subs from outside callers, in a way similar to C++ or Java. You can declare member subs, private subs, or public subs. The truth is, unfortunatly, that the subs aren't actually being declared, public, member, or private. What actually happens is a juicy bit of runtime checking, with perl's C<caller()> function to see where the subroutine call is coming from.
C<is> is allows you to declare set the subroutine as one of three modes.
The following are all modes that can be set by using the is function.
By setting your subroutine as C<private>, no-one can call it from outside the package that it was declared in. Doing so will cause the program to die.
By setting the C<member> mode, only the current package and those packages that are declared as C<members> of the current package are allowed to call the subroutine.
By declaring C<public> you are actually doing nothing at all, but it looks nice to declare a function public, when every other sub is declared something or other.
C<members> allows you to declare members of the current package family, and therefore use subroutines declared as member. members'ing is one way only. ie 'Just because you lent me your basketball, doesn't mean I'll lend you mine'.
C<nowarn> allows you to have protect simply die without explaining why it did so. This is in order to help restrict attacks where it is possible for people to see the runtime errors generated by the script.
C<private> and C<member> methods/functions can still be redefined without the private/member attribute.
Both private and member methods/functions can be called by simply entering the package that they were declared in.
C<protect> now exports a lot less into the namespace, thanks to the wonders of C<is>.
C<members> and C<member> functions now work like they should.
James Duncan <jduncan@hawk.igs.net>
perl(1), perlfunc(1)
|
protect - allows the declaration of protected subs |