HTML::...ponent(3pm)
π HTML::Mason::Component β the tea on Mason's Component class
okay bestie, sit down, hydrate, because we're about to get so cozy with HTML::Mason::Component, aka the Mason Component Class, from the HTML::...ponent(3pm) manual page π this is the class Mason uses to keep components chilling in memory, and honestly? it's giving backstage pass energy. β¨
β¨ SYNOPSIS β¨
my $comp1 = $m->current_comp;
my $comp2 = $m->callers(1);
my $comp3 = $m->fetch_comp('foo/bar');
foreach ($comp1,$comp2,$comp3) {
print "My name is ".$_->title.".\n";
}
likeβ¦ look at her. three comps, one loop, all of them introducing themselves. iconic behavior fr π
πΈ DESCRIPTION πΈ
so Mason uses the Component class to store components that have been loaded into memory. and babes, those components come from three distinct sources:
- File-based: loaded from a source or object file. ποΈ
- Subcomponents: the embedded ones you define with the
<%def>or<%method>tags. πͺ - Anonymous: created on-the-fly with the
make_componentInterp method. π«₯
and here's the thing β some of the methods below will hand you different values (or literally nothing at all) depending on which type of component you're dealing with. so like, vibe check your component type first, okay? π
the component API is primarily useful for introspection, which is a fancy way of saying "who called me??" or "does the next component even take that argument??". lowkey, you can build super complex Mason sites without ever touching a component object directly. no cap.
π«Ά CREATING AND ACCESSING COMPONENTS π«Ά
the common ways to get your hands on existing component objects are the Request->current_comp, Request->callers, and Request->fetch_comp methods. those are your entry points, bestie.
there is no published new method, because creating a component requires an Interpreter. so instead, use the make_component method when you want to dynamically create a new component. that's the move. π
similarly, there is no execute or call method, because calling a component requires a request. all of the interfaces for calling a component (<& &>, $m->comp, $interp->exec) that normally take a component path will also take a component object. so you're covered either way, periodt.
π METHODS π
attr (name)β looks for the specified attribute in this component and its parents, and returns the first value it finds. if it's not found, it dies with an error π. attributes are declared in the<%attr>section.attr_if_exists (name)β works exactly like the one above, but returnsundefif the attribute doesn't exist. chill version. π§attr_exists (name)β returns true if the specified attribute exists in this component or one of its parents,undefotherwise.attributesβ returns a hashref containing the attributes defined in this component, with the attribute names as keys. heads up though: this does not return attributes inherited from parent components. it's a you-and-your-own-attrs situation. πcall_method (name, args...)β looks for the specified user-defined method in this component and its parents, and calls the first one found. if it's not found, it dies with an error π. methods are declared in the<%method>section.create_timeβ a synonym forload_time. deprecated, so maybe don't build your whole personality around it.declared_argsβ returns a reference to a hash of hashes representing the arguments declared in the<%args>section. the keys of the main hash are the variable names including prefix (e.g.$foo,@list). each secondary hash contains:'default': the string specified for default value (e.g.'fido') orundefif none specified. note that in general this is not the default value itself but rather a Perl expression that gets evaluated every time the component runs.
for example:
# does $comp have an argument called $fido?
if (exists($comp->declared_args->{'$fido'})) { ... }
# does $fido have a default value?
if (defined($comp->declared_args->{'$fido'}->{default})) { ... }
dir_pathβ returns the component's notion of a current directory, relative to the component root; this is used to resolve relative component paths. for file-based components this is the full component path minus the filename. for subcomponents this is the same as the component that defines it. undefined for anonymous components.flag (name)β returns the value for the specified system flag. flags are declared in the<%flags>section and affect the behavior of the component. and unlike attributes, flag values do not get inherited from parent components. no generational wealth for flags, sorry. πis_subcompβ returns true if this is a subcomponent of another component. and for historical reasons, this returns true for both methods and subcomponents. lore. πis_methodβ returns true if this is a method.is_file_basedβ returns true if this component was loaded from a source or object file. ποΈload_timeβ returns the time (in Perltime()format) when this component object was created.method_exists (name)β returns true if the specified user-defined method exists in this component or one of its parents,undefotherwise.methodsβ this method works exactly like thesubcompsmethod, but it returns methods, not subcomponents. and again, it does not return methods inherited from parent components.
methods are declared in <%method> sections. π
nameβ returns a short name of the component. for file-based components this is the filename without the path. for subcomponents this is the name specified in<%def>. undefined for anonymous components.object_fileβ returns the object filename for this component.parentβ returns the parent of this component for inheritance purposes β by default, the nearestautohandlerin or above the component's directory. can be changed via theinheritflag. πpathβ returns the entire path of this component, relative to the component root.scall_method (name, args...)β likeitem_call_method, but returns the method output as a string instead of printing it. (thinksprintfversusprintf.) the method's return value, if any, is discarded. ποΈsubcompsβ with no arguments, returns a hashref containing the subcomponents defined in this component, with names as keys and component objects as values. with one argument, returns the subcomponent of that name, orundefif no such subcomponent exists. e.g.
if (my $subcomp = $comp->subcomps('.link')) {
...
}
subcomponents are declared in <%def> sections. πͺ
titleβ returns a printable string denoting this component. it's intended to uniquely identify a component within a given interpreter, although this is not 100% guaranteed. Mason uses this string in error messages, among other places.
for file-based components this is the component path. for subcomponents this is parent_component_path:subcomponent_name. for anonymous components this is a unique label like [anon 17]. β¨
π FILE-BASED METHODS π
the following methods apply only to file-based components (those loaded from source or object files). they return undef for other component types. sorry to everyone else, this section is not for you. π
source_fileβ returns the source filename for this component.source_dirβ returns the directory of the source filename for this component.
aaaand that's the whole page, bestie! π this one ships with perl v5.38.2, dated 2024-03-05, straight from the HTML::...ponent(3pm) page in the User Contributed Perl Documentation. go forth and introspect responsibly, ily π«Άπ
π¦ where this page comes from
- debian trixie: libhtml-mason-perl 1:1.60-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libhtml-mason-perl 1:1.60-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libhtml-mason-perl 1:1.60-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms