<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Beutelevision's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 14:06:56 GMT</pubDate>
    <description>DZone Snippets: Beutelevision's Code Snippets</description>
    <item>
      <title>Create an RFC822 compliant date in Perl</title>
      <link>http://snippets.dzone.com/posts/show/466</link>
      <description>&lt;code&gt;&lt;br /&gt;use Date::Manip qw(ParseDate UnixDate);&lt;br /&gt;&lt;br /&gt;# Create an RFC822 compliant date (current time)&lt;br /&gt;my $rfc822_format = "%a, %d %b %Y %H:%M %Z";&lt;br /&gt;my $today         = ParseDate("Now");&lt;br /&gt;&lt;br /&gt;my $rfc822_date   = UnixDate($today,$rfc822_format);&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 08 Jul 2005 04:41:10 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/466</guid>
      <author>beutelevision (Thomas Beutel)</author>
    </item>
    <item>
      <title>Cheap XML append in Perl</title>
      <link>http://snippets.dzone.com/posts/show/418</link>
      <description>Here is a really cheap way in Perl to append an XML fragment to an existing XML doc. It appends it just before the last closing tag. Note that there is no validation or well-formedness checking. Use at your own risk. That said, it can be handy if you know that your XML is going to be OK.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;#!/usr/bin/perl&lt;br /&gt;&lt;br /&gt;sub xappend(\$$) { ${$_[0]} =~ s/(&lt;\/[^&gt;]+&gt;\s*)$/$_[1]$1/s }&lt;br /&gt;&lt;br /&gt;$a = "&lt;doc&gt;&lt;item&gt;foo&lt;/item&gt;&lt;/doc&gt;";&lt;br /&gt;xappend($a,"&lt;item&gt;bar&lt;/item&gt;");&lt;br /&gt;&lt;br /&gt;print $a; # prints &lt;doc&gt;&lt;item&gt;foo&lt;/item&gt;&lt;item&gt;bar&lt;/item&gt;&lt;/doc&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 01 Jul 2005 01:19:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/418</guid>
      <author>beutelevision (Thomas Beutel)</author>
    </item>
    <item>
      <title>Easy Substitution Oneliner</title>
      <link>http://snippets.dzone.com/posts/show/258</link>
      <description>Suppose you have a bunch of files where you need to make the same substitution. This oneliner will make the substitution and keep the old version in backups. This is particularly handy for configuration files.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;perl -p -i.bak -e 's:oldtext:newtext:g' *.conf&lt;br /&gt;&lt;/code&gt;&lt;br /&gt; </description>
      <pubDate>Fri, 06 May 2005 23:45:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/258</guid>
      <author>beutelevision (Thomas Beutel)</author>
    </item>
    <item>
      <title>From database records to XML</title>
      <link>http://snippets.dzone.com/posts/show/182</link>
      <description>Here is a quick way to convert database records to XML using XML::Simple.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/perl&lt;br /&gt;&lt;br /&gt;use warnings;&lt;br /&gt;use strict;&lt;br /&gt;use XML::Simple;&lt;br /&gt;use DBI;&lt;br /&gt;&lt;br /&gt;my $dbh = DBI-&gt;connect('DBI:mysql:MYDATABASE','user','password')&lt;br /&gt;  or die DBI-&gt;errstr;&lt;br /&gt;&lt;br /&gt;# Get an array of hashes&lt;br /&gt;my $recs = $dbh-&gt;selectall_arrayref('SELECT * FROM contents',{ Columns =&gt; {} });&lt;br /&gt;&lt;br /&gt;# Convert to XML where each hash element becomes an XML element&lt;br /&gt;my $xml = XMLout( {record =&gt; $recs}, NoAttr =&gt; 1 );&lt;br /&gt;&lt;br /&gt;print $xml;&lt;br /&gt;&lt;br /&gt;$dbh-&gt;disconnect;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;For a table with fields named "id", "entry", and "modified", the output would be:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;opt&gt;&lt;br /&gt;  &lt;record&gt;&lt;br /&gt;    &lt;id&gt;1&lt;/id&gt;&lt;br /&gt;    &lt;entry&gt;1932 - 1939&lt;/entry&gt;&lt;br /&gt;    &lt;modified&gt;2005-04-15 22:24:44&lt;/modified&gt;&lt;br /&gt;  &lt;/record&gt;&lt;br /&gt;  &lt;record&gt;&lt;br /&gt;    &lt;id&gt;2&lt;/id&gt;&lt;br /&gt;    &lt;entry&gt;Yet another entry&lt;/entry&gt;&lt;br /&gt;    &lt;modified&gt;2005-04-15 22:25:00&lt;/modified&gt;&lt;br /&gt;  &lt;/record&gt;&lt;br /&gt;  &lt;record&gt;&lt;br /&gt;    &lt;id&gt;3&lt;/id&gt;&lt;br /&gt;    &lt;entry&gt;More stuff here&lt;/entry&gt;&lt;br /&gt;    &lt;modified&gt;2005-04-15 22:25:13&lt;/modified&gt;&lt;br /&gt;  &lt;/record&gt;&lt;br /&gt;&lt;/opt&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Sat, 16 Apr 2005 14:57:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/182</guid>
      <author>beutelevision (Thomas Beutel)</author>
    </item>
    <item>
      <title>Post XML using curl</title>
      <link>http://snippets.dzone.com/posts/show/181</link>
      <description>You can use curl on the command line to do a POST to an endpoint. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;echo '&lt;doc&gt;&lt;item&gt;Some content.&lt;/item&gt;&lt;/doc&gt;' | curl -X POST -H 'Content-type: text/xml' -d @- http://example.com/restapi&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This is handy for adding web services to applications that do not do web services but can do command lines--FileMaker for example.</description>
      <pubDate>Sat, 16 Apr 2005 09:42:57 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/181</guid>
      <author>beutelevision (Thomas Beutel)</author>
    </item>
    <item>
      <title>Convert HTML entities</title>
      <link>http://snippets.dzone.com/posts/show/171</link>
      <description>Include this to convert those ampersands and angle brackets to proper entities. It can help with XML too but remember there are lots of other entities that need converting. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;use HTML::Entities;&lt;br /&gt;&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;$html = encode_entities($string, '&lt;&gt;&amp;"');&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 15 Apr 2005 00:05:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/171</guid>
      <author>beutelevision (Thomas Beutel)</author>
    </item>
    <item>
      <title>XSLT Alternating Colors for HTML Table Rows</title>
      <link>http://snippets.dzone.com/posts/show/141</link>
      <description>This attaches a style attribute to the tr tag, but only on every other tag.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;xsl:template match="my_repeating_item"&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;  &lt;xsl:if test="position() mod 2 != 1"&gt;&lt;br /&gt;    &lt;xsl:attribute  name="style"&gt;background-color:#dddddd&lt;/xsl:attribute&gt;&lt;br /&gt;  &lt;/xsl:if&gt;&lt;br /&gt;  &lt;xsl:apply-templates/&gt;&lt;br /&gt;&lt;/tr&gt;&lt;br /&gt;&lt;xsl:template&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 12 Apr 2005 01:18:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/141</guid>
      <author>beutelevision (Thomas Beutel)</author>
    </item>
    <item>
      <title>Oneliner - Print a File as Hex</title>
      <link>http://snippets.dzone.com/posts/show/100</link>
      <description>&lt;code&gt;&lt;br /&gt;perl -ne '@a=split"";for(@a){printf"%02x ",ord;print"\n"unless++$c % 20}' sample.txt&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 09 Apr 2005 06:09:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/100</guid>
      <author>beutelevision (Thomas Beutel)</author>
    </item>
    <item>
      <title>Expand Numerical Ranges</title>
      <link>http://snippets.dzone.com/posts/show/99</link>
      <description>This rather long winded Perl subroutine returns an array of numbers expanded from a list such as 34,35,36,42-56,98-150. There are plenty of other ways to do this (eval comes to mind).&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt; sub expand {                                                              &lt;br /&gt;  my $range = shift;            &lt;br /&gt;  my @result; &lt;br /&gt;$range =~ s/[^\d\-\,]//gs; #remove extraneous characters&lt;br /&gt; my @items = split(/,/,$range);    &lt;br /&gt;  foreach (@items){                 &lt;br /&gt;    m/^\d+$/ and push(@result,$_) and next;  my ($start,$finish) = split /-/;   push(@result,($start .. $finish)) if $start &lt; $finish;                    &lt;br /&gt;  }                                 &lt;br /&gt;  return @result;                        &lt;br /&gt; } &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 09 Apr 2005 05:57:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/99</guid>
      <author>beutelevision (Thomas Beutel)</author>
    </item>
    <item>
      <title>Read a File with any Line Ending</title>
      <link>http://snippets.dzone.com/posts/show/98</link>
      <description>Here is a quick way in Perl to read lines from a file regardless of whether it is Unix, Mac, or DOS.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;my @lines = split(/\012\015?|\015\012?/,(join '',&lt;FILE&gt;));&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 09 Apr 2005 05:49:20 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/98</guid>
      <author>beutelevision (Thomas Beutel)</author>
    </item>
  </channel>
</rss>
