Ok No is not Moose

A many readers of this blog know I have been spending a good deal of time lately playing about with Moose doing all sorts of silly little things with AD&D character creation and alike.

Well I was curious today and started to take apart a little bit of Moose and see what was under the hood so to speak. So far I have discover that all the nice Moose sugar are really just function calls and since some programming I was doing today requires me to use 'no Moose', he much preferred 'namespace::autoclean;' not being install on the box I was playing with yet.

So I started to look in the Moose dir of my perl for the 'sub no' the one that takes away all the sugar. Well I tired every combination of 'sub no' I could think of with 'ack', 'Grep' and even loading things on Padre to see the outline result.

Nothing couldn't find it anywhere???

Well like I have said a few times before perl gave me another surprise it seems 'no' is a core perl function

and most likely has the shortest entry

'See the use function, of which no is the opposite.'

as well.

Sure enough a little reading later at the very bottom of the use I found out what it does

There's a corresponding no declaration that unimports meanings imported by use, i.e., it calls unimport Module LIST instead of import.

Having played with the 'import' bits of perl before I myself did not need to go any further in researching it.

It could see it usefulness back in the days when 64k as alot of free ram to play with, as good deal of early perl code was to process suff so if you used something and didn't need it anymore you could get rid of it with 'no'

Funny it is when I started playing with 'Post Modern' perl that I ran into a function from the dark dark ages??

Oh well at least Moose gave 'no' some new life.


3 Comments

no is not a memory saving technique. The module is still loaded in memory after calling it. It's just a hook for modules to tidy up after themselves, or switch off their effects. For example:

my $sum = do {
  no warnings "uninitialized";
  $thing_that_might_be_undef + $another_thing_that_might_be_undef;
};

In Moose's case, it removes the has, extends, etc keywords from your namespace, which prevents people from trying to do silly things like $object->extends() on your objects.

no is not just for Moose et al. If you've ever done something like:

    no warnings qw(uninitialized);

you're using the same thing.

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations