<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Keith's Web Blog RSS Feed</title>
<language>en-us</language>
<link>http://www.keithwatanabe.net/index.php</link>
<description>Keith Watanabe's Website</description>
<item>
<title>PHP Variable Interpolation</title>
<link>http://www.keithwatanabe.net/blogs/2008/1/28/4e5c75bba88c6ee08fe3cdf3d5aaabc6.html</link>
<description><![CDATA[As you progress in a language, you'll probably end up getting bored or better yet, attempting to figure out the limits of a language.  I'm getting to that point with <strong>PHP</strong>.  Along the way, I wanted to find some neat tricks to either improve the performance of my code, or to make my code more compact, even <strong>perl-like </strong>(i.e. one liners).  I find a neat little trick you can do in a string that's not very well known.   <br />
<br />
For the longest time, I would concatenate strings together if I wanted to use a method call or remove single quotes from hashes if I'd use them in a string.  For instance:<br />
<br />
$str = &quot;Hello &quot; . $this-&gt;getFirstName() . &quot; &quot; . $this-&gt;getLastName();<br />
$out = &quot;My name is $arr[fname] $arr[lname]&quot;;<br />
<br />
It's said that both have some performance issues.  In perl, the second one would be considered decent format, since they often discourage you from quote in a hash.  However, in PHP, the second form is considered a bit slower because of some internal conversions that occur with the interpreter.  So how can we handle this in a slicker manner?  Use <em>curly braces</em> {}. <br />
<br />
$str = &quot;Hello {$this-&gt;getFirstName()} {$this-&gt;getLastName()}&quot;;<br />
$out = &quot;My name is {$arr['fname']} {$arr['lname']}&quot;;<br />
<br />
Both styles are considered good form in PHP, if not bulking up your strings a bit.  But you can avoid nasty concatenations or some performance hits by doing this.<br />
<br />
Another little trick for handling variable interpolation, especially if you do a lot of inline messages (which isn't a good thing to do, but sometimes can't be helped), is using the <strong>heredoc</strong> format.  Heredoc is a nice technique for avoiding the string concatenation problem.  It makes the code appear a little uglier, but you don't take the performance hit of making something more readable by forcing the interpreter to work a little harder.  The format goes like this:<br />
<br />
$str&lt;&lt;&lt;EOL<br />
My favorite Japanese famous women are:<br />
1) Reina Miyauchi<br />
2) Norika Fujiwara<br />
3) Shiho<br />
4) Kaori Kawai<br />
5) Ryoko Yonekura<br />
EOL<br />
<br />
Perl and other Unix scripting languages provide similar mechanisms.  You can put variables and even make method calls in the heredoc as well.  In terms of when to use such a device, my recommendation is SQL when you need to create long statements and lack some nice ORM to handle this internally for you.  If you have to create email messages or insert HTML into your code, you might as well just skip right over to a templating system like Smarty instead.  Of course, if your application is small, then a heredoc wouldn't be considered out of the question either.]]></description>
<pubDate>Mon, 28 Jan 2008 08:12:22 -0700</pubDate>
<guid>http://www.keithwatanabe.net/blogs/2008/1/28/4e5c75bba88c6ee08fe3cdf3d5aaabc6.html</guid>
</item>
<item>
<title>Re-sharpening My Perl Skills</title>
<link>http://www.keithwatanabe.net/blogs/2008/4/23/13f7bab8a7369e6b5e5490db552a0a06.html</link>
<description><![CDATA[In my programmatic quest to shrink all lines of code into one line, I have managed to tune my <strong>Perl</strong> scripts recently at work into 1-3 line wonders.  I've been influenced a bit (perhaps in a geeky negative way) by some people over at ValueClick in the past in creating obfuscated, but killer <strong>Perl</strong> code.  The great thing is that I'm learning how to really utilize some of the more lesser known features that you probably won't see much in anything but the most hardcore type of modules out there.<br />
<br />
For instance, I started using the following paradigm:<br />
<br />
sub func {<br />
+{ map { $_-&gt;{something} =&gt; $_-&gt;{value} } @{$_[0]-&gt;someMethod($_[1], $_[2])} };<br />
}<br />
<br />
this is a cool little piece of code that i finally got to use the <strong>+{</strong>.  I found that little bit of code in the <strong>O'Reilly SOAP</strong> book, the author of <strong>SOAP::Lite.pm</strong> had a few statements like that.  I scratched my head for a while trying to figure out what it did.  Later, I found out that it's a type hint to the interpreter to return back a <strong>hash reference</strong>.<br />
<br />
I used it here because if I just did something like:<br />
<br />
{ map { ....} };<br />
<br />
I'd receive an <strong>array</strong>, which isn't what I wanted.  The <strong>+{</strong> sign will convert this into the appropriate <strong>hash reference</strong>.  I showed my boss to see if he knew what the heck it did, but it's a pretty obscure thing in <strong>Perl</strong> that I don't see often times used.  But I'm finding it immensely useful with statements like that.<br />
<br />
Of course, your next comment might be, well why the heck would you need a statement like that in the first place?  I'm working quite a bit with various types of data.  Sometimes, I have lists of data in the <strong>array</strong> format.  But I occasionally want to convert it over to a <strong>hash</strong> format.  Map partly helps here, but if I want to stay true to my virtue of one liners and avoid returning another array, then the <strong>+{</strong> becomes quite useful in conjunction with <strong>map</strong>.<br />
<br />
I really enjoy programming in <strong>Perl</strong> the most because it's like a puzzle where you're constantly trying to optimize what you're doing.  I've seen people use tons of variables or less obscure syntax, but I think that it's really not that great of a thing in the end.  I mean, in theory, you're replicating memory by creating more variables and the function call potentially grows, whereas a one liner, for a true veteran of <strong>Perl</strong>, can easily be deciphered without having to painfully scroll through unnecessary pieces of code.  I mean, if you want verbose, code <strong>Java</strong> or <strong>COBOL</strong>.  Then tell me how readable your code is and how long it took you to get a simple idea out the door.]]></description>
<pubDate>Wed, 23 Apr 2008 10:35:47 -0600</pubDate>
<guid>http://www.keithwatanabe.net/blogs/2008/4/23/13f7bab8a7369e6b5e5490db552a0a06.html</guid>
</item>
</channel>
</rss>
