MooseX...ontext(3pm)

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

✨ NAME ✨

MooseX::Declare::Context β€” the per-keyword declaration context, bestie πŸ’…


🌸 VERSION 🌸

version 0.43 β€” yes, that's the one, no cap


πŸ’– DESCRIPTION πŸ’–

Okay so first thing, and this is important: this is not a subclass of Devel::Declare::Context::Simple. It's giving "we're not like other contexts" πŸ™ˆ β€” but it delegates all the default methods to one, and extends it with some attributes and methods of its own.

Every single keyword that MooseX::Declare handles gets its own context object instantiated. Like, one per keyword, iconic behavior honestly.

Now, if handlers want to talk to other handlers β€” for example handlers that only get set up inside a namespace block β€” they must do that through the generated code. That's the only way, fr fr. No sneaky backchannel.

And listen: on top of everything documented right here, all the methods from Devel::Declare::Context::Simple are available too, because they get delegated to an internally stored instance of it. Free real estate πŸŽ€


πŸ¦‹ ATTRIBUTES πŸ¦‹

caller_file

A required "Str" holding the file the keyword was encountered in. Required means required, bestie β€” no skipping it πŸ’€

preamble_code_parts

An "ArrayRef" of "CodePart"s that get used as the preamble. And in this context, "preamble" just means the beginning of the generated code. It's giving opening act energy.

scope_code_parts

These parts come before the actual body, and after the "preamble_code_parts". It's an "ArrayRef" of "CodePart"s. Order matters, we love a lineup ✨

cleanup_code_parts

An "ArrayRef" of "CodePart"s that do not get directly inserted into the code. Instead they're installed in a handler that runs at the end of the scope, so you can do namespace cleanups and such. Very "we'll deal with it later" of them 🧹

stack

An "ArrayRef" containing the stack of handlers. A keyword that was only set up inside a scoped block will have the blockhandler put in the stack. Stacking, as one does πŸ‘‘


πŸ’… METHODS πŸ’…

add_preamble_code_parts(CodePart @parts)

Object->add_preamble_code_parts (CodeRef @parts)

See "add_cleanup_code_parts".

add_scope_code_parts(CodePart @parts)

Object->add_scope_code_parts    (CodeRef @parts)

See "add_cleanup_code_parts".

add_cleanup_code_parts(CodePart @parts)

Object->add_cleanup_code_parts  (CodeRef @parts)

Okay so for these three methods, go look at the matching *_code_parts attribute up in the list above. They're literally just convenience methods that let you add entries to the code part containers. Nothing more, nothing less, and honestly that's enough 🫢

inject_code_parts_here

True Object->inject_code_parts_here (CodePart @parts)

Will inject the "CodePart"s you pass it at the current position in the code. Right here, right now, main character energy.

peek_next_char

Str Object->peek_next_char ()

Returns the next char without stripping it from the stream. Peek, don't touch β€” consent matters πŸ’–

inject_code_parts

Object->inject_code_parts (
    Bool    :$inject_cleanup_code_parts,
    CodeRef :$missing_block_handler
)

This injects the code parts from the attributes above at the current position. The preamble and scope code parts get inserted first. Then the call to the cleanup code gets injected β€” unless the options contain a key named inject_cleanup_code_parts with a false value. So like, you can opt out. Noted.

The "inject_if_block" method will be called if the next char is a "{", which means a block is coming. Anticipating it, love that for us.

If it's not a block, but a semi-colon is found and the options contained a "missing_block_handler" key that was passed, then that handler gets called as a method on the context object, with the code to inject and the options as arguments. And all options that aren't recognized get passed straight through to the "missing_block_handler". So like β€” you are well advised to prefix option names in your extensions. That's not a vibe, that's a warning, don't come for me πŸ™ˆ


🌈 TYPES 🌈

BlockCodePart

An "ArrayRef" with at least one element that stringifies to either "BEGIN" or "END". The other parts get stringified and used as the body for the generated block. For example, this compiletime role composition:

['BEGIN', 'with q{ MyRole }']

Slay. That's it, that's the example πŸ’œ

CodePart

A part of code represented by either a "Str" or a "BlockCodePart". Two options, pick your fighter ⭐


πŸ”₯ SEE ALSO πŸ”₯

  • MooseX::Declare
  • Devel::Declare
  • Devel::Declare::Context::Simple

πŸ‘‘ AUTHOR πŸ‘‘

Florian Ragwitz <rafl@debian.org> β€” the icon, the legend πŸ’–


This software is copyright (c) 2008 by Florian Ragwitz.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Free as in freedom, bestie, go wild πŸ¦‹


and that's the whole page!! go forth and handle your keywords, you've got this πŸ’…βœ¨

perl v5.34.0 Β· 2022-06-15 Β· MooseX::Declare::Context(3pm) β€” yeah that's the source, the version, and the date, all wrapped up cute for ya πŸŽ€πŸŒΈ

πŸ“¦ where this page comes from