<?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>Internet Explorer Bug Fix For Margins, Floats and Disappearing Backgrounds</title>
<link>http://www.keithwatanabe.net/blogs/2008/3/8/4c8ad283df73bbd43b8be5c133f766a9.html</link>
<description><![CDATA[One of my fears about Yahoo losing the shareholder battle to M$ is that MSIE will become even more dominant, despite the fact that it's a crappy browser and hard to program (well at least for a linux dude like myself :p).  But the main thing I want to share is a few tips on some of the horrible excursions I dealt with in MSIE:<br />
<br />
Float and the Double Margin Bug<br />
<br />
Apparently, MSIE suffers from a nasty little bug where if you float an element and provide a margin, MSIE will double the size of the margin either left or right.  To counter it, you need to add:<br />
<br />
display: inline (or block)<br />
<br />
for your class that you're employing the float.  There's a lot of resources on the web for why this occurs as well as more details on this.<br />
<br />
Next, there's another annoying bug that I encountered and spent hours knocking my head against my table (okay, not literally).  It's where backgrounds disappear if you have numerous div tags and use the background-color attribute.  The quick fix is to trick MSIE into redrawing the section, so you add a height attribute of say 1%.  The code would look like this:<br />
<br />
height: 1%;<br />
<br />
Put that in the class that has the background-color attribute and these should disappear.]]></description>
<pubDate>Sat, 08 Mar 2008 11:20:02 -0700</pubDate>
<guid>http://www.keithwatanabe.net/blogs/2008/3/8/4c8ad283df73bbd43b8be5c133f766a9.html</guid>
</item>
<item>
<title>Note to Adobe: OPEN SOURCE FLASH YOU IDIOTS!!!!</title>
<link>http://www.keithwatanabe.net/blogs/2008/6/9/fb8d533f235411bff82a1d2ae6530d25.html</link>
<description><![CDATA[Okay, I've been going through numerous forums and it seems that my <strong>Flash</strong> woes can be purely pinpointed on <strong>Adobe</strong>.  The biggest complaints I've been reading are that <strong>Flash</strong> is not open source so developers have no chance to go in and fix the issue for <strong>Linux</strong>.  <strong>PulseAudio</strong> people, <strong>Firefox</strong> people, <strong>Ubuntu</strong> people, etc. are all pointing the finger at one point.  Well, if this is true, then <strong>Adobe</strong> should at the very least open source the <strong>Linux</strong> version of their plugin to the community.  I'm going to be forced to remove the <strong>flash plugin</strong> from my browser temporarily in case some stupid site in flash takes down my whole system.<br />
<br />
(Updated)<br />
<br />
In the meantime, I read that the new <strong>Flash Beta 10 plugin</strong> solves these problems.  I've linked the plugin here so you can get it.  Also, I believe you need to add <strong>libflashsupport</strong> using something like <strong>apt-get</strong> or <strong>synaptic</strong> for compatibility since there might still be some issues with <strong>PulseAudio</strong> and <strong>Flash</strong>.<br />
<br />
Funny how <strong>Adobe's</strong> Beta software works far better.  This isn't something new for me either.  But again, if they just open source their stuff I think these frustrations might go away....]]></description>
<pubDate>Mon, 09 Jun 2008 17:37:38 -0600</pubDate>
<guid>http://www.keithwatanabe.net/blogs/2008/6/9/fb8d533f235411bff82a1d2ae6530d25.html</guid>
</item>
<item>
<title>Neat Perl Tricks</title>
<link>http://www.keithwatanabe.net/blogs/2008/6/30/0e94daf300bfbb59ec41bffad4459384.html</link>
<description><![CDATA[<h2>Using qw for an array ref:</h2>
<span class="Code"> [qw(item1 item2 item3 item4)];</span><br />
<br />
<br />
<h2> Extracting parts of a split list</h2>
<span class="Code">my ($item1, $item2, $item3) = (split(/s+/, $str))[2,4,5];</span><br />
<br />
<h2> Converting An Array To Key-Value Pairs For A Hash</h2>
<span class="Code"> my @list = qw(key1 val1 key2 val2 key3 val3 key4 val4);<br />
my %h = %{+{@list}};<br />
</span><br />
<h2> Calling a function inside of a quoted element</h2>
Very useful when you don't want to end a quote or heredoc.<br />
<br />
<span class="Code">my $var = &quot;hello @{[getName()]}n&quot;;</span><br />
<br />
<h2> Checking if the correct number of parameters are passed into a subroutine</h2>
<span class="Code">sub func {<br />
  die unless @_ == 4;<br />
}<br />
</span><br />
<h2> Using A Lookup Table To Quickly Validate If Arguments Exist</h2>
<br />
Good for checking input.<br />
<br />
<span class="Code">sub validate {<br />
  my %args = (arg1 =&gt; 1, arg2 =&gt; 1, arg3 =&gt; 1, arg4 =&gt; 1);<br />
  return exists $args{$_};<br />
}<br />
</span><br />
<h2> Converting a subroutine's arrayref argument to an array</h2>
<span class="Code">some_func(&quot;arg1&quot;, &quot;arg2&quot;, ['item1', ['item2'], ['item3'], ['item4']);<br />
<br />
sub some_func {<br />
  my $arg1 = shift;<br />
  my $arg2 = shift;<br />
  my @items = @{shift()};<br />
  print &quot;$_n&quot; for @items;<br />
}<br />
</span><br />
<h2> Dynamic Subroutines With Hashrefs</h2>
Nice technique when you want to create callbacks and require that the function calls are a little dynamic (for instance, when you don't know what function you might need to call).<br />
<br />
<span class="Code">my $h = { progname =&gt; 'some_func' };<br />
eval &quot;$h-&gt;{progname}('David')&quot;;<br />
<br />
sub some_func {<br />
  print &quot;Hello $_[0]n&quot;;<br />
}<br />
</span><br />
<h2>Getting Local Time in Epoch Seconds</h2>
<span class="Code">use Time::Local;<br />
my $sec = timelocal( (localtime)[0..5] );</span><br />
<h2> Efficient File Slurping</h2>
<span class="Code">open (FILE, $dir) || die $!;<br />
my $file = do { local $/; &lt;FILE&gt; };<br />
close (FILE);</span><br />
<br />
<h2> Getting All Files in A Directory</h2>
<span class="Code"> opendir( DIR, $dir ) || die $!;<br />
my @segdirs = grep { !/^./ } readdir( DIR );<br />
closedir( DIR );</span><br />
<br />
<h2> Better Inheritance in Perl</h2>
<span class="Code"> use base(Path::MyClass);</span><br />
<br />
<h2> Creating A Basic Constructor</h2>
<span class="Code"> sub new {<br />
  my ($class, %args) = @_;<br />
  my $self = {};<br />
  bless $self, ref $class || $class;<br />
}</span><br />
<h2>Creating A Singleton Instance</h2>
Using the constructor from above:<br />
<br />
<span class="Code">sub instance {<br />
  my $class = shift;<br />
  strict 'refs';<br />
  my $instance = ${&quot;$class::_instance&quot;};<br />
  defined $$instance ? $$instance : ( $$instance = $class-&gt;new( @_ ) );<br />
}<br />
</span>]]></description>
<pubDate>Mon, 30 Jun 2008 03:10:22 -0600</pubDate>
<guid>http://www.keithwatanabe.net/blogs/2008/6/30/0e94daf300bfbb59ec41bffad4459384.html</guid>
</item>
</channel>
</rss>
