<?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 Integration Engineer &#187; PerlTips</title>
	<atom:link href="http://www.theintegrationengineer.com/category/script/perl/perl-tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.theintegrationengineer.com</link>
	<description>When it just has to work.</description>
	<lastBuildDate>Mon, 06 Feb 2012 15:52:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Perl Tricks &#8211; When errors happen</title>
		<link>http://www.theintegrationengineer.com/perl-tricks-when-errors-happen/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=perl-tricks-when-errors-happen</link>
		<comments>http://www.theintegrationengineer.com/perl-tricks-when-errors-happen/#comments</comments>
		<pubDate>Thu, 23 Sep 2010 15:03:08 +0000</pubDate>
		<dc:creator>Roy</dc:creator>
				<category><![CDATA[Logging]]></category>
		<category><![CDATA[PerlTips]]></category>
		<category><![CDATA[alert]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[error messages]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[monitoring]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://www.theintegrationengineer.com/?p=1071</guid>
		<description><![CDATA[Back in the day, I used to use a program called Windows made by a little company called Microsoft.  I liked windows, and for the most part it did what I needed it to do.  But it also had a nasty habit of crashing when I tried to do something complex.  And this was really [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.theintegrationengineer.com/wp-content/uploads/2009/08/monitoring_pzl.jpg"><img class="alignleft size-thumbnail wp-image-596" title="monitoring_pzl" src="http://www.theintegrationengineer.com/wp-content/uploads/2009/08/monitoring_pzl-150x150.jpg" alt="monitoring pzl 150x150 Perl Tricks   When errors happen" width="150" height="150" /></a>Back in the day, I used to use a program called Windows made by a little company called Microsoft.  I liked windows, and for the most part it did what I needed it to do.  But it also had a nasty habit of crashing when I tried to do something complex.  And this was really annoying.</p>
<p>Well as time went on, and I became more adept at using windows, I noticed that it tried to tell me what was wrong.  Windows was writing out a crash report.  And it had a message on the blue screen of death.  (a mostly incomprehensible message, but at least it was trying.)  And I discovered that if you looked for the error codes in Microsoft&#8217;s knowledge base, you could find the errors had corresponding articles for many of them.  (some of these articles were also incomprehensible.  )<span id="more-1071"></span></p>
<p>Well when you write an application in perl or any other language you will need to do something other than just crash when you encounter an error.  There are two views on what to do with an error condition; write the message to a log, and write the error out in a separate error message or file.</p>
<p>I guess there could be a third view, and that is to do both.</p>
<p><strong>Logging:</strong></p>
<p>Writing errors to a log, can be just part of the normal logging that you do as your application processes.  If you aren&#8217;t logging, you might want to consider it.  There is value in having a log file of what your application is doing.  In order to keep your logging coherant, I create a logging object and then just pass my log messages to it.  This way they can all have the same format with timestamps and such without having to write it out every time.</p>
<p><strong>Error Messages:</strong></p>
<p>My application is processing messages, transforming CSV and other types of files.  So when errors in processing happen, I move the files to an error directory, and I also write a special error file with them so that I don&#8217;t have to parse through the log to find it.</p>
<p><strong>Using alerts:</strong></p>
<p>Writing messages to a log and moving files to an error directory are fairly passive ways to handle errors.  A more active way to handle and error is to send an email or trigger an alert of some kind.  You can also use a monitoring technology like nagios to watch for errors, and send alerts.</p>
<p>What are ways that you have handled errors and logging?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theintegrationengineer.com/perl-tricks-when-errors-happen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sending Email with Perl</title>
		<link>http://www.theintegrationengineer.com/sending-email-with-perl/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sending-email-with-perl</link>
		<comments>http://www.theintegrationengineer.com/sending-email-with-perl/#comments</comments>
		<pubDate>Wed, 26 May 2010 16:05:27 +0000</pubDate>
		<dc:creator>Roy</dc:creator>
				<category><![CDATA[PerlTips]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.theintegrationengineer.com/?p=108</guid>
		<description><![CDATA[Recently I needed to send our team an email.  Yeah okay, so I email a lot, but this was different.  I needed to send my team an email from a server that was not anywhere near a mail server, and that was pretty paired down.  I did a bunch of Googling for answers and came [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.theintegrationengineer.com/wp-content/uploads/2008/09/envelope.jpg"><img class="alignleft size-full wp-image-28" title="envelope" src="http://www.theintegrationengineer.com/wp-content/uploads/2008/09/envelope.jpg" alt="envelope Sending Email with Perl" width="257" height="75" /></a>Recently I needed to send our team an email.  Yeah okay, so I email a lot, but this was different.  I needed to send my team an email from a server that was not anywhere near a mail server, and that was pretty paired down.  I did a bunch of Googling for answers and came up with quite a few that didn&#8217;t work.</p>
<p>I hate it when that happens.  So as a form of perl tutorial, I am posting my working script here as an example of sending email from a limited server using perl and the MIME::Lite library.</p>
<p><span id="more-108"></span></p>
<ul>
<li>Server:  CentOS 5.2</li>
<li>Internet connection: Present</li>
<li>Task:  Automation Scripts written in Perl</li>
</ul>
<p>So I had perl, but how to send an email.  Well the first thing, is get the mail libraries.  On a Red Hat based server, this is yum install perl-MIME-tools.  You may have a different way.  But this is required for the sending messages in MIME.  (if you can&#8217;t install perl and perl modules, you may want to use CPAN)</p>
<p>Then I wrote this perl script:</p>
<p>&#8212;-begin code  &#8212;-</p>
<p style="padding-left: 30px;">#!/usr/bin/perl<br />
#declare that you are using the MIME library at the top.<br />
use MIME::Lite;</p>
<p style="padding-left: 30px;">#list files in /Email into array<br />
opendir MYDIR, &#8220;/share/Email/&#8221; or die &#8220;couldn&#8217;t open $! \n&#8221;;</p>
<p style="padding-left: 30px;">#This will only grab files with &#8220;PDF&#8221; in their name.</p>
<p style="padding-left: 30px;">#this will eliminate other files, and more importantly, &#8220;.&#8221; and &#8220;..&#8221; that are directories.<br />
@contents = grep(/pdf/,readdir (MYDIR));<br />
closedir MYDIR;</p>
<p style="padding-left: 30px;">#we want to know how many files are in our list.<br />
$fileCount = @contents;</p>
<p style="padding-left: 30px;">#loop array to sendmail function<br />
for ($i = 0; $i &lt; $fileCount; ++$i){<br />
$fileName = &#8220;/share/Email&#8221; . $contents[$i];<br />
$Subject = &#8220;Alert Request for: &#8221; . $contents[$i];<br />
#print $fileName . &#8220;\n&#8221;;</p>
<p style="padding-left: 30px;">my $msg = MIME::Lite-&gt;new(<br />
From    =&gt; &#8216;mail@yourserver.com&#8217;,<br />
To    =&gt; &#8216;roy@TheIntegrationEngineer.com&#8217;,<br />
Cc    =&gt; &#8221;,<br />
Subject    =&gt; $Subject,<br />
Type    =&gt; &#8216;multipart/mixed&#8217;,<br />
);</p>
<p style="padding-left: 30px;">$msg-&gt;attach(<br />
Type    =&gt; &#8216;Text&#8217;,<br />
Data    =&gt; &#8216;Please read this file&#8217;,<br />
);</p>
<p style="padding-left: 30px;">$msg-&gt;attach(<br />
Type    =&gt; &#8216;Data&#8217;,<br />
Path    =&gt; $fileName,<br />
Filename    =&gt; $contents[$i],<br />
);</p>
<p style="padding-left: 30px;">$msg-&gt;send(&#8216;smtp&#8217;, &#8216;mail.yourserver.com&#8217;);</p>
<p style="padding-left: 30px;">#Remove File<br />
$cmd = &#8220;mv &#8221; . $fileName . &#8221; /alert/backup/&#8221; . $filename . &#8220;.&#8221; . epoc . &#8221; \n&#8221;;<br />
print $cmd;<br />
system ($cmd);<br />
}</p>
<p>&#8212;- end code &#8212;-</p>
<p><strong>Summary</strong></p>
<p>Basically this script polls the specified directory for PDF files.  If it finds one or more it grabs each of them and emails them to me.  Then, as a last step, it moves the file to a backup location to be archived.</p>
<p>There are probably other ways to do this, but this is a script that I threw together and have working on one of my severs.  If anyone want to add some corrections to this I will try them out and post my results.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theintegrationengineer.com/sending-email-with-perl/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Perl Tricks &#8211; timestamps</title>
		<link>http://www.theintegrationengineer.com/perl-tricks-timestamps/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=perl-tricks-timestamps</link>
		<comments>http://www.theintegrationengineer.com/perl-tricks-timestamps/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 15:17:53 +0000</pubDate>
		<dc:creator>Roy</dc:creator>
				<category><![CDATA[PerlTips]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[date stamp]]></category>
		<category><![CDATA[datestamp]]></category>
		<category><![CDATA[localtime]]></category>
		<category><![CDATA[log]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[Per Tip]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[time-stamp]]></category>
		<category><![CDATA[timestamp]]></category>

		<guid isPermaLink="false">http://www.theintegrationengineer.com/?p=1069</guid>
		<description><![CDATA[Running perl integration tools we have two basic needs for time stamps and date time values. First, it is useful to note the time that something is happening in a log file so that we don&#8217;t have to guess. Second, it is also useful to append the date to a file name to keep our [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.theintegrationengineer.com/wp-content/uploads/2009/11/stopwatch.jpg"><img class="alignleft size-thumbnail wp-image-975" title="stopwatch" src="http://www.theintegrationengineer.com/wp-content/uploads/2009/11/stopwatch-150x150.jpg" alt="stopwatch 150x150 Perl Tricks   timestamps" width="150" height="150" /></a>Running perl integration tools we have two basic needs for time stamps and date time values.</p>
<p>First, it is useful to note the time that something is happening in a log file so that we don&#8217;t have to guess.</p>
<p>Second, it is also useful to append the date to a file name to keep our logs in daily files not just one long never ending file.</p>
<p>Here is a function that I use to do both of these things.<span id="more-1069"></span></p>
<hr />sub timestamp($)<br />
{</p>
<p style="padding-left: 30px;">#return pre-formatted timestamps based on the localtime()<br />
#&#8221;date&#8221; asks for a Month-Day-Year string.<br />
#&#8221;date&#8221; time asks for the date and time.<br />
#anything else returns the epoch date.<br />
my $param = $_[0];<br />
if ($param eq &#8220;date&#8221;)<br />
{</p>
<p style="padding-left: 60px;">@time = split(/\ /, localtime());<br />
$return = $time[1] . &#8220;-&#8221; . $time[2] . &#8220;-&#8221; . $time[4];<br />
return $return;</p>
<p style="padding-left: 30px;">}<br />
elsif ($param eq &#8220;datetime&#8221;)<br />
{</p>
<p style="padding-left: 60px;">return localtime();</p>
<p style="padding-left: 30px;">}<br />
else<br />
{</p>
<p style="padding-left: 60px;">return time();</p>
<p style="padding-left: 30px;">}</p>
<p>}</p>
<h2>How to use</h2>
<p>When we call this function within a perl script we will want one of three things back.</p>
<p>1.  If we want the date or DATESTAMP, we will call it in this manner,</p>
<blockquote><p>timestamp(date)</p></blockquote>
<p>and it will return the formatted date that we have formatted like this, &#8220;Mar-29-2010&#8243;</p>
<p>This call can be withing a string like a file name   $filename = &#8220;Log&#8221; . timestamp(date) . &#8220;.log&#8221;;  or just set as a variable, $date = timestamp(date);</p>
<p>2.  If we want the date and time or TIMESTAMP, we will call it in this manner,</p>
<blockquote><p>timestamp(datetime)</p></blockquote>
<p>and it will return a formatted string like this, &#8220;Thu Apr 15 17:01:10 2010&#8243;</p>
<p>This can be called the same way.  However, don&#8217;t use this in a file name.  It  contains spaces.  And having a unique file every second might not be useful to you.</p>
<p>3. If we just want the epoc date back, we just call it in this maner,</p>
<blockquote><p>timestamp()</p></blockquote>
<p>but why bother.  I wanted the function to return something all the time so that you can tell when you have the parameter flag misspelled, but you could just you <em>time</em>() for that.</p>
<p>In any case this is a function that I like to use when I am creating timestamps for my log files.  It is easily extended by adding new <em>elsif </em>conditions and handling the <em>localtime</em>() or <em>time</em>() with a different split or regular expression.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theintegrationengineer.com/perl-tricks-timestamps/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/

Page Caching using disk: enhanced
Database Caching using disk: basic

Served from: www.theintegrationengineer.com @ 2012-02-08 00:10:53 -->
