>>>>> "t" == trapd00r  <trapd...@trapd00r.se> writes:

  >> i would say to just use a temporary scalar variable. there is no shame
  >> in doing this and it is simpler than using the Interpolation module
  >> which is doing tied things and calling eval (which is dangerous).

  t> When I dont want to use a temp var, I usually do like this:

  t> print << "EOF";
  t>   foo @{[scalar(localtime)]} bar
  t> EOF

  t> Do you approve? I do not see the benefit of using that Interpolation
  t> module.

that technique is old and not well liked by many. i never use it. it is
just too noisy for my taste. also you can do a scalar version with
${\EXPR} but it also supplies a list context so there is little benefit
over the array version.

if you are just putting in a couple of elements then temp vars are fine
and very clear. 

in a boston.pm thread on the same subject, the OP wrote his own
version of the Interpolate module including the tied object and
eval. that is just plain overkill.

check out these methods i listed in that thread:

**********************

first off there are the old hackish ways of doing this with
dereferencing anon refs with code inside:

        "text${\$foo->method()}more text"
        "t...@{[$foo->method()]}more text"

note that in both cases the method (or sub) is called in list
context. this style is frowned upon for production code as it is noisy
and not easy to read.

an old reliable way is using sprintf as long as you don't have too many
fields which again makes it noisy.

        sprintf "text%smore text", $foo->method() ;

if you want more templating without all the eval fuss and tied stuff,
try my module Template::Simple. it would be very easy to use this for
method interpolation:

use Template::Simple ;

        my $tmpl = Template::Simple->new() ;

        my $out = render( "text[%STUFF%]more text",
                        { STUFF => $foo->method() } ) ;

so no eval or tied is ever needed for basic stuff like interpolating
methods and subs.


three more ways i forgot to add. there is no shame or anything wrong with
first putting the value of a method or sub call into a scalar and
interpolating that.

        my $val = $foo->method() ;
        my $out = "text${val}more text" ;

since you can interpolate hash values, a simple templater can be done
this way:

        my %vals = ( FOO => $foo->method(), BAR => $bar->other_method ) ;
        my $out = "text$vals{FOO}more text$vals{BAR}" ;

and the old way of using . is still fine even if i don't like it:

        my $out = "text" . $foo->method() . "more text" ;

i just hate using . by itself. hard to read and rarely needed. note that
i use .= a ton of times which is not the same. :)

so that is 6 ways to do it without eval and tied. :)

****************************

the point is choose a way to do it with the least work, least pain and
most clarity for the reader of your code. the interpolate module (and
copies) is dangerous because of its use of eval and slow (tied objects,
tons of code to do something simple).

uri

-- 
Uri Guttman  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to