<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Moose - Tag - blog::weyl.io</title>
    <link>https://weyl.io/tags/moose/</link>
    <description>Chris Weyl&#39;s technical blog</description>
    <generator>Hugo 0.155.3 &amp; FixIt v0.4.0-alpha.3-20251225101113-8ffb9a95</generator>
    <language>en-us</language>
    <managingEditor>chris@weyl.io (Chris Weyl)</managingEditor>
    <webMaster>chris@weyl.io (Chris Weyl)</webMaster>
    <lastBuildDate>Wed, 08 Apr 2015 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://weyl.io/tags/moose/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>MX::AttributeShortcuts -- now with Moo-style type constraints</title>
      <link>https://weyl.io/2015/04/mxas-and-moo-constraints/</link>
      <pubDate>Wed, 08 Apr 2015 00:00:00 +0000</pubDate><author>chris@weyl.io (Chris Weyl)</author>
      <guid>https://weyl.io/2015/04/mxas-and-moo-constraints/</guid>
      <category domain="https://weyl.io/categories/perl/">Perl</category>
      <description>&lt;p&gt;I just released &lt;a href=&#34;https://metacpan.org/pod/MooseX::AttributeShortcuts&#34; target=&#34;_blank&#34; rel=&#34;external nofollow noopener noreferrer&#34;&gt;MooseX::AttributeShortcuts&lt;i class=&#34;fa-solid fa-external-link-alt fa-fw fa-xs ms-1 text-secondary&#34; aria-hidden=&#34;true&#34;&gt;&lt;/i&gt;&lt;/a&gt;&#xA;0.028; it incorporates &lt;a href=&#34;https://metacpan.org/pod/Moo&#34; target=&#34;_blank&#34; rel=&#34;external nofollow noopener noreferrer&#34;&gt;Moo&lt;i class=&#34;fa-solid fa-external-link-alt fa-fw fa-xs ms-1 text-secondary&#34; aria-hidden=&#34;true&#34;&gt;&lt;/i&gt;&lt;/a&gt;-style type constraints.&lt;/p&gt;&#xA;&lt;p&gt;&amp;hellip;largely because I needed to relax, and wrote&#xA;&lt;a href=&#34;https://metacpan.org/pod/MooseX::Meta::TypeConstraint::Mooish&#34; target=&#34;_blank&#34; rel=&#34;external nofollow noopener noreferrer&#34;&gt;MooseX::Meta::TypeConstraint::Mooish&lt;i class=&#34;fa-solid fa-external-link-alt fa-fw fa-xs ms-1 text-secondary&#34; aria-hidden=&#34;true&#34;&gt;&lt;/i&gt;&lt;/a&gt; :)&lt;/p&gt;&#xA;&lt;p&gt;That means you can now pass a coderef to has() in isa that, like with Moo,&#xA;dies on validation failure and lives on validation success:&lt;/p&gt;&#xA;&lt;!-- &lt;script src=&#34;https://gist.github.com/rsrchboy/2e2216c9a16fa06b345b.js&#34;&gt;&lt;/script&gt; --&gt;&#xA;&lt;pre&gt;&lt;code&gt;# easiest is via AttributeShortcuts&#xA;use MooseX::AttributeShortcuts 0.028;&#xA;&#xA;has foo =&amp;gt; (&#xA;  is =&amp;gt; &amp;#39;rw&amp;#39;,&#xA;  # $_[0] == the value to be validated&#xA;  isa =&amp;gt; sub { die unless $_[0] == 5 },&#xA;);&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    <item>
      <title>Cheap Caching with AutoDestruct</title>
      <link>https://weyl.io/2012/05/cheap-caching-with-autodestruct/</link>
      <pubDate>Fri, 25 May 2012 00:00:00 +0000</pubDate><author>chris@weyl.io (Chris Weyl)</author>
      <guid>https://weyl.io/2012/05/cheap-caching-with-autodestruct/</guid>
      <description>&lt;p&gt;I&amp;rsquo;ve seen a couple &lt;a href=&#34;http://blogs.perl.org/users/buddy_burden/2012/04/lazy-cache.html&#34; target=&#34;_blank&#34; rel=&#34;external nofollow noopener noreferrer&#34;&gt;references&lt;i class=&#34;fa-solid fa-external-link-alt fa-fw fa-xs ms-1 text-secondary&#34; aria-hidden=&#34;true&#34;&gt;&lt;/i&gt;&lt;/a&gt;&#xA;lately to using lazy attributes as a form of caching.  This is a great&#xA;approach to thinking about lazy attributes, as they share a number of&#xA;characteristics with traditional caching: you only have to build a&#xA;(potentially) expensive value once, and then only when you actually need it.&lt;/p&gt;&#xA;&lt;script src=&#34;https://gist.github.com/rsrchboy/2729554.js?file=lazy.pl&#34;&gt;&lt;/script&gt;&#xA;&lt;p&gt;But what about when that lazily generated value is too old to trust?&lt;/p&gt;&#xA;&lt;p&gt;A lazy attribute isn&amp;rsquo;t going to help you much then, as your instance is&#xA;quite happy to keep on returning the same value forever once it has been&#xA;built, unless you clear or change it manually.  This is no good when, say,&#xA;you&amp;rsquo;ve run a database query and you can really only expect your painfully&#xA;contorted query to get the twitter ids of all the left handed Justin Beiber&#xA;fans north of the Mason-Dixon line who own hypo-allergenic cats to be valid&#xA;for, oh, say 55 minutes or so.&lt;/p&gt;&#xA;&lt;p&gt;You could add an attribute to store the age of the value generated for the&#xA;lazy attribute and check it either manually (boring!), or by wrapping the&#xA;reader method (less boring, but still, unsightly).&lt;/p&gt;&#xA;&lt;script src=&#34;https://gist.github.com/rsrchboy/2729554.js?file=not-so-lazy.pl&#34;&gt;&lt;/script&gt;&#xA;&lt;p&gt;Ok, method modifiers can be fun, but still&amp;hellip;  That&amp;rsquo;s a lot of annoying&#xA;little code that, well, isn&amp;rsquo;t Moose there to help reduce that sort of code in&#xA;our lives?&lt;/p&gt;&#xA;&lt;p&gt;What we&amp;rsquo;re running into here is that while we implement one part of a cache&#xA;(generate once, return many), lazy attributes don&amp;rsquo;t have any internal logic to&#xA;determine when a value is no longer good.  They don&amp;rsquo;t even have any concept of&#xA;that, just &amp;ldquo;someone needed my value, so we&amp;rsquo;re going to get it and hang on to&#xA;it until told otherwise&amp;rdquo;.&lt;/p&gt;&#xA;&lt;p&gt;This is just the sort of behaviour an attribute trait can alter.&lt;/p&gt;&#xA;&lt;script src=&#34;https://gist.github.com/rsrchboy/2729554.js?file=very-lazy.pl&#34;&gt;&lt;/script&gt;&#xA;&lt;p&gt;The &lt;a href=&#34;https://metacpan.org/pod/MooseX::AutoDestruct&#34; target=&#34;_blank&#34; rel=&#34;external nofollow noopener noreferrer&#34;&gt;MooseX::AutoDestruct&lt;i class=&#34;fa-solid fa-external-link-alt fa-fw fa-xs ms-1 text-secondary&#34; aria-hidden=&#34;true&#34;&gt;&lt;/i&gt;&lt;/a&gt; Moose&#xA;attribute trait allows us to specify an expiration date for our stored values.&#xA; We can specify a time-to-live option at attribute creation, and then every&#xA;time a value is set, the set time is stored.  Every time the value is&#xA;accessed, the attribute checks to make sure the value isn&amp;rsquo;t older than the set&#xA;time to live, and if it is, clears the value.  This allows the lazy value&#xA;generation to kick in once more, without requiring any extra effort on the&#xA;part of the user &amp;ndash; just as one would expect.&lt;/p&gt;&#xA;</description>
    </item>
    <item>
      <title>Simulating multiple, lazy attributes</title>
      <link>https://weyl.io/2012/05/simulating-multiple-lazy-attributes/</link>
      <pubDate>Tue, 08 May 2012 00:00:00 +0000</pubDate><author>chris@weyl.io (Chris Weyl)</author>
      <guid>https://weyl.io/2012/05/simulating-multiple-lazy-attributes/</guid>
      <description>&lt;p&gt;Lazy attributes are wonderful.  They allow us to postpone generating attribute&#xA;values for any number of reasons:  it&amp;rsquo;s expensive and we don&amp;rsquo;t want to do it&#xA;unless we need it, it should be initialized after instantiation because it&#xA;depends on other attributes, etc.  And it does this without our having to&#xA;worry about the value being around: if we need it, it&amp;rsquo;ll be generated on the&#xA;fly without any extra effort on our part.&lt;/p&gt;&#xA;&lt;p&gt;As an example, let&amp;rsquo;s say we have a simple config file that defines key/value&#xA;pairs.  We need to find out the author&amp;rsquo;s name, which has the key &amp;lsquo;author&amp;rsquo; in&#xA;the config file.  We could create a lazy attribute as such:&lt;/p&gt;&#xA;&lt;script src=&#34;https://gist.github.com/rsrchboy/2639832.js?file=one.pl&#34;&gt;&lt;/script&gt;&#xA;&lt;p&gt;Simple, yes?  Now, whenever you need the authors name, you have it.&lt;/p&gt;&#xA;&lt;p&gt;So, let&amp;rsquo;s now say that a couple days later, you realize that you also need to&#xA;get the author&amp;rsquo;s email from the config file (key &amp;rsquo;email&amp;rsquo;):&lt;/p&gt;&#xA;&lt;script src=&#34;https://gist.github.com/rsrchboy/2639832.js?file=silly.pl&#34;&gt;&lt;/script&gt;&#xA;&lt;p&gt;Voila!&lt;/p&gt;&#xA;&lt;p&gt;Except&amp;hellip;  Hm.  We&amp;rsquo;re now loading and parsing the config file twice.  Though&#xA;it&amp;rsquo;s likely to be very low cost to do that (assuming a local, simple config&#xA;file on the filesystem), it still feels wrong.  Besides, what happens when you&#xA;run into a situation like this and the base set of data (e.g. what&#xA;load_config() is returning) is expensive to generate?&lt;/p&gt;&#xA;&lt;p&gt;There are a couple things we could do here: we could create a config&#xA;attribute, make it lazy and load the config, then change our attributes to&#xA;pull their value out of the config attribute; we could create a new class to&#xA;handle the config, and setup a config attribute that delegates to it; etc,&#xA;etc.  That&amp;rsquo;s a lot of work, however, and work that doesn&amp;rsquo;t need to be done if&#xA;we leverage other parts of Moose correctly.&lt;/p&gt;&#xA;&lt;p&gt;One of the easy, often overlooked ways to do this is to use the tools Moose&#xA;itself gives us: native attribute traits and accessor currying.&lt;/p&gt;&#xA;&lt;script src=&#34;https://gist.github.com/rsrchboy/2639832.js?file=attribute.pl&#34;&gt;&lt;/script&gt;&#xA;&lt;p&gt;In the above, we see one attribute being created.  Note the &lt;code&gt;is =&amp;gt; &#39;bare&#39;&lt;/code&gt;;&#xA;this keeps the attribute from generating the reader, writer or accessor&#xA;methods.  We&amp;rsquo;re applying the &amp;ldquo;Hash&amp;rdquo; native trait, and using the delegation it&#xA;provides to create custom accessors that pull from the hash without needing&#xA;the end user to provide keys to a generic lookup.&lt;/p&gt;&#xA;&lt;p&gt;Note that with either of these approaches gives us the same interface to&#xA;someone using our class:&lt;/p&gt;&#xA;&lt;script src=&#34;https://gist.github.com/rsrchboy/2639832.js?file=foo.pl&#34;&gt;&lt;/script&gt;&#xA;&lt;p&gt;This isn&amp;rsquo;t always appropriate, but if you ever find yourself with multiple&#xA;attributes whose values can all be generated through one builder, then this&#xA;may be a good starting approach.&lt;/p&gt;&#xA;&lt;p&gt;It&amp;rsquo;s certainly the laziest one  :)&lt;/p&gt;&#xA;</description>
    </item>
  </channel>
</rss>
