<?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>Connecting the dots... &#187; perl</title>
	<atom:link href="http://blog.rajatpandit.com/tag/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.rajatpandit.com</link>
	<description>Thoughts on Web Development, Infrastructure and Application Scalability</description>
	<lastBuildDate>Thu, 29 Dec 2011 13:21:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Installing modules using CPAN interactive shell in perl</title>
		<link>http://blog.rajatpandit.com/2009/01/28/installing-modules-using-cpan-interactive-shell-in-perl/</link>
		<comments>http://blog.rajatpandit.com/2009/01/28/installing-modules-using-cpan-interactive-shell-in-perl/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 12:45:21 +0000</pubDate>
		<dc:creator>rp</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[cpan]]></category>
		<category><![CDATA[mechanize]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[rocking]]></category>

		<guid isPermaLink="false">http://blog.rajatpandit.com/?p=180</guid>
		<description><![CDATA[We have all done this, get a *.tar.gz file from cpan, do a configure, make, test and make install only to realize that the dependencies for the package are missing...]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.rajatpandit.com/wp-content/uploads/2009/01/cpan_banner.png" alt="CPAN" title="CPAN" width="264" height="77" class="alignright size-full wp-image-183" />We have all done this, get a *.tar.gz file from cpan, do a configure, make, test and make install only to realize that the dependencies for the package are missing and then going and fetching them from CPAN and downloading it again and doing the process over and over again. CPAN Shell is one of the many gems that PERL hold and this is how you use it.</p>
<p>Starting the Shell</p>
<pre class="brush: bash; title: ; notranslate">
perl -MCPAN -eshell
</pre>
<p>Note: You might want to start that command using <code>sudo</code> in case you have permission issues. This depends on your setup.</p>
<p>That shows you the shell.</p>
<pre class="brush: bash; title: ; notranslate">
cpan&gt;
</pre>
<p>Now give it the command to install (I wanted to install <code>WWW:Mechanize</code> for my little project)</p>
<pre class="brush: bash; title: ; notranslate">
cpan&gt;install WWW::Mechanize
</pre>
<p>That triggers it go and automatically fetch the package, unpack it and compile it and in the process find all the dependencies. Now this package has a hell lot of dependency and if you plan on installing it manually be prepared for a couple of really long hours. CPAN shell quite intelligent in the way it looks for dependencies and shows you what they are and asking you if it should go and fetch them for you:</p>
<pre class="brush: bash; title: ; notranslate">
Checking if your kit is complete...
Looks good
Warning: prerequisite Sub::Uplevel 0.18 not found.
Warning: prerequisite Test::Builder 0.7 not found. We have 0.32.
Warning: prerequisite Test::Builder::Tester 1.07 not found. We have 1.02.
Warning: prerequisite Test::More 0.7 not found. We have 0.62.
Warning: prerequisite Test::Simple 0.7 not found. We have 0.62.
Writing Makefile for Test::Exception
---- Unsatisfied dependencies detected during [A/AD/ADIE/Test-Exception-0.27.tar.gz] -----
    Test::More
    Test::Builder
    Sub::Uplevel
    Test::Simple
    Test::Builder::Tester
Shall I follow them and prepend them to the queue
of modules we are processing right now? [yes]
</pre>
<div id="in_post_ad_bottom_1" style="clear:both;margin:0;padding:0;"><div class="brp-bp-234">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-4254382394977039";
/* brp-234x60-bp */
google_ad_slot = "7787511801";
google_ad_width = 234;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<div class="brp-bp-234">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-4254382394977039";
/* brp-234x60-BP-1 */
google_ad_slot = "9111022353";
google_ad_width = 234;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div></div><div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://blog.rajatpandit.com/2009/01/28/installing-modules-using-cpan-interactive-shell-in-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing an IRC Bot</title>
		<link>http://blog.rajatpandit.com/2008/02/25/writing-an-irc-bot/</link>
		<comments>http://blog.rajatpandit.com/2008/02/25/writing-an-irc-bot/#comments</comments>
		<pubDate>Mon, 25 Feb 2008 15:50:29 +0000</pubDate>
		<dc:creator>rp</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[bot]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[irc]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[yahoo search]]></category>
		<category><![CDATA[Yahoo!]]></category>

		<guid isPermaLink="false">http://blog.rajatpandit.com/2008/02/25/writing-an-irc-bot/</guid>
		<description><![CDATA[I had always been intrigued about IRC Bots and always thought it was quite cool to be able to write one, esp when i would see loads of script kiddies...]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.rajatpandit.com/wp-content/uploads/2008/02/rbot.pl" title="IRC Search Bot"></a>I had always been   intrigued about IRC Bots and always thought it was quite cool to be able to write one, esp when i would see loads of script kiddies downloading stuff and setting it up on free shell accounts. recently at work it was suddenly a season for writing bots, it seemed like everyone was writing one, for a useful purpose ofcourse so i though i would jump it and give it a shot.</p>
<p>So as it turned out to be a fairly straight forward job to do. Since I was happy writing stuff in Perl i decided to write the bot in perl like most of my hack are.</p>
<p>Setting up the development/deployment environment:<br />
Install the Bot::BasicBot from cpan</p>
<pre class="brush: bash; title: ; notranslate">
#sudo perl -MCPAN -e 'install Bot::BasicBot'
</pre>
<p>This would go out for you and get all the required modules and its dependencies.</p>
<p>Writing the actual bot:<br />
The API for the pm is pretty well documented and has a good enough example to get you started. Writing the bot using this PERL module was pretty much like writing a SAX parser where you would override methods.</p>
<p>So lets get started with the basic bot:</p>
<pre class="brush: perl; title: ; notranslate">
#!/usr/bin/perl
use warnings;
use strict;

package MyBot;
use base qw(Bot::BasicBot);

# Create an instance of the bot and start it running. Connect
# to the main perl IRC server, and join some channels.
MyBot-&gt;new(
  server =&gt; 'irc.freenode.net',
  channels =&gt; ['#testbot'],
  nick =&gt; 'blabot'
  )-&gt;run();
</pre>
<p>Now do a <code>chmod a+x </code> on this bot and run it, this will cause the bot to come to life, connect to the server and join the channel as specified in the MyBot&#8217;s constructor. You can find other interesting attributes to assign in the constructor <a href="http://search.cpan.org/dist/Bot-BasicBot/lib/Bot/BasicBot.pm#ATTRIBUTES">here</a></p>
<p>This is a pretty boring bot, it doesnt do much it just joins the channel and sits there doing nothing. To make it do something, you need to override interesting &#8216;events&#8217; for the bot to do something. Some of the interesting events are</p>
<ul>
<li><strong>chanjoin</strong>: This is called everytime someone joins the channel</li>
<li><strong>said</strong>: this function is called everytime someone says something</li>
<li><strong>help</strong>: this is called when someone calls the bot with the message help</li>
<li>Other interesting events can be found on the <a href="http://search.cpan.org/dist/Bot-BasicBot/lib/Bot/BasicBot.pm#METHODS_TO_OVERRIDE">API page</a></li>
</ul>
<p>So continuing to write something more useful we go ahead and update the code with some new overridden methods.</p>
<pre class="brush: perl; title: ; notranslate">
sub chanjoin {
    my ($self, $message) = @_;
    if ($message-&gt;{who} eq 'rajat') {
        return 'Hello Rajat, how are you doing!!';
    }
}
</pre>
<p>This is a clever one, it greets me everytime I join the channel, ok maybe not so much.</p>
<p>Ok so to make the bot do something useful, I get to make a search query on yahoo.com everytime one calls it with a search term. So i go ahead and install the Yahoo::Search module as well.</p>
<pre class="brush: bash; title: ; notranslate">
#sudo perl -MCPAN -e 'install Yahoo::Search'
</pre>
<p>Which does all the installation magic  for you.</p>
<p>Loads the Module in the perl script we are writing.</p>
<pre class="brush: perl; title: ; notranslate">
#!/usr/bin/perl
use warnings;
use strict;
use Yahoo::Search;
</pre>
<p>and then override the <strong>said</strong> method:</p>
<pre class="brush: perl; title: ; notranslate">
sub said {
  my ($self, $message) = @_;
  my $out;
  my $address;
  $address  = $message-&gt;{address};
  if (!$address) {
      $address = &quot;&quot;;
  }
  if ($address eq 'blabot') {
      my @results = Yahoo::Search-&gt;Results(
          Doc =&amp;gt; $message-&gt;{body},
          AppId =&gt; &quot;YahooDemo&quot;,
          Mode =&gt;'all',
          Start =&gt; 0,
          Count =&gt;5,
          Type =&gt;'any',
          AllowAdult =&gt; 0,
          AllowSimilar =&gt; 0,
          Language =&gt; undef);      

      for my $result(@results) {
          $out = $out . ' ' . $result-&gt;Url . &quot;\n&quot;;
      }

  }
  return $out;
}
</pre>
<p>and that&#8217;s all that is required.  Running the script will get it to connect to the irc server and join the channel, to test the bot you can either type to message in the mains on in private by send the command.<br />
blabot, yahoo developer</p>
<p>it will cause the bot to return the top five search results for the term &#8216;yahoo developer&#8217;. You can download the script <a href="http://blog.rajatpandit.com/wp-content/uploads/2008/02/rbot.pl" title="IRC Search Bot">here</a>.</p>
<p>A couple of useful links:<br />
<a href="http://search.cpan.org/dist/Bot-BasicBot/lib/Bot/BasicBot.pm">http://search.cpan.org/dist/Bot-BasicBot/lib/Bot/BasicBot.pm</a><br />
<a href="http://www.ddj.com/web-development/184416221">Writing an IRC Bot in Perl</a></p>
<div id="in_post_ad_bottom_1" style="clear:both;margin:0;padding:0;"><div class="brp-bp-234">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-4254382394977039";
/* brp-234x60-bp */
google_ad_slot = "7787511801";
google_ad_width = 234;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<div class="brp-bp-234">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-4254382394977039";
/* brp-234x60-BP-1 */
google_ad_slot = "9111022353";
google_ad_width = 234;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div></div><div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://blog.rajatpandit.com/2008/02/25/writing-an-irc-bot/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

