Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

$client is doing a lot of heavy lifting (perhaps accidentally) in some Catalyst-driven Template Toolkit code. Apparently, it's far too easy to pass a DBIx::Class object into the stash, and then trigger things that end up hitting the database... from the view. That wouldn't be horrible, except the same exact queries are being used in multiple places in the templates, causing many redundant identical queries to the database per page hit. (Note aside: if this was Rose::DB... I'd trivially jump to Rose::DB::Object::Cached. Problem solved.)

I wanted a way to see what method calls were being invoked on the objects in the stash. Simple... just subclass Template::Stash! Now I get a list of the component invoking the method, the path from the root stash to the object, and the method call being made.

package Test::Stash; use base qw(Template::Stash); use strict; use warnings; use Scalar::Util qw( blessed reftype ); use Template::Config; our @PATH; our $BYPASS = 0; sub _dotop { my ($self, $root, $item, $args, $lvalue) = @_; unless ($BYPASS) { my $rootref = ref $root; my $atroot = (blessed $root && $root->isa(ref $self)); if ($atroot) { @PATH = $item; } else { push @PATH, $item; } if (blessed $root && $root->can($item)) { # likely a method call if ($root =~ m{ \A( DBIx::Class::QueryLog(::(Query|Analyzer))? | # (redacted Catalyst model class) | Template::Plugin::(Math|Date) | Moose::Meta::Class:: .*? )=}x) { ## ignore these } else { local $BYPASS = 1; my $component = $self->get(['component', 0, 'name', 0]); warn sprintf "%s: %s => %s\n", $component, join('.', @PATH), $root; } } } return (shift @_)->SUPER::_dotop(@_); } # warn "Loaded Test::Stash... before is $Template::Config::STASH"; $Template::Config::STASH = __PACKAGE__; # warn "Loaded Test::Stash... after is $Template::Config::STASH"; 1;

-- Randal L. Schwartz, Perl hacker

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

In reply to Subclassing Template::Stash to identify improper heavy lifting by merlyn

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [choroba]: Do you mean nettop?

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (5)
    As of 2018-04-20 19:17 GMT
    Find Nodes?
      Voting Booth?