Rose::...::MixIn(3pm)

πŸ“¦
version 0.860 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

Rose::Object::MixIn πŸ’–

okay bestie, welcome to the Rose::Object::MixIn page (it lives in the perl docs as Rose::...::MixIn(3pm)) β€” it's short, it's sweet, and it's literally the blueprint for every mix-in you're about to write. let's get into it πŸŽ€


✨ NAME ✨

Rose::Object::MixIn β€” a base class for mix-ins. that's it. that's the whole name section. iconic. πŸ’…


🌸 SYNOPSIS 🌸

package MyMixInClass;

use Rose::Object::MixIn(); # Use empty parentheses here
our @ISA = qw(Rose::Object::MixIn);

__PACKAGE__->export_tag(all => [ qw(my_cool_method my_other_method) ]);

sub my_cool_method  { ... }
sub my_other_method { ... }
...

package MyClass;
# Import methods my_cool_method() and my_other_method()
use MyMixInClass qw(:all);
...

package MyOtherClass;
# Import just my_cool_method()
use MyMixInClass qw(my_cool_method);
...

package YetAnotherClass;
# Import just my_cool_method() as cool()
use MyMixInClass { my_cool_method => 'cool' }

see how it flows? that's the whole vibe. 🌈


πŸ¦‹ DESCRIPTION πŸ¦‹

Rose::Object::MixIn is a base class for mix-ins. and what even is a mix-in, you ask? it's a class that exports methods into another class. that's the whole thing. it's giving "sharing is caring" but for methods. 🫢

the export process is controlled with an Exporter-like interface β€” but Rose::Object::MixIn does not inherit from Exporter. nope. not even a little. don't come for me about it.

when you use a Rose::Object::MixIn-derived class, its import method gets called at compile time. so like, this:

use Rose::Object::MixIn 'a', 'b', { c => 'd' };

is literally the same thing as this:

BEGIN { Rose::Object::MixIn->import('a', 'b', { c => 'd' }) }

same energy, different font. πŸ’…

now β€” if you do not want the import method to run, put empty parentheses () after the package name instead of a list of arguments.

use Rose::Object::MixIn();

the synopsis up there has an example of when this is handy: using Rose::Object::MixIn from within a subclass. and bestie, the empty parentheses are important. this next one is not equivalent:

# This is not the same thing as the example above!
use Rose::Object::MixIn;
⚠️ no cap: those two lines are different. one of them runs import, one of them doesn't. choose wisely. 😭

wanna know what arguments import actually accepts? scroll down to the docs for the import method. it's right there. I got you. πŸ‘‘


πŸ’œ CLASS METHODS πŸ’œ

import ARGS

import the methods specified by ARGS into the package from which this method was called. πŸ’«

but β€” if the current class can already perform one of these methods, a fatal error will occur. yes, fatal. not a warning, not a little nudge. fatal. πŸ’€

to override an existing method, you must use the -force argument (more on that below).

valid formats for ARGS are as follows:

  • a method name β€” literal method names will be imported as-is. no notes. πŸŽ€
  • a tag name β€” tag names are indicated with a leading colon. for example, :all specifies the all tag. a tag is a stand-in for a list of methods. wanna know how to create tags? see the export_tag method. πŸ¦‹
  • a reference to a hash β€” each key/value pair in this hash contains a method name and the name that it will be imported as. use this feature to import methods under different names so you can dodge conflicts with existing methods. rename and slay. ✨
  • -force β€” the special literal argument -force will cause the specified methods to be imported even if the calling class can already perform one or more of those methods. πŸ’ͺ
  • -target_class CLASS β€” the special literal argument -target-class followed by a class name will cause the specified methods to be imported into CLASS rather than into the calling class. πŸ“¦

see the synopsis for several examples of the import method in action. and remember, it's called implicitly when you use a Rose::Object::MixIn-derived class with anything other than an empty set of parenthesis () as an argument. so yeah, it's doing stuff behind your back. in a cute way. πŸ™ˆ

clear_export_tags

delete the entire list of export tags. all of them. gone. bye. πŸ’€βœ¨

export_tag NAME [, ARRAYREF]

get or set the list of method names associated with a tag. the tag name should not begin with a colon. (we're not doing the colon thing here, bestie, that's for import.) if ARRAYREF is passed, then the list of methods associated with that specific tag is set. πŸ”₯

returns a list (in list context) or a reference to an array (in scalar context) of method names. the array reference return value should be treated as read-only β€” don't go mutating it, that's not your business. πŸ™…

if no such tag exists, and if an ARRAYREF is not passed, then a fatal error will occur. so yeah. check your tag names. 😭

export_tags

returns a list (in list context) and a reference to an array (in scalar context) containing the complete list of export tags. the array reference return value should be treated as read-only. same rules as above. hands off. 🫢


πŸ‘‘ AUTHOR πŸ‘‘

John C. Siracusa (siracusa@gmail.com) β€” the icon who made all of this happen. 🌟


πŸŽ€ LICENSE πŸŽ€

Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


and that's the whole page, bestie. πŸ¦‹ this was Rose::...::MixIn(3pm) from the perl v5.34.0 docs, dated 2022-05-28 β€” go forth and mix in your methods responsibly. mwah. πŸ’–πŸ’…

πŸ“¦ where this page comes from