<?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>wildpointer* &#187; perl</title>
	<atom:link href="http://wildpointer.org/category/tech/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://wildpointer.org</link>
	<description>referencing whatever comes to mind, by chet nichols</description>
	<lastBuildDate>Sun, 19 Jun 2011 07:27:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>perl typecast beware! weak typing and the range operator.</title>
		<link>http://wildpointer.org/2010/11/23/perl-typecast-beware-weak-typing-and-the-range-operator/</link>
		<comments>http://wildpointer.org/2010/11/23/perl-typecast-beware-weak-typing-and-the-range-operator/#comments</comments>
		<pubDate>Wed, 24 Nov 2010 07:46:01 +0000</pubDate>
		<dc:creator>chet</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://chetnichols.org/?p=156</guid>
		<description><![CDATA[Background: I&#8217;ve been working on a suite of tools for the past few months to help with some config automation, and one of the config files allows you to put in a range (ie: 1-100, or even 0200-0399). The tool will take the range, look for running jobs with an ID in that range, then [...]]]></description>
			<content:encoded><![CDATA[<p>Background: I&#8217;ve been working on a suite of tools for the past few months to help with some config automation, and one of the config files allows you to put in a range (ie: 1-100, or even 0200-0399). The tool will take the range, look for running jobs with an ID in that range, then do something with it. In some cases, we do have jobs whose ID starts with a zero (or multiple zeroes), so the leading zero can be important. <strong>This is where I ran into some interesting discoveries!</strong></p>
<p>Okay, so, to aid in making it easy to iterate over the range, I was using the <em>range operator</em> in Perl, which is the little &#8220;..&#8221; thing you can do, (or dotdot, or dot-dot, or whatever you might call it). For example:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">..</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>.. or, being a little more realistic:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$start</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$end</span>   <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$i</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$start</span> <span style="color: #339933;">..</span> <span style="color: #0000ff;">$end</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Run those, and you&#8217;ll get all the numbers from 1 to 100. Fantastic. Want to get even more crazy? How about letters. Yeah, it does those too. Sweet.</p>
<p>In this example, we&#8217;ll get everything from a to z:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$start</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;a&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$end</span>   <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;z&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$i</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$start</span> <span style="color: #339933;">..</span> <span style="color: #0000ff;">$end</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Hrmm, what about if we want the range from a to zzzzz? Yup, it does that too:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$start</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;a&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$end</span>   <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;zzzzz&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$i</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$start</span> <span style="color: #339933;">..</span> <span style="color: #0000ff;">$end</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Of course, I&#8217;m not giving you the output, because that would be insane.. but it works. Nice!</p>
<p>There&#8217;s actually a bunch more you can do with it, too. There&#8217;s even a &#8220;&#8230;&#8221; (dot-dot-dot) that you can do something with (but I forget what). However, it is all documented here:</p>
<p><a href="http://perldoc.perl.org/perlop.html#Range-Operators">http://perldoc.perl.org/perlop.html#Range-Operators</a>.</p>
<p>But that&#8217;s not what this post is about. It&#8217;s about something interesting I ran into. Still interested? Good. Keep reading.</p>
<p>So anyway, in the case of my config parser, I would parse out ranges as tokens (ie: 0200-0300), split, and do a search across the range. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># $range = &quot;0200-0300&quot;</span>
<span style="color: #b1b100;">my</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$start</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$end</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$range</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/^(\d+)\-(\d+)$/</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$id</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$start</span> <span style="color: #339933;">..</span> <span style="color: #0000ff;">$end</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>found_job_id<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      do_something_with<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This basically allows us to lazily put large ranges into the config, but requires us to be more specific when creating jobs with new IDs (to ensure it falls within our range). In any case, this works exactly as we&#8217;d expect, leading zeroes and all.</p>
<p>Now, as part of error checking, I decided I should make sure that the <em>$start</em> value was less than the <em>$end</em> value, since the range operator will not work if <em>$start</em> is greater than <em>$end</em>. It will just return an empty set. This is documented and not an error, so I wanted to throw an error.</p>
<p>To do so, I just modified my code to implement a simple comparison check:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># $range = &quot;0200-0300&quot;</span>
<span style="color: #b1b100;">my</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$start</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$end</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$range</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/^(\d+)\-(\d+)$/</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$start</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000ff;">$end</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   throw_error_and_exit<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Start is greater than end! Fix your range, buddy.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$id</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$start</span> <span style="color: #339933;">..</span> <span style="color: #0000ff;">$end</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>found_job_id<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      do_something_with<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The change seemed harmless enough, but when I ran the config parser again to make sure things worked, I got nothing. Literally. Nothing. It appeared as if everything broke. Weird. No errors, the range is valid (I was using 0100-0200), but, um, what?</p>
<p>After scratching my head for about 20 seconds, I went, &#8220;aha!&#8221; Yes, just like that.</p>
<p>Sure enough, <strong>when I parse out my range from the config, the values I get back are strings</strong> (which makes sense), so in the case of <em>0200-0300</em>, the range operator is actually performing the operation based on the string value of each, using what they consider the &#8220;magical auto-increment algorithm&#8221; (per the Perl documentation). I don&#8217;t know what type of analysis it does, but it&#8217;s probably just based on ASCII values.</p>
<p>In any case, <strong>AFTER</strong> I added my new comparison operation (to check if <em>$start</em> was greater than <em>$end</em>), <strong>Perl had to typecast my variables</strong> (which were strings) <strong>to integers to compare them, therefore dropping the leading zeroes</strong>! Here&#8217;s something a little more graphic:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># $range = &quot;0200-0300&quot;</span>
<span style="color: #b1b100;">my</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$start</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$end</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$range</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/^(\d+)\-(\d+)$/</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># &lt;--- STRINGS</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$start</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000ff;">$end</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;"># &lt;--- OMG NOW THEY'RE INTEGERS</span>
   throw_error_and_exit<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Start is greater than end! Fix that.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$id</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$start</span> <span style="color: #339933;">..</span> <span style="color: #0000ff;">$end</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;"># &lt;--- HEY WHERE'D THE LEADING ZERO GO?</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>found_job_id<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      do_something_with<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is one of those things with Perl where a mix of dynamic and weak typing features lead to strings magically (and uncontrollably) being casted to integers where needed. In most cases, having a language with both dynamic and weak typing on variables can be nice, but it can also lead to laziness and unexpected results if you&#8217;re not careful. <img src='http://wildpointer.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Well, that&#8217;s all for today. Just thought I&#8217;d share that one.</p>
]]></content:encoded>
			<wfw:commentRss>http://wildpointer.org/2010/11/23/perl-typecast-beware-weak-typing-and-the-range-operator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>writing a custom Netscaler health check for NTP</title>
		<link>http://wildpointer.org/2010/04/11/writing-a-custom-netscaler-health-check-for-ntp/</link>
		<comments>http://wildpointer.org/2010/04/11/writing-a-custom-netscaler-health-check-for-ntp/#comments</comments>
		<pubDate>Sun, 11 Apr 2010 09:12:48 +0000</pubDate>
		<dc:creator>chet</dc:creator>
				<category><![CDATA[netscaler]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://chetnichols.org/?p=18</guid>
		<description><![CDATA[There are lots of load balancing products on the market today. Some are good, some are bad. Opinions aside, one such product is the Citrix Netscaler. Now, the Netscaler has built in health checking for many different types of backend service protocols: HTTP, FTP, DNS, and more. However, one protocol not included is NTP. If [...]]]></description>
			<content:encoded><![CDATA[<p>There are lots of load balancing products on the market today. Some are good, some are bad. Opinions aside, one such product is the Citrix Netscaler.</p>
<p>Now, the Netscaler has built in health checking for many different types of backend service protocols: HTTP, FTP, DNS, and more. However, one protocol not included is NTP. If you happen to be load balancing a farm of NTP servers, and want to ensure you&#8217;re not routing to a hosed NTP daemon (even if it&#8217;s ping-able), it would be nice to be able to health check the service [and take it out of rotation if it's not responding].</p>
<p>Luckily, the Netscaler provides (with documentation) a very flexible way to write your own custom health checks. Using the system, you can test whatever you want, in any way you want, with just a little Perl. Even better, the custom health check subsystem provides a few useful things to simplify the whole process, including:</p>
<ul>
<li>passing in the service IP, and port, as arguments to your script</li>
<li>passing in a custom argument string (key value pairs, user/pass info, whatever)
<li>handling the timeout for you from a higher level; if your check doesn&#8217;t respond within -respTimeout seconds, it will be considered a failure.</li>
</ul>
<p>Now, for just the basics, this monitor will actually be pretty easy to put together. We are given the IP/port to send an NTP request to, and we don&#8217;t need to worry about a timeout, because the underlying subsystem will handle that for us. All we really need to do is send a request and [hope] for a response!</p>
<p>That being said, we&#8217;ll need to do a couple things to get to that point. These things are:</p>
<ol>
<li>Load the health check subsystem module.</li>
<li>Load the Socket module, and create a socket to use to send the request [using the parameters passed in as args].</li>
</ol>
<p>From that point, we can build and send our request. Since we don&#8217;t need to worry about handling a timeout, we can just sit there waiting for a response. First up:</p>
<pre>
use IO::Socket;
use Netscaler::KAS;
</pre>
<p>We&#8217;ll need both of these modules &#8211; one will tap into the health check subsystem, and the other is so we can send our UDP-based request.</p>
<p>Next is our function, which we&#8217;ll just call <i>ntp_probe</i>. It assigns a few variables from <i>$_[0]</i> (the service IP) and <i>$_[1]</i> (the service port) as arguments, which get passed in from work within probe().</p>
<p>So, the magic-fu to this is that, later on, we will pass our <i>ntp_probe</i> function as a coderef to the KAS probe() command. That will ultimately result in our function being called with the arguments passed in; it is similar to this:</p>
<pre>
$custom_function->($host,$port,$args);
</pre>
<p>If you look at the KAS code (and since it&#8217;s perl, you can), you can see the exact line; this just gives you an idea of what&#8217;s going on.</p>
<p>In any case, after that, we&#8217;ll create a new UDP socket, send the NTP request message down it, and either:</p>
<ul>
<li>return 1 if something wasn&#8217;t defined (ip, port, or sock) &#8211; failure!</li>
<li>return 0 if we got a response &#8211; success!</li>
</ul>
<p>Again, we don&#8217;t need to worry about timeouts, since the subsystem will handle that for us. So, here&#8217;s the function in it&#8217;s entirety:</p>
<pre>
sub ntp_probe {

    my $host    = $_[0];
    my $port    = $_[1];
    my $req     = "\010"."\0"x47;

    if(!$host || !$port) {
        return(1,"Host or port not specified.");
    }

    my $sock = IO::Socket::INET->new(
        Proto     => "udp",
        PeerAddr  => $host,
        PeerPort  => $port,
    );

   if($sock) {
        $sock->send($req);
        $sock->recv($_,1);
        return 0;
    }

    return(1,"Could not create socket");

}
</pre>
<p>Not bad, was it? Now that we have loaded our modules, and defined a function, the next step is to call the probe() method, with a coderef to our new function, to tell KAS what function to use for probing:</p>
<pre>
probe(\&#038;ntp_probe);
</pre>
<p>And you&#8217;re off to the races! Just don&#8217;t forget to start your script with <i>#!/usr/bin/perl</i>, and it should be good to go <img src='http://wildpointer.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Now, you might be asking &#8220;how do I actually configure the Netscaler to use this?&#8221; That&#8217;s also just as easy. Let&#8217;s pretend our new script is called <i>customNTP.pl</i>. Just plop it into the <b>/nsconfig/monitors/</b> directory, and do something like this from the CLI:</p>
<pre>
add lb monitor "custom-ntp-mon" USER -scriptName customNTP.pl
   -scriptArgs 1 -dispatcherIP 127.0.0.1 -dispatcherPort 3013
   -interval 20 -respTimeout 3
</pre>
<p>With that line, you will now have a working NTP healthcheck (20 second intervals, 3 second timeout). Just bind it to your service(s) that need it and enjoy!. Note that the type of monitor is <b>USER</b>; this is the type used for custom health checks.</p>
<p>Well, that&#8217;s all for this evening. There are bits and pieces of info like this around the web, on the Citrix AppExpert site, etc. And, even though it wasn&#8217;t too difficult to figure out, hopefully this will help someone out some day.</p>
<p>Take care!</p>
]]></content:encoded>
			<wfw:commentRss>http://wildpointer.org/2010/04/11/writing-a-custom-netscaler-health-check-for-ntp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>use perl: pack() to send floats between intel/sparc with proper endian-ness.</title>
		<link>http://wildpointer.org/2010/02/22/use-perl-pack-to-send-floats-between-intelsparc-with-proper-endian-ness/</link>
		<comments>http://wildpointer.org/2010/02/22/use-perl-pack-to-send-floats-between-intelsparc-with-proper-endian-ness/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 08:13:39 +0000</pubDate>
		<dc:creator>chet</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://chetnichols.org/?p=78</guid>
		<description><![CDATA[So I like using Perl, if anyone hasn&#8217;t realized yet. Python seems awesome and all, but I haven&#8217;t had the time to start writing things in Python. I&#8217;m still at the stage when, if a tool needs to be put together, it needs to be done ASAP; I can&#8217;t delay trying to figure out Python [...]]]></description>
			<content:encoded><![CDATA[<p>So I like using Perl, if anyone hasn&#8217;t realized yet. Python seems awesome and all, but I haven&#8217;t had the time to start writing things in Python. I&#8217;m still at the stage when, if a tool needs to be put together, it needs to be done ASAP; I can&#8217;t delay trying to figure out Python particulars. </p>
<p>In any case, one of our metrics publishing systems is wrapped by a few different object classes I put together in Perl (follows a certain protocol created by another group, etc). Of the many data types supported by our metrics receivers, we can do things like 32-bit unsigned integers, single precision (32-bit) floating points, and a few others.</p>
<p>Now, when I would publish the 32-bit uint&#8217;s, it would work great- no issues. The receivers would expect a byte stream, so I would use pack() to send my values as binary. For example:</p>
<pre code="Perl">
$val=29;
print $sock pack('N',$val);
</pre>
<p>Easy, whatever. However, when I would send values as a single precision float, it would totally be hosed on the other end. For example:</p>
<pre code="Perl">
$val="30.0";
print $sock pack('f',$val);
</pre>
<p>On the other end, they would get some massive negative number. I don&#8217;t deal with this too much, things usually just work. Thankfully, my mind was jogged in regards to standards- there are lots of standards around byte order for integers (16 bit, 32 bit, 64 bit), but not as much for float; one architecture may use big-endian, and another architecture might use little-endian.</p>
<p>In my searches, I found that Intel stores floating points in little-endian byte order, and Sparc stores them in big-endian byte order. Sure enough, I was trying to publish from an Intel-based BSD host to a Sparc based Solaris host. Aha!</p>
<p>Luckily, pack() can *also* handle this for us! With a simple carat added to the templates section of pack(), I can easily pack the value in big-endian byte order and resolve our issue. I do that like so:</p>
<pre code="Perl">
$val="30.0";
print $sock pack('f>',$val);
</pre>
<p>And, ta da! Worked like a charm. Was it difficult to get it working? No. But, it was fun to have to deal with something like this. On any given day, you don&#8217;t really worry about endian-ness; things just work. This was one of those days where I actually had to use the old noggin, and that was refreshing.</p>
]]></content:encoded>
			<wfw:commentRss>http://wildpointer.org/2010/02/22/use-perl-pack-to-send-floats-between-intelsparc-with-proper-endian-ness/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>adventures in perl: foreach returns pointers to elements</title>
		<link>http://wildpointer.org/2009/12/17/adventures-in-perl-foreach-returns-pointers-to-elements/</link>
		<comments>http://wildpointer.org/2009/12/17/adventures-in-perl-foreach-returns-pointers-to-elements/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 08:58:50 +0000</pubDate>
		<dc:creator>chet</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://chetnichols.org/?p=55</guid>
		<description><![CDATA[I&#8217;m not sure how I&#8217;ve never run into this issue before. In some work I was doing recently, I ran into what I thought was a nasty bug [in my code] but couldn&#8217;t explain it. Without thinking, I started trying to undo this, fix that, hack this, and ignore that. After the meeting I was [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not sure how I&#8217;ve never run into this issue before. In some work I was doing recently, I ran into what I thought was a nasty bug [in my code] but couldn&#8217;t explain it. Without thinking, I started trying to undo this, fix that, hack this, and ignore that. </p>
<p>After the meeting I was in finished, I was able to sit down and actually put some thought into what was happening. The one thing that stood out was interesting, but until then, I had no clue it was how Perl interpreted [in that scenario]. So, I wrote a short test script, sure enough proving the theory, and was able to fix my error.</p>
<p>Take a look at this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@animals</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;cat&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;dog&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;emu&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;frog&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$animal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@animals</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;do not eat the %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$animal</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>which dumps out this:</p>
<p><code>do not eat the cat<br />
do not eat the dog<br />
do not eat the emu<br />
do not eat the frog<br />
</code></p>
<p><b>In my head</b>, the code above would work like this: for each element in the array @animals, copy the data from that element into a new scalar $animal, then print it out. Simple enough. Now, consider this sample:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@animals</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;cat&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;dog&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;emu&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;lemur&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$animal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@animals</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;do not eat the %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$animal</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$animal</span> <span style="color: #339933;">=</span> <span style="color: #000066;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;rabid %s&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$animal</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$animal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@animals</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;do not eat the %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$animal</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I had *EXPECTED* it to output this:</p>
<p><code>do not eat the cat<br />
do not eat the dog<br />
do not eat the emu<br />
do not eat the frog</p>
<p>do not eat the cat<br />
do not eat the dog<br />
do not eat the emu<br />
do not eat the frog<br />
</code></p>
<p>For the first loop, again, <b>my thinking</b> was that we copy each array element&#8217;s data into the new scalar $animal, print it, modify it (by adding &#8216;rabid&#8217; to it), but do nothing with the modification (we are just modifying $animal, which should be assigned by copy, of which would be lost when we iterate to the next element). Then, in our next loop, we iterate over @animals again, initializing $animal yet again for each element, so we just hit the un-modified @animals array and see the same thing.</p>
<p>[un]Surprisingly, <b>I was totally wrong</b>. This was the output I got:</p>
<p><code>do not eat the cat<br />
do not eat the dog<br />
do not eat the emu<br />
do not eat the frog</p>
<p>do not eat the rabid cat<br />
do not eat the rabid dog<br />
do not eat the rabid emu<br />
do not eat the rabid frog<br />
</code></p>
<p>Wow! What happened? Turns out, like the title suggests, when you use foreach in Perl, <b>it actually assigns $animal to be a pointer</b> (reference, whatever) to that array element, and not a copy. When you make any changes, the change applies directly to the element the array. As another example, it&#8217;s for-loop equivalent would be this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@animals</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;cat&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;dog&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;emu&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;lemur&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$index</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">;</span> <span style="color: #0000ff;">$index</span> <span style="color: #339933;">&lt;</span> <span style="color: #0000ff;">@animals</span> <span style="color: #339933;">;</span> <span style="color: #0000ff;">$index</span><span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;do not eat the %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$animals</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$index</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$animals</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$index</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066;">sprintf</span><span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;rabid %s&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$animals</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$index</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Fun! Doing some research after the fact, it turns out there has been some mild discussion on the topic. Some consider it a bug, but <b>in reality, it works by design</b>. What does this mean for you? Well, if you happen to be iterating over an array using foreach, and want to modify each element, and plan on looping over the array multiple times, make sure you understand what&#8217;s going on behind the scenes, or else you&#8217;ll run into the same issue I did. You can create a temporary variable (which will assign by copy), ie:</p>
<p><code>my $new_animal = $animal;</code></p>
<p>or you can iterate using a for-loop and just do it like this:</p>
<p><code>my $animal = $animals[$index];</code></p>
<p>There of course may be some cases where you want to take advantage of the referencing feature, and if you do, all the more power to you.</p>
<p>Well, that&#8217;s all I have for now. Hopefully this helps someone out some day. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://wildpointer.org/2009/12/17/adventures-in-perl-foreach-returns-pointers-to-elements/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

