3

I wanted to export a simple function from a base class that I use all over my sub classes without having to use $self->myfunc() all the time, just a simple func() call.

I tried doing this with the example from Moose::Exporter

but I didnt understand what

as_is     => [ 'sugar3', \&Some::Random::thing ],

was doing, as the example seems incomplete. sugar3 isnt defined anywhere, so I dunno where or how to use it. Can I call sugar3() in the subclass now? Is sugar3() some secret Moose thing?

and then the was this thing... literally

thing;

that was exported, but I have no idea what thing is doing since there is no example of it. Is this a function call?

Anyway, more to the point how do you export functions like you would normally do with Exporter, but with Moose Exporter instead, and what happens if my baseclass has 3 levels of inheritance after it, will all the sub sub classes have access to this exported function?

qodeninja
  • 10,946
  • 30
  • 98
  • 152
  • 1
    This isn't much of a help, but thinking about trying to mash OO inheritance and package syntax like this just gives me the heebie-jeebies. Are you sure there's not a better way to do what you're trying to do? Besides, I'm fairly certain (though I could also very easily be wrong) that a use parent 'Whatever' doesn't end up importing functions into the local package, anyway, so it doesn't sound any different than the way Exporter works. – Oesor Oct 14 '11 at 20:28
  • im not trying to mash anything i just use a function call a lot to debug print and test things but I'll clean it up when im done =]. In any case I still would like to understand how this works. +1 thanks for sharing ur thoughts about this – qodeninja Oct 14 '11 at 20:29
  • 2
    `$self->myfunc()` and `myfunc()` are two very different things, and you shouldn't be writing a method and then trying to export it as a sub. An exported *function* will not have access to the object - that is, it is not *object oriented*. – Ether Oct 14 '11 at 21:36
  • +1 Good point. It seems like I shouldn't be trying to do this in Moose, but then why would Moose provide an exporter if we werent to export anything? – qodeninja Oct 14 '11 at 22:58

1 Answers1

3

as_is => [ ... ]:

This list of function names or sub references will be exported as-is. You can identify a subroutine by reference, which is handy to re-export some other module's functions directly by reference (\&Some::Package::function).

sugar3 is the name of a sub to export.

Yes, you can call sugar3 in the subclass now if that's where you exported it to. That said, it's usually weird to export (anything but constants) to a subclass.

Yes, thing; is a sub call. Under no strict;, it could also be the same as 'thing';.

The sub classes won't have access to the sub unless it's called as a method (e.g. $o->thing; instead of thing;). It's extremely weird to export methods, though. Create a Moose::Role to give methods to a class.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • so is this essentially exporting -- using Roles? Is there a way not to have to type $self->func() everytime? -- not like Im going to die if I cant =P the problem is some of the older libary dont use Moose and I have to still use what they export, so why cant I do it in Moose instead? – qodeninja Oct 14 '11 at 22:33
  • 1
    @nodebunny, `$self->func()` is required for methods. I didn't say you can't export methods, I said it's extremely weird. – ikegami Oct 14 '11 at 22:59