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:

  1. File-based: loaded from a source or object file. πŸ—‚οΈ
  2. Subcomponents: the embedded ones you define with the <%def> or <%method> tags. πŸͺ†
  3. Anonymous: created on-the-fly with the make_component Interp 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 returns undef if 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, undef otherwise.
  • 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 for load_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') or undef if 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 Perl time() 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, undef otherwise.
  • methods β€” this method works exactly like the subcomps method, 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 nearest autohandler in or above the component's directory. can be changed via the inherit flag. πŸ‘‘
  • path β€” returns the entire path of this component, relative to the component root.
  • scall_method (name, args...) β€” like item_call_method, but returns the method output as a string instead of printing it. (think sprintf versus printf.) 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, or undef if 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