<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Otter Book &#187; perl</title>
	<atom:link href="http://www.otterbook.com/blog/category/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.otterbook.com</link>
	<description>Automating System Administration with Perl</description>
	<lastBuildDate>Mon, 19 Jul 2010 03:46:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>PMotD: Text::Balanced</title>
		<link>http://www.otterbook.com/blog/2009/07/pmotd-textbalanced/</link>
		<comments>http://www.otterbook.com/blog/2009/07/pmotd-textbalanced/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 05:26:59 +0000</pubDate>
		<dc:creator>dnb</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[modules]]></category>

		<guid isPermaLink="false">http://www.otterbook.com/?p=285</guid>
		<description><![CDATA[If you&#8217;ve read Jeffrey Friedl&#8217;s Mastering Regular Expressions1 you&#8217;ll recall that one of the things that is a lot harder than it looks to get right using regexps is extracting the content from within some text-delimited string, especially when there are opening and closing delimiters2. Text::Balanced originally by Damian Conway and now maintained by Adam [...]]]></description>
			<content:encoded><![CDATA[<p></p><p style="clear: both">If you&#8217;ve read Jeffrey Friedl&#8217;s <a href="http://oreilly.com/catalog/9780596528126/">Mastering Regular Expressions</a><sup class='footnote'><a href='#fn-285-1' id='fnref-285-1'>1</a></sup> you&#8217;ll recall that one of the things that is a lot harder than it looks to get right using regexps is extracting the content from within some text-delimited string, especially when there are opening and closing delimiters<sup class='footnote'><a href='#fn-285-2' id='fnref-285-2'>2</a></sup>.</p>
<p style="clear: both"><a href="http://search.cpan.org/dist/Text-Balanced/" target="_blank">Text::Balanced</a> originally by Damian Conway and now maintained by <a href="http://search.cpan.org/~adamk/" target="_blank">Adam Kennedy</a> makes short work of this extraction for:</p>
<ul style="clear: both">
<li>strings delimited by the same single character</li>
<li>strings delimited by brackets of some sort (parens, square brackets, etc.)</li>
<li>strings delimited by XML or other kinds of tags</li>
<li>strings delimited by Perl quoting operators</li>
<li>code blocks</li>
</ul>
<p>Each kind of delimited string has its own function, e.g. <span class="function">extract_bracketed()</span>. For the most part, they all work like this:</p>
<pre class="brush: perl;">my ($extracted,$remainder) =
        extract_something($extract_from, $delimiter, $opt_prefix);</pre>
<p>where you have a function called <span class="function">extract_<em>something</em>()</span> that takes the string to extract the data from, the delimiter that surrounds the data in question, and an optional prefix. That last argument reveals a part of <span class="modulename">Text::Balanced</span> that tends to confuse people so we&#8217;ll look more at it in a second. The <span class="function">extract_<em>something()</em></span> functions return (in a list context) the first extracted piece of data and what was left over or just the extracted data in a scalar context (with alas, another twist).</p>
<p>There are two things about this module that tend to trip up newcomers:</p>
<ol>
<li>By default (i.e. without the optional third argument), the extraction functions all expect the data they are going to extract to be found either right at the beginning of the string or right at the position in the string that the last extraction left off. If you expect it to skip over non-delimited data that isn&#8217;t just whitespace, you will have to provide that third <span class="variable">$opt_prefix</span> argument.</li>
<li>When called in a scalar context, the extraction functions eat the text. Extracted strings are removed from the input text. Fans of functional or immutable data structure programming will not be pleased.</li>
</ol>
<p>If you do want to do <span class="function">split()</span> on steroids kind of stuff, <span class="modulename">Text::Balanced</span> also offers a <span class="function">extract_multiple()</span> function that takes a list of extraction functions, each of which gets run over the string, returning what they collectively find. <span class="modulename">Text::Balanced</span> can also make Friedl proud by generating optimal regular expressions for balanced matches that you can use with a minimum of head scratching.</p>
<p>If you need something to extract data matched delimiters of almost all sorts, this module will be spot on for you and will do its job like a laser beam.
<div class='footnotes'>
<div class='footnotedivider'></div>
<ol>
<li id='fn-285-1'>and if you haven&#8217;t&#8211;you really should. It is a bit light on plot and character development, but it is definitely required reading <span class='footnotereverse'><a href='#fnref-285-1'>&#8617;</a></span></li>
<li id='fn-285-2'>the other being validating email addresses, but that&#8217;s a post for another day <span class='footnotereverse'><a href='#fnref-285-2'>&#8617;</a></span></li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.otterbook.com/blog/2009/07/pmotd-textbalanced/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PMotD: Devel::Deprecate</title>
		<link>http://www.otterbook.com/blog/2009/07/perl-module-of-the-day-develdeprecate/</link>
		<comments>http://www.otterbook.com/blog/2009/07/perl-module-of-the-day-develdeprecate/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 03:51:29 +0000</pubDate>
		<dc:creator>dnb</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[hacks]]></category>
		<category><![CDATA[modules]]></category>

		<guid isPermaLink="false">http://www.otterbook.com/?p=246</guid>
		<description><![CDATA[Today&#8217;s interesting Perl module of the Day:  Devel::Deprecate by Curtis &#8220;Ovid&#8221; Poe. Back in 2000, I started teaching a class called &#8220;Perl Saves the Day: Writing Small Perl Programs to Get Out of Big SysAdmin Pinches&#8221; which was essentially a class about Perl hacks and how to use them responsibly in System Administration. One of my [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Today&#8217;s interesting Perl module of the Day:  <a href="http://search.cpan.org/~ovid/Devel-Deprecate/">Devel::Deprecate</a> by Curtis &#8220;Ovid&#8221; Poe.</p>
<p>Back in 2000, I started teaching a <a href="http://www.otterbook.com/the-classes/">class</a> called &#8220;Perl Saves the Day: Writing Small Perl Programs to Get Out of Big SysAdmin Pinches&#8221; which was essentially a class about Perl hacks and how to use them responsibly in System Administration. One of my slides was &#8220;How to Get Rid of Hacks,&#8221; which started with:</p>
<blockquote><p>It can be hard. You may have to wait for the rewrite.<br />
Step one: remember you did it.</p>
<ul>
<li>Go back and document the code after the crisis.</li>
<li>Send yourself mail.</li>
<li>Set up an AT job.</li>
<li>Put it in your calendar.</li>
</ul>
</blockquote>
<p>Now there&#8217;s an even cooler way: <span class="modulename">Devel::Deprecate</span>. <span class="modulename">Devel::Deprecate</span> provides a <span class="function">deprecate()</span> function that let&#8217;s you write code like this (to quote the doc):</p>
<pre class="brush: perl;">deprecate (
    reason =&gt; 'Please use the set_name() method for setting names',
    warn   =&gt; '2008-11-01',    # also accepts DateTime objects
    die    =&gt; '2009-01-01',    # two month deprecation period
);</pre>
<p><span class="function">deprecate()</span> only comes into play when the code is run during a test (which you are writing, right?). Each time it is run under this condition (and only this condition, it never comes into play when the code is run outside of a test), it produces output that is crystal clear:</p>
<pre># DEPRECATION WARNING
#
# Package:     Our::Customer
# File:        lib/Our/Customer.pm
# Line:        58
# Subroutine:  Our::Customer::name
#
# Reason:      Please use the set_name() method for setting names
#
# This warning becomes FATAL on (2009-01-01)</pre>
<p>After the due date, it blows up just as promised with an equally verbose message. Very cool.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.otterbook.com/blog/2009/07/perl-module-of-the-day-develdeprecate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)
Database Caching 10/18 queries in 0.028 seconds using disk

Served from: www.otterbook.com @ 2010-07-31 00:09:24 -->