<?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>Perlblogs &#187; perlprogramming</title>
	<atom:link href="http://perlblogs.com/category/perlprogramming/feed/" rel="self" type="application/rss+xml" />
	<link>http://perlblogs.com</link>
	<description>Posts from selected Perl bloggers</description>
	<lastBuildDate>Fri, 18 May 2012 19:03:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>Share the Modern Perl ePub</title>
		<link>http://www.modernperlbooks.com/mt/2011/02/share-the-modern-perl-epub.html</link>
		<comments>http://www.modernperlbooks.com/mt/2011/02/share-the-modern-perl-epub.html#comments</comments>
		<pubDate>Fri, 11 Feb 2011 21:02:47 +0000</pubDate>
		<dc:creator>chromatic</dc:creator>
				<category><![CDATA[books]]></category>
		<category><![CDATA[modernperl]]></category>
		<category><![CDATA[perl5]]></category>
		<category><![CDATA[perlprogramming]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[You asked, and we (eventually) delivered. Modern Perl, the book, is now available in ePub format as well as A4- and letter-sized PDFs. There is no DRM and there are no special licensing terms. Please redistribute this book far and...]]></description>
			<content:encoded><![CDATA[
        <p>You asked, and we (eventually) delivered.</p>

<p><a href="http://onyxneon.com/books/modern_perl/index.html#why_free">Modern Perl</a>, the book, is now available in ePub format as well as A4- and letter-sized PDFs.</p>

<p>There is no DRM and there are no special licensing terms. Please
redistribute this book far and wide and tell everyone that they too can write
great Perl 5 code that takes advantage of all of the hard-earned wisdom and
effort we've put into improving the language in the past several years.</p>

<p>Of course, you can always tell people to buy the printed copy&mdash;I'm a
fan of paper&mdash;but the important part is spreading the word, whether
through reviews on Perl Monger lists, web sites like Slashdot and Ars Technica
and LWN, and of course booksellers. You're also very welcome to upload and seed
torrents, post it on ebook sites, or just hand around USB sticks whenever you
get together.</p>

<p>Bilingual Perl hackers have already begun translations into other languages, and we're very happy to see this and help in any way possible.</p>

<p>If we're going to show off how great Perl programming can be, let's make
sure everyone has a chance to read a good book about it.</p>

        
    ]]></content:encoded>
			<wfw:commentRss>http://perlblogs.com/2011/02/11/share-the-modern-perl-epub/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Politely Suggesting Improvements</title>
		<link>http://www.modernperlbooks.com/mt/2011/02/politely-suggesting-improvements.html</link>
		<comments>http://www.modernperlbooks.com/mt/2011/02/politely-suggesting-improvements.html#comments</comments>
		<pubDate>Wed, 02 Feb 2011 18:22:00 +0000</pubDate>
		<dc:creator>chromatic</dc:creator>
				<category><![CDATA[modernperl]]></category>
		<category><![CDATA[perl5]]></category>
		<category><![CDATA[perlprogramming]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Thanks to IBM dW and Seda &#38;Ouml;zses for fixing the code to which I linked in CGI is Okay, but Bad Code is Irresponsible. To their credit, the security flaw and the Perl 4 style code in the article have...]]></description>
			<content:encoded><![CDATA[
        <p>Thanks to IBM dW and Seda &Ouml;zses for fixing the code to which I linked
in <a
href="http://www.modernperlbooks.com/mt/2011/01/cgi-is-okay-but-bad-code-is-irresponsible.html">CGI
is Okay, but Bad Code is Irresponsible</a>. To their credit, the security flaw
and the Perl 4 style code in the article have become much, much
better&mdash;now it's good example code, which is what everyone in this process
wants.</p>

<p>This sort of thing will happen again. Perhaps we can learn from this experience and handle it better next time.</p>

<p>What should you do the next time you see example code with a security flaw and/or clunky style?</p>

<ul>

<li>Contact the author directly and privately.</li>

<li>Explain the security flaw and demonstrate how to fix it and it alone.</li>

<li>Expand further on stylistic issues (explaining that, although there is not complete agreement in the entire Perl 5 community over some stylistic issues, the dominant trends are fairly widely understood as something we can all call modern or enlightened Perl), revising the code as necessary.</li>

<li>Suggest that the Perl 5 community has many resources such as <a href="http://perlmonks.org/">PerlMonks</a>, Perl Mongers, and mailing lists full of people who can and will perform technical review on code.</li>

<li>Be polite.</li>

<li>... if that does not work, publish an explanation and corrected code elsewhere.</li>

</ul>

<p>Where possible, stick with the constraints of the article. For example, Ms. &Ouml;zses's code has good reasons to use CGI instead of a Plack-capable module. Respect that. (Though where it's obvious that a CPAN module is far superior solution, such as in the case of CSV parsing, do recommend the module.)</p>

<p>We share a goal, that useful and usable and correct and secure and sufficiently elegant code is available. We do not have to write the same code nor the best possible code to achieve that goal, but...</p>

<p>... people who publish code examples have a <em>responsibility</em> to write good code.</p>

<p>... people who review and comment on code have a <em>responsibility</em> to
be both accurate and polite.</p>

<p>To that end, I owe Ms. &Ouml;zses an apology. However I intend neither
personal attack nor insult in my writings here, I failed to live up to my
personal standards of kindness and politeness in this case. She deserves much
credit for taking the suggestions of various Perl 5 programmers despite
unwarranted criticisms.</p>

        
    ]]></content:encoded>
			<wfw:commentRss>http://perlblogs.com/2011/02/02/politely-suggesting-improvements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What I Did Wrong (Test::MockObject)</title>
		<link>http://www.modernperlbooks.com/mt/2010/08/what-i-did-wrong-testmockobject.html</link>
		<comments>http://www.modernperlbooks.com/mt/2010/08/what-i-did-wrong-testmockobject.html#comments</comments>
		<pubDate>Tue, 17 Aug 2010 01:49:55 +0000</pubDate>
		<dc:creator>chromatic</dc:creator>
				<category><![CDATA[mockobjects]]></category>
		<category><![CDATA[modernperl]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[perl5]]></category>
		<category><![CDATA[perlprogramming]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I admit it, I avoid languages such as Java because they make it so difficult to write useful libraries such as Test::MockObject. When you're writing lots of test cases and flipping back and forth between code and test code and...]]></description>
			<content:encoded><![CDATA[
        <p>I admit it, I avoid languages such as Java because they make it so difficult to write useful libraries such as <a href="http://search.cpan.org/perldoc?Test::MockObject">Test::MockObject</a>.  When you're writing lots of test cases and flipping back and forth between code and test code and refactoring every time you make a test pass, the more ceremony and boilerplate you have to move around, the more friction you feel and the less work you can get done easily.</p>

<p>I wrote T::MO several years ago when I noticed a pattern in test code I'd written.  I wanted to isolate a couple of cases of behavior to test all of the code paths within specific functions and methods.  Invariably I took advantage of Perl's dynamism to construct objects which adhered to a specific protocol of behavior but which were under my direct control.</p>

<p>Think of it this way.  You want to test the error handling code for a database abstraction layer.  If the network connection goes away or if the remote process crashes, you want to retain the data and perform the appropriate logging and recovery.</p>

<p>How do you test that?  You force a failure.  No, you don't hire an intern to yank the network cable during your tests.  You mock up the code which raises the "Wow, it's suddenly quiet in here!" exception.</p>

<p>This is where I wrote T::MO wrong.  Instead of mocking <em>just one piece</em> of the underlying plumbing, T::MO encourages people to mock the entire system, from the faucets down to the sewer system.  You do get more control over the process, but you can lose yourself in a sea of sweaty little details.</p>

<p>T::MO <em>can be</em> an invaluable tool.  Sometimes it's exactly what you need, especially when you have to clean up a pile of legacy code written without good design taste.  Even so, I wish I'd written <a href="http://search.cpan.org/perldoc?Test::MockObject::Extends">Test::MockObject::Extends</a> first to encourage people to mock only <em>part</em> of the behavior of an object.  If your code is anywhere close to well-factored, you should be able to splice in the specific behavior you need, scalpel precise, and have confidence that the rest of the code, the real code you run when you run the code for real, behaves appropriately.</p>

<p>The more you need to mock, the more coupled (and, generally, the poorer) your design.  The less you do mock, the better your tests.</p>

        
    ]]></content:encoded>
			<wfw:commentRss>http://perlblogs.com/2010/08/17/what-i-did-wrong-testmockobject/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Improving Perl 5&#8242;s Core Exceptions</title>
		<link>http://www.modernperlbooks.com/mt/2010/08/improving-perl-5s-core-exceptions.html</link>
		<comments>http://www.modernperlbooks.com/mt/2010/08/improving-perl-5s-core-exceptions.html#comments</comments>
		<pubDate>Mon, 09 Aug 2010 20:33:18 +0000</pubDate>
		<dc:creator>chromatic</dc:creator>
				<category><![CDATA[internals]]></category>
		<category><![CDATA[languagedesign]]></category>
		<category><![CDATA[modernperl]]></category>
		<category><![CDATA[perl5]]></category>
		<category><![CDATA[perlprogramming]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[As I wrote in The Stringceptional Difficulty of Changing Error Messages, using strings in place of what should be structured data when reporting errors from Perl 5 makes improving Perl 5 more difficult than it has to be. This is...]]></description>
			<content:encoded><![CDATA[
        <p>As I wrote in <a href="http://www.modernperlbooks.com/mt/2010/08/the-stringceptional-difficulty-of-changing-error-messages.html">The Stringceptional Difficulty of Changing Error Messages</a>, using strings in place of what should be structured data when reporting errors from Perl 5 makes improving Perl 5 more difficult than it has to be.</p>

<p>This is fixable.</p>

<p>From the conceptual side, all someone has to do is to change <em>what</em> Perl 5 throws for its core exceptions and warnings from a string to an object.  That object can overload stringification so that all Perl code which treats it as a string will continue to get the string value.  All code which treats it as an object will continue to work correctly even if the string value changes.  (I haven't thought about how this might break XS code which pokes into SV guts with macros....)</p>

<p>Someone could even provide numeric overloading so that you can compare exception types numerically without having to call methods to figure out exception information.</p>

<p>Reconfiguring Perl 5's guts to make this possible is fairly simple, at least at the point of the API which actually throws the errors.  A few functions in <em>util.c</em> such as <code>Perl_croak()</code> need to build an object instead of a string, but that's a modest amount of code.  It's much more difficult to find every place in the Perl 5 core which <em>calls</em> <code>Perl_croak</code> and friends to change them to use the new API...</p>

<p>... because the right way to make this API work better is not to pass C strings as the text of error messages but instead to pass symbolic constants which represent error messages.  For example, instead of calling <code>Perl_croak( "Can't invoke non-invocant" );</code>, the calling code should instead use something like <code>Perl_croak( PERL5_NON_INVOCANT_EXCEPTION );</code>.  This allows a quick lookup of the right exception information as well as another benefit: localization of exception messages.</p>

<p>Even so, that's still a lot of code to change (590 uses of <code>Perl_croak*</code> in the <em>.c</em> files of bleadperl alone, not to mention everything on the CPAN)&mdash;and this code won't be available for wide use until 5.14 next spring at the earliest.  In a few years, maybe enough people will use exception objects by default that it's possible to clean up error messages throughout the core without worrying about breaking fragile old code.  Then again, fragile code tends to do the lazy thing, not the correct thing.</p>

<p>As an intermediary step, perhaps it's possible to refactor the core to use exception type concepts instead of literal (or <code>sprintf</code>-style) strings within the bleadperl source code.  That would allow for localization, if that's desirable, and it gets Perl 5.13.x closer to making real exception objects useful.</p>

<p>The real question is whether real exception objects in the core are sufficiently worthwhile to justify this change.  If the best rationale for this work is "Someday, we may be able to fix old, misleading, crufty, or wrong diagnostic messages!" then ... well, how soon will be too late?  This is why to plan for making&mdash;and correcting&mdash;mistakes in your language design.</p>
        
    ]]></content:encoded>
			<wfw:commentRss>http://perlblogs.com/2010/08/09/improving-perl-5s-core-exceptions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Stringceptional Difficulty of Changing Error Messages</title>
		<link>http://www.modernperlbooks.com/mt/2010/08/the-stringceptional-difficulty-of-changing-error-messages.html</link>
		<comments>http://www.modernperlbooks.com/mt/2010/08/the-stringceptional-difficulty-of-changing-error-messages.html#comments</comments>
		<pubDate>Fri, 06 Aug 2010 18:47:39 +0000</pubDate>
		<dc:creator>chromatic</dc:creator>
				<category><![CDATA[backwardscompatibility]]></category>
		<category><![CDATA[languagedesign]]></category>
		<category><![CDATA[modernperl]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[perl5]]></category>
		<category><![CDATA[perlprogramming]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Perl 5.10 modified a warning message in a wonderful and useful way. If you've used Perl 5 much at all, you've accidentally stringified an undefined value. If you enable warnings, you've seen the message Use of uninitialized value.... In Perl...]]></description>
			<content:encoded><![CDATA[
        <p>Perl 5.10 modified a warning message in a wonderful and useful way.  If
you've used Perl 5 much at all, you've accidentally stringified an undefined
value.  If you enable warnings, you've seen the message <code>Use of
uninitialized value...</code>.</p>

<p>In Perl 5.10, that message changed to include the <em>name</em> of the
uninitialized variable when it's available.  This was a huge usability
improvement and it's one of my favorite features of modern Perl.  Sometimes
changing a warning or error message for user clarity is the kindest improvement
ever.</p>

<p>(Having an interactive shell react to the user typing <code>exit</code> with the message <code>Type Ctrl-D to exit!</code> is a response to user confusion in the wrong direction.)</p>

<p>Unfortunately, changing error and warning messages in Perl 5 is a quagmire
because <a
href="http://www.modernperlbooks.com/mt/2010/07/dont-parse-that-string.html">parsing
unstructured data is a mess</a>.  Want to catch an exception in Perl 5?  You can do it safely with a module such as <a href="http://search.cpan.org/perldoc?Try::Tiny">Try::Tiny</a>, but to find out what the exception is, you have to perform string manipulations on <code>$@</code>.</p>

<p>If you're performing an exact match, a substring match, or a regular
expression, any change to the text of that message in a subsequent release of
Perl 5 could change the way your code behaves.  Thus p5p must be very, very,
very careful about even improving the text of internal exceptions and warnings,
lest they break the DarkPAN.</p>

<p>The only real solution in Perl 5 is never to attempt to parse the text of exceptions and to consider throwing exception objects with something like <a href="http://search.cpan.org/perldoc?Exception::Class">Exception::Class</a>.  Adding true exception object support to the Perl 5 core in a backwards-compatible fashion is also very possible, though that's a subject for another post.</p>

        
    ]]></content:encoded>
			<wfw:commentRss>http://perlblogs.com/2010/08/06/the-stringceptional-difficulty-of-changing-error-messages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Memory Optimization That Almost Wasn&#8217;t</title>
		<link>http://www.modernperlbooks.com/mt/2010/08/the-memory-optimization-that-almost-wasnt.html</link>
		<comments>http://www.modernperlbooks.com/mt/2010/08/the-memory-optimization-that-almost-wasnt.html#comments</comments>
		<pubDate>Wed, 04 Aug 2010 16:35:18 +0000</pubDate>
		<dc:creator>chromatic</dc:creator>
				<category><![CDATA[modernperl]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[perl5]]></category>
		<category><![CDATA[perlprogramming]]></category>
		<category><![CDATA[profiling]]></category>
		<category><![CDATA[warstories]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I made a silly mistake the other day. A client had asked me to review the memory usage of a suite of programs running on a shared host. His account had a low hard memory limit, and when multiple programs...]]></description>
			<content:encoded><![CDATA[
        <p>I made a silly mistake the other day.</p>

<p>A client had asked me to review the memory usage of a suite of programs
running on a shared host.  His account had a low hard memory limit, and when
multiple programs ran simultaneously, the Linux kernel's out of memory killer
killed one of the processes.</p>

<p>I ran the program on my machine and saw about half the memory usage.  "Ah," I thought to myself.  "I have a 32-bit Perl 5 installed and he has a 64-bit Perl 5 installed.  "There's lots of little data in the program, so the double sized pointers on his installation compared to mine account for the memory difference."</p>

<p>This is where I should have realized that profiling such an assumption would have taken only a few minutes and could have saved an afternoon.</p>

<p>Installing a 32-bit Perl 5 on a 64-bit machine takes some work.  You have to convince your C compiler to compile in 32-bit mode, and this must take place during the point at which you <em>configure</em> Perl 5.  After a few failed attempts and a couple of patches to <a href="http://search.cpan.org/perldoc?App::perlbrew">perlbrew</a>, I had a new custom installation of Perl 5.</p>

<p>I installed the dependent modules and ran the application... and the memory use was only slightly better.</p>

<p>Then I thought for a bit.  Then I instrumented some code for a quick profile
and realized the problem.  The application fetches a list of tasks out of a
data store it uses as a queue.  This queue has an iterator interface, as the
list of tasks can be arbitrarily large.  Within the code which performs the
fetch, I'd assumed that the dataset would be reasonably short at any given
time, so I'd flattened the iterator into an anonymous array and kept the whole
thing in memory.</p>

<p>The program now processed more data than I'd ever planned, and the problem had only grown worse as more and more tasks piled up, as the consumer couldn't keep up with the producer thanks to OOM killing.</p>

<p>Five minutes later, I'd switched back to the iterator interface, and the process used a fraction of the memory than it did before.  If I'd asked myself "What within the program <em>grows</em> over time?", I'd have figured out my mistake before I recompiled Perl 5.</p>

<p>So it goes sometimes.</p>

        
    ]]></content:encoded>
			<wfw:commentRss>http://perlblogs.com/2010/08/04/the-memory-optimization-that-almost-wasnt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How About a Shetland Ponie?</title>
		<link>http://www.modernperlbooks.com/mt/2010/07/how-about-a-shetland-ponie.html</link>
		<comments>http://www.modernperlbooks.com/mt/2010/07/how-about-a-shetland-ponie.html#comments</comments>
		<pubDate>Thu, 29 Jul 2010 20:47:15 +0000</pubDate>
		<dc:creator>chromatic</dc:creator>
				<category><![CDATA[Parrot]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[perl5]]></category>
		<category><![CDATA[perl6]]></category>
		<category><![CDATA[perlprogramming]]></category>
		<category><![CDATA[ponie]]></category>
		<category><![CDATA[Rakudo]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Rakudo Star is out, and so begins the next great wave of interest and use of Perl 6. The next several releases will improve performance, fix bugs, add features, port or create more libraries, and&#38;mdash;in all likelihood&#38;mdash;improve and otherwise clarify...]]></description>
			<content:encoded><![CDATA[
        <p><a href="http://rakudo.org/node/75">Rakudo Star is out</a>, and so begins
the next great wave of interest and use of Perl 6.  The next several releases
will improve performance, fix bugs, add features, port or create more
libraries, and&mdash;in all likelihood&mdash;improve and otherwise clarify the
Perl 6 specification.</p>

<p>The Perl ecosystem has room for other projects, however.</p>

<p>For example, one of the clearest benefits Perl 6 has over Perl 5 is its portability to other virtual machines and runtimes.  By design Perl 6 <em>encourages</em> multiple implementations.  Perl 5 is its own specification; in many places, what Perl 5 is is solely what Perl 5 happens to do.  Sometimes that behavior gets enshrined in the specification tests, but other times it's folklore and institutional community knowledge.</p>

<p>Just as <a href="http://trac.parrot.org/parrot/wiki/Lorito">Parrot's Lorito project</a> intends to make Parrot at least an order of magnitude faster, so too a reorganization of Perl 5 internals could make amazing things more possible.</p>

<p>What if there were a project to <a
href="http://www.nntp.perl.org/group/perl.perl5.porters/2010/07/msg162367.html">implement
a minimal set of Perl 5 on the Parrot virtual machine</a> as a prototype and
exploration of how much of Perl 5 you can support, the effort it takes to do
so, and what kind of utility you can expect?  Parrot's compiler tools let the
Rakudo developers write most of Perl 6 in Perl 6; surely it's possible to write
Perl 5 in a similar fashion.  (Credit to other projects such as <a
href="http://rubini.us/">Rubinius</a> and <a
href="http://codespeak.net/pypy/">PyPy</a> for demonstrating that such things
are possible.)</p>

<p>I know other projects have attempted this in the past.  Perhaps the best place to steal information is Bradley Kuhn's masters thesis, <a href="http://www.ebb.org/bkuhn/articles/thesis/">Considerations on Porting Perl
to the Java Virtual Machine</a>.</p>

<p>As Jesse wrote in his comments, bug-for-bug compatibility isn't necessary.
Nor is full compliance with the existing Perl 5 test suite.  A simple proof of
concept to produce the 80% of Perl 5 most people use in most programs should
suffice.  (Parrot gives you a lot of that anyway.)</p>

<p>As a bonus, you get cheap and easy interoperability with Perl 5, access to
Parrot features such as multidispatch, grammars, continuations, and bytecode
serialization, and you could even replace some of the uses of Perl 5 within
Parrot's and perhaps even Rakudo's configuration and build processes.</p>

<p>It doesn't even have to be a pony of full size.</p>
        
    ]]></content:encoded>
			<wfw:commentRss>http://perlblogs.com/2010/07/29/how-about-a-shetland-ponie/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Strings and Security and Designing Away Bugs</title>
		<link>http://www.modernperlbooks.com/mt/2010/07/strings-and-security-and-designing-away-bugs.html</link>
		<comments>http://www.modernperlbooks.com/mt/2010/07/strings-and-security-and-designing-away-bugs.html#comments</comments>
		<pubDate>Wed, 14 Jul 2010 16:23:45 +0000</pubDate>
		<dc:creator>chromatic</dc:creator>
				<category><![CDATA[apis]]></category>
		<category><![CDATA[languagedesign]]></category>
		<category><![CDATA[modernperl]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[perlprogramming]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Some people believe that security problems and other severe bugs are inevitable. Some of these people believe that conscientious design and clear thinking about how languages and APIs work is irrelevant; bad code is possible in every language. Bad code...]]></description>
			<content:encoded><![CDATA[
        <p>Some people believe that security problems and other severe bugs are inevitable.  Some of these people believe that conscientious design and clear thinking about how languages and APIs work is irrelevant; bad code is possible in every language.</p>

<p>Bad code <em>is</em> possible in any language and wrong code is possible with any API.  Even so, it's possible to create languages and APIs which make the right thing so much easier than the wrong thing that only the most incompetent (or dangerously malicious) write bad code.</p>

<p>Imagine, for example, a database access layer which forbids the use of raw strings to create SQL queries.  You might have to write:</p>

<pre><code>my $sth = $dbh->select( @tables )->join( %relations )->where( %conditions );</code></pre>

<p>That's not necessarily a <em>beautiful</em> interface dashed off after a moment of thinking, but it has an important security property: it avoids the interpolation of untrusted user input.  All data sent to the database may go through a quoting or untainting process without the user having to remember to do so.</p>

<p>A similar library could help avoid malicious user input from interfering
with the display or operation of a web site, for example.  These are both
specific cases of a general principle: <a
href="http://www.modernperlbooks.com/mt/2010/07/dont-parse-that-string.html">replace
unstructured string data with structured data</a>.  In both cases, the
structure of the data makes the intent of the data clear, which allows the
library to ensure as much safety as possible.</p>

<p>This principle has other implications as well; more on that next time.</p>

        
    ]]></content:encoded>
			<wfw:commentRss>http://perlblogs.com/2010/07/14/strings-and-security-and-designing-away-bugs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When Sugar and Semantics Collide</title>
		<link>http://www.modernperlbooks.com/mt/2010/07/when-sugar-and-semantics-collide.html</link>
		<comments>http://www.modernperlbooks.com/mt/2010/07/when-sugar-and-semantics-collide.html#comments</comments>
		<pubDate>Thu, 01 Jul 2010 18:10:12 +0000</pubDate>
		<dc:creator>chromatic</dc:creator>
				<category><![CDATA[languagedesign]]></category>
		<category><![CDATA[learningperl]]></category>
		<category><![CDATA[modernperl]]></category>
		<category><![CDATA[moose]]></category>
		<category><![CDATA[perl5]]></category>
		<category><![CDATA[perlprogramming]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I use Moose to explain object orientation in Perl in the Modern Perl book. It's much easier to explain the what and why of OO with syntax like: { package Cat; use Moose; has 'name', is =&#38;gt; 'ro', isa =&#38;gt;...]]></description>
			<content:encoded><![CDATA[
        <p>I use <a href="http://moose.perl.org/">Moose</a> to explain object orientation in Perl in the <a href="http://www.modernperlbooks.com/mt/2010/06/modern-perl-the-book-the-draft.html">Modern Perl book</a>.  It's much easier to explain the what and why of OO with syntax like:</p>

<pre><code>{
    package Cat;

    use Moose;

    has 'name', is =&gt; 'ro', isa =&gt; 'Str';
    has 'age',  is =&gt; 'ro', isa =&gt; 'Int';
    has 'diet', is =&gt; 'rw';
}</code></pre>

<p>... than the corresponding code where you must write your own accessors, poke into a blessed hash directly (and <code>bless</code> it yourself), perform your own coercions and verifications, and the like.</p>

<p>Of course, the preferred syntax for doing this within the Moose documentation is different from how I've done things.  Moose recommends:</p>

<pre><code>{
    package Cat;

    use Moose;

    has 'name' =&gt; <strong>(</strong>is =&gt; 'ro', isa =&gt; 'Str'<strong>)</strong>;
    has 'age' =&gt;  <strong>(</strong>is =&gt; 'ro', isa =&gt; 'Int'<strong>)</strong>;
    has 'diet' =&gt; <strong>(</strong>is =&gt; 'rw'<strong>)</strong>;
}</code></pre>

<p>Sometimes you quote the name of the attribute and sometimes you don't.</p>

<p>Should I drop the parentheses?  Should I drop the fat arrow between the name of the attribute and its specializers?  I do in my own Moose code for my preferences, and I did in the book.  Then I thought about it and realized <em>why</em> I write code this way.</p>

<p>First, a digression.  <a href="http://www.modernperlbooks.com/mt/2010/06/the-virtuous-dilemma-of-iterative-improvements.html#comment-439">Perrin Harkins mentioned the inability of the "Takes a block!" prototype to replicate builtin syntaxes</a> as a reason to dislike syntax-bending modules such as <a href="http://search.cpan.org/perldoc?Error">Error</a>.  For example:</p>

<pre><code>
use Error ':try';

try
{
    ...
}
catch
{
    ...
}<strong>;</strong></code></pre>

<p>... <em>really</em> needs that trailing semicolon.  For similar reasons,
many modules which use <a
href="http://search.cpan.org/perldoc?Devel::Declare">Devel::Declare</a> magic
go through contortions to add trailing commas and semicolons.  Perl 5's syntax
is malleable, but when the parser wants something from the lexer, it really
really wants something from the lexer.  (When it wants to know that a statement
or a group of terms has ended, you don't get to lie.)</p>

<p>In other words, even though you have a lot of options for mangling Perl 5's
syntax any way you like it, the semantics of the host language will shine
through.  A parenthesis is a parenthesis.  A labeled block is a labeled block.
A bare <code>sub { ... }</code> is never an expression on its own, and it can
never terminate an outer expression.</p>

<p>This is one of the downfalls of the so-called "embedded domain specific
languages".  If you haven't written your own parser, you'll have to take what
you can get.  This is even true if you <em>do</em> write a parser and generate
and <code>eval</code> code, and it's especially true if your EDSL desugars to
chained function or method calls.</p>

<p>I'm not suggesting a flaw with Moose's approach: it's clever and Perlish and
doesn't succumb to the saccharine cutery of so many other so-called DSLs.  (To
my knowledge, no one in the Moose world has claimed it's anything other than
Perl 5 syntax bent slightly into something which looks declarative enough.)</p>

<p>My concern&mdash;especially when explaining object orientation in Perl 5 to
novices&mdash;is that any extra syntactic elements might confuse people to
think that they mean more than they mean.  You and I might both understand that
the grouping parentheses in the <code>Cat</code> attribute declaration are
merely visual hints to the reader that the specializers are subordinate to the
attribute itself and that the fat arrow between the name of the attribute and
its grouped specializers confers the notion of pairing between the attribute
and its specializers, but how do you explain that to someone who's still
struggling to figure out what this encapsulation thing is all about?</p>

<p>I've attempted caution throughout the book such that the fat comma always signifies a pairish relationship, such as for hash keys or named arguments.  Certainly you can always use it in place of the skinny comma (and, barring any quoting changes, vice versa), but is it <em>clear</em> to do so?</p>

<p>Likewise, you can wrap parentheses around almost any old rvalue (barring precedence changes) and not change the behavior of lists, yet this confuses novices all the time:</p>

<pre><code>my @lololol = ( 1, 2, ( 3, 4, (5, 6) ) );</code></pre>

<p>I'm not criticizing the Moose documentation or the standard approaches to formatting Moose code.  I'm not suggesting a change.  I don't like deviating from community standards for declaring Moose attributes.  Even so, avoiding the need to explain the equivalencies of syntax to people for whom learning syntax is still a really big deal is itself to me a big deal.</p>
        
    ]]></content:encoded>
			<wfw:commentRss>http://perlblogs.com/2010/07/01/when-sugar-and-semantics-collide/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sometimes a Little Too Dynamic</title>
		<link>http://www.modernperlbooks.com/mt/2010/06/sometimes-a-little-too-dynamic.html</link>
		<comments>http://www.modernperlbooks.com/mt/2010/06/sometimes-a-little-too-dynamic.html#comments</comments>
		<pubDate>Mon, 14 Jun 2010 23:15:47 +0000</pubDate>
		<dc:creator>chromatic</dc:creator>
				<category><![CDATA[languagedesign]]></category>
		<category><![CDATA[modernperl]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[perl5]]></category>
		<category><![CDATA[perlprogramming]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I've written before about difficulties in parsing introduced by indirect notation in Perl 5 as well as why barewords in Perl 5 making parsing difficult. Nicholas Clark reminded me over the weekend of something I heard long ago. In particular:...]]></description>
			<content:encoded><![CDATA[
        <p>I've written before about <a href="http://www.modernperlbooks.com/mt/2009/08/the-problems-with-indirect-object-notation.html">difficulties in parsing introduced by indirect notation in Perl 5</a> as well as <a href="http://www.modernperlbooks.com/mt/2010/05/how-to-parse-perl-5-on-the-jvm.html">why barewords in Perl 5 making parsing difficult</a>.</p>

<p>Nicholas Clark reminded me over the weekend of something I heard long ago.  In particular:</p>

<blockquote>... as well as action at a distance, there's a speed hit on every
class method call because first the code does a stash lookup to see if the
package name string is actually a filehandle ... </blockquote>

<p>&mdash; Nicholas Clark, <a href="http://www.nntp.perl.org/group/perl.perl5.porters/2010/06/msg160904.html">methods and bareword file handles, action at a distance, (un)speed</a></p>

<p>That is, when invoking a method on a bareword (such as the name of a class),
Perl 5 has to check whether a filehandle of that name exists, then fetch the
underlying filehandle object, then invoke the method on that object.  This
takes place even if you never use bareword filehandles in your program.  It's likely too risky to change now (at least in Perl 5) lest millions of programs written since 1994 suddenly and mysteriously break with odd error messages.</p>

<p>(The correct design solution is to forbid the use of ambiguous barewords at the syntax level in order to avoid costly and often unnecessary runtime checks.  In other words, similar things should look similar and different things&mdash;filehandles and class names&mdash;should look different.)</p>
        
    ]]></content:encoded>
			<wfw:commentRss>http://perlblogs.com/2010/06/15/sometimes-a-little-too-dynamic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

