http://www.perlmonks.org?node_id=439990


in reply to Writing Modules/namespace polution

You're confusing exporting with object-oriented programming. You don't need Exporter to have an object, which is what you have. Change your script to:
#!/usr/bin/perl use strict; use warnings; use mymodule; my $m = mymodule->new(); print "My Test\n"; $m->Func1(); $m->Func2("Fluffy", 5); Func1(); Func2("Fluffy", 5); sub Func1 { print "Func1() in main!\n" } sub Func2 { print "I'm still in main! (@_)\n" }

See what happens?

Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Replies are listed 'Best First'.
Re^2: Writing Modules/namespace polution
by thekestrel (Friar) on Mar 16, 2005 at 16:15 UTC
    The little light goes on =).... Thanks...I'm trying to do a OO method, but I didn't realize the object had all the methods by default, which makes sense as they are essentially protected by virtue of the fact that you have to use the object.
      they are essentially protected by virtue of the fact that you have to use the object

      I would be careful about calling much in Perl protected. Sure, general politeness will keep most people from using the package in unintended ways, but Perl sure won't:

      Module

      package Example::Module; use strict; sub new { bless {}, shift } sub foo { my ($s, $v1, $v2) = @_; print "Hi '$v1' and '$v2'\n"; } 1;

      Script

      use strict; use Example::Module; # add a new sub to the package sub Example::Module::bar { my ($s, $v1, $v2) = @_; print "Bye '$v1' and '$v2'\n"; } my $em = Example::Module->new; $em->bar("one", "two"); # call the sub not as a method Example::Module::foo('one', 'two', 'three'); # change the existing sub *Example::Module::foo = sub { my ($s, $v1, $v2) = @_; print "Oops '$v1' and '$v2'\n"; }; $em->foo("one", "two");
        Thanks Mugatu,

        I see what you mean, protection is a relative term when you have ahold of the object. Is there a way of privatizing variables/member functions of a package as you would with a C/C++ class so you can limit the interface size, otherwise without reading about each function via documention for the package its not clear from quickly scanning the code which functions are meant to be part of the interface?


        Regards Paul.