Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Template Toolkit: silencing pop() and the other list virtual methods

by jbullock35 (Hermit)
on Dec 05, 2005 at 07:08 UTC ( #514053=perlquestion: print w/ replies, xml ) Need Help??
jbullock35 has asked for the wisdom of the Perl Monks concerning the following question:

A TT2 curiosity: pop does what it always does—but when I have template code embedded in HTML, it also inserts the popped value into my HTML, causing it to be displayed on screen. For example,
[% list = [1, 2, 3] %] [% list.pop %]
removes "3" from the list and inserts it into my HTML. I don't want it to insert anything into my HTML. I just want the list to contain one less value. I see a lot of ways to do this, e.g.,
[% list = [1, 2, 3] %] [% p = list.pop %]
but I don't like any of them. The example I've provided is unsatisfactory because I've no use for the popped value. I would use something like
[% list = [1, 2, 3] %] [% list = list.slice(0,1) %]
but I won't always know the length of the list in advance. Isn't there a simple standard way to do what I want?

Many thanks. I searched the TT mailing list but couldn't find anything speaking to this.

Comment on Template Toolkit: silencing pop() and the other list virtual methods
Select or Download Code
Re: Template Toolkit: silencing pop() and the other list virtual methods
by EvanCarroll (Chaplain) on Dec 05, 2005 at 07:12 UTC
    Try this:
    [% list = [1, 2, 3] %] [% undef = list.pop %]
    If it gives you a warning you might have to improvise with something of this like:
    [% [undef] = list.pop %]


    Evan Carroll
    www.EvanCarroll.com
Re: Template Toolkit: silencing pop() and the other list virtual methods
by PodMaster (Abbot) on Dec 05, 2005 at 07:44 UTC
    'perldoc Template::Manual::Directives'
    CALL

    The CALL directive is similar to GET in evaluating the variable named, but doesn't print the result returned. This can be useful when a variable is bound to a sub-routine or object method which you want to call but aren't interested in the value returned.

    # run tpage tt.just.pop.pl # expect no numbers OUTPUT: [% list = [1, 2, 3] %][% CALL list.pop %]

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Template Toolkit: silencing pop() and the other list virtual methods
by holli (Monsignor) on Dec 05, 2005 at 07:54 UTC
    You can try
    [% CALL list.pop %]
    or
    [% PERL %] $stash->get('list')->pop; #or maybe #pop @{$stash->get('list')}; [% END %]
    The latter is untested.


    holli, /regexed monk/
      If you enable debugging (DEBUG => 99), you can see how Template compiles the templates (that [% CALL list.pop %] becomes $stash->get(['list', 0, 'pop', 0]);, and [% list.pop %] becomes $output .=  $stash->get(['list', 0, 'pop', 0]);).

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Template Toolkit: silencing pop() and the other list virtual methods
by jbullock35 (Hermit) on Dec 06, 2005 at 09:04 UTC
    Thanks to all. These replies are quite helpful.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://514053]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2015-07-01 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (24 votes), past polls