<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Hathy</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/hathibelagal_mohammed/" />
    <link rel="self" type="application/atom+xml" href="http://blogs.perl.org/users/hathibelagal_mohammed/atom.xml" />
    <id>tag:blogs.perl.org,2009-11-03:/users/hathibelagal_mohammed//732</id>
    <updated>2011-03-20T08:00:44Z</updated>
    <subtitle>A blog about the Perl programming language</subtitle>
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type Pro 4.38</generator>

<entry>
    <title>Javascript style pseudo-classes in Perl</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/hathibelagal_mohammed/2011/03/simpler-javascript-style-pseudo-classes-in-perl.html" />
    <id>tag:blogs.perl.org,2011:/users/hathibelagal_mohammed//732.1577</id>

    <published>2011-03-19T20:17:33Z</published>
    <updated>2011-03-20T08:00:44Z</updated>

    <summary>I am as much a Javascript developer as a Perl developer, and that is primarily because of the striking similarities the languages share, one of the most useful of which is the concept of closures. Having private(hidden) data, and public...</summary>
    <author>
        <name>Hathibelagal</name>
        
    </author>
    
    <category term="inheritance" label="inheritance" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="objectoriented" label="object-oriented" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="pseudoclass" label="pseudo-class" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/hathibelagal_mohammed/">
        <![CDATA[<p>I am as much a Javascript developer as a Perl developer, and that is primarily because of the striking similarities the languages share, one of the most useful of which is the concept of closures.<br />
Having private(hidden) data, and public accessors/mutators has always been one of the key reasons people use the object oriented paradigm. Perl however, being the flexible language that it is, does not provide any direct way to hide the data in your objects. One of the solutions to this problem is <a href="http://www.stonehenge.com/merlyn/UnixReview/col63.html">inside-out objects</a> (and I think it is a very elegant solution too.)<br />
But, when I want to write lesser code, and don't really want to use all of Perl's OO functionality, I simply use closures.<br />
Here's how,</p>


<pre>
<code>
#!/usr/bin/perl
use strict;
use warnings;

sub Person{
        my $data={
                _NAME=&gt;$_[0] || &quot;Anon&quot;,
                _AGE=&gt;$_[1] || 0,
                _PHONE=&gt;$_[2] || &quot;001&quot;
        };
        # Now the following is an anonymous hash of anonymous methods.
        # It contains all the methods to access the above data hash.
        my $methods={
                name=&gt;sub{
                        $data-&gt;{_NAME}=shift if @_;
                        $data-&gt;{_NAME};
                },
                age=&gt;sub{
                        $data-&gt;{_AGE}=shift if @_;
                        $data-&gt;{_AGE};
                },
                phone=&gt;sub{
                        $data-&gt;{_PHONE}=shift if @_;
                        $data-&gt;{_PHONE};
                },
                say_hello=&gt;sub{
                        print &quot;Hello &quot;.$data-&gt;{_NAME},&quot;\n&quot;;
                }
        };
        # Now I make sure only the methods are available outside
        return $methods;
}

#Here is a sample of simple inheritance
sub Employee{
        my $methods=Person(@_);
        my $data={
                _COMPANY=&gt;$_[3] || &quot;Unemployed&quot;     #You can't use shift here
        };
        $methods-&gt;{company}=sub {
                $data-&gt;{_COMPANY}=shift if @_;
                $data-&gt;{_COMPANY};
        };
        #Now Employee pseudo-class has the extra company() accessor/mutator
        return $methods;
}

#Now I create the pseudo-objects
my $person=Person(&quot;Hathy&quot;,26,&quot;+919919&quot;);
$person-&gt;{say_hello}();
$person-&gt;{name}(&quot;Hathibelagal&quot;);
$person-&gt;{age}($person-&gt;{age}()+1);
print $person-&gt;{name}(),&quot; is &quot;,$person-&gt;{age}(),&quot; years old\n&quot;;

#This is another pseudo-object (With inheritance)
my $employee=Employee(&quot;Bela&quot;,25,&quot;+01119&quot;,&quot;Mozilla&quot;);
$employee-&gt;{say_hello}();       
print $employee-&gt;{name}(),&quot; works for &quot;,$employee-&gt;{company}();
#Works just the way it should
</code>
</pre>


<p>Yeah, I understand it is not a class, but you do get some class-like functionality with this style of coding, and very often the purpose of having a class is served.</p>]]>
        
    </content>
</entry>

<entry>
    <title>A very basic LISP style expression parser</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/hathibelagal_mohammed/2011/03/a-very-basic-lisp-style-expression-parser.html" />
    <id>tag:blogs.perl.org,2011:/users/hathibelagal_mohammed//732.1565</id>

    <published>2011-03-16T21:39:50Z</published>
    <updated>2011-03-16T21:49:20Z</updated>

    <summary>I had always been interested in writing a simple LISP interpreter. Well, had some free time today, and thought, why not create a parser that can return the results of mathematical expressions that are entered as LISP expressions. Nothing fancy,...</summary>
    <author>
        <name>Hathibelagal</name>
        
    </author>
    
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/hathibelagal_mohammed/">
        <![CDATA[<p>I had always been interested in writing a simple <span class="caps">LISP </span>interpreter. Well, had some free time today, and thought, why not create a parser that can return the results of mathematical expressions that are entered as <span class="caps">LISP </span>expressions. Nothing fancy, but here is a class that can take in a file containing math expressions and return the results of each line as an array.</p>



<pre><code>package Processor;

use strict;
use warnings;

#Creates the object, nothing else
sub new {
	my $class=shift;
	my $self={};
	bless $self, $class;
	$self;
}

#Set the file to be processed, or return the currently
#selected file
sub file{
	my $self=shift;
	my $filename=shift;
	$self-&gt;{filename}=$filename if defined $filename;
	$self-&gt;{filename};
}

#Process each line of the file as an expression, and load
#an array with the results
sub process_file{
	my $self=shift;
	return unless defined $self-&gt;{filename};
	my @results=();
	open FILE, $self-&gt;{filename} or die (&quot;Could not open file&quot;) ;
	while(&lt;FILE&gt;){
		push @results, ($self-&gt;process_line($_));
	}
	@results;
}

#Processes LISP style expressions that are valid
sub process_line{
	my $self=shift;
	my $line=shift;
	my @opstack=();
	my @operandStack=();
	for(split(&quot; &quot;,$line)){
		push @opstack,$_ if $_ eq &quot;(&quot; or 
				    $_ eq &quot;+&quot; or 
				    $_ eq &quot;-&quot; or 
				    $_ eq &quot;*&quot; or
				    $_ eq &quot;/&quot;;
		push @operandStack,$_ if /^\d+$/;
		if($_ eq &quot;)&quot;){
			my $op1=pop @operandStack;
			my $op2=pop @operandStack;
			my $op=pop @opstack;
			pop @opstack;
			if ($op eq &quot;+&quot;){
				push @operandStack,($op1+$op2);
			}elsif ($op eq &quot;-&quot;){
				push @operandStack,($op2-$op1);
			}elsif ($op eq &quot;*&quot;){
				push @operandStack,($op1*$op2);
			}elsif ($op eq &quot;/&quot;){
				push @operandStack,($op2/$op1);
			}
		}
	}
	return &quot;Invalid expression&quot; unless @operandStack==1;
	$operandStack[0];
}

1;</code></pre>



<p>I sure do hope to add variables to this, so that results can be used from one line of the file to another.</p>]]>
        
    </content>
</entry>

<entry>
    <title>A simple API to access Reddit</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/hathibelagal_mohammed/2011/02/a-simple-api-to-access-reddit.html" />
    <id>tag:blogs.perl.org,2011:/users/hathibelagal_mohammed//732.1496</id>

    <published>2011-02-23T12:24:06Z</published>
    <updated>2011-02-23T20:28:03Z</updated>

    <summary>I had always wanted to perform some analysis on all the posts that Reddit gets. I searched CPAN for a module that does something similar, and found nothing. So, I decided to write my own. This is still a work...</summary>
    <author>
        <name>Hathibelagal</name>
        
    </author>
    
    <category term="reddit" label="Reddit" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/hathibelagal_mohammed/">
        <![CDATA[<p>I had always wanted to perform some analysis on all the posts that Reddit gets. I searched <span class="caps">CPAN </span>for a module that does something similar, and found nothing. So, I decided to write my own. This is still a work in progress, and currently only allows you to fetch and parse the data in <span class="caps">XML.</span></p>]]>
        <![CDATA[<pre><code>package Reddit;

use strict;
use warnings;
use Carp;

use LWP::Simple;
use XML::Simple;

sub new {
	my $class=shift;
	my $self={
		version=&gt;&quot;2.0&quot;,
		format=&gt;&quot;xml&quot;
	};
	bless $self, $class;
	return $self;
}

sub format{
	my $self=shift;
	my $data=shift;
	if (defined $data) {
		$self-&gt;{format}=$data if $data eq &quot;xml&quot; or $data eq &quot;json&quot; or $data eq &quot;rss&quot;;
	}
	return $self-&gt;{format};
}

sub version{
	return $_[0]-&gt;{version};
}

sub search{
	my $self=shift;
	my $data=shift;
	croak &quot;REDDIT: Search must be called by an instance only&quot; if not defined ref $self;
	croak &quot;REDDIT: No inputs specified&quot; if not defined $data;
	my $url=&quot;http://www.reddit.com/search.&quot;.$self-&gt;{format}.&quot;?q=&quot;;
	$url.=$data-&gt;{query} if defined $data-&gt;{query};
	$url.=' author:'.$data-&gt;{author} if defined $data-&gt;{author};
	$url.=' over18:'.$data-&gt;{over18} if defined $data-&gt;{over18};
	$url.=' site:'.$data-&gt;{site} if defined $data-&gt;{site};
	$url.='&amp;sort='.$data-&gt;{orderby} if defined $data-&gt;{orderby};
	carp q(REDDIT: over18 must be yes|no) if defined $data-&gt;{over18} and $data-&gt;{over18} ne &quot;yes&quot; and $data-&gt;{over18} ne &quot;no&quot;;
	my $contents=get($url);
	if($self-&gt;{format} eq &quot;xml&quot;) {
		my $xml=XMLin($contents);
		return $xml;
	}else{
		return $contents;
	}
}

sub front_page{
	my $self=shift;
	my $data=shift;
	my $subreddit=&quot;&quot;;
	$subreddit=&quot;r/&quot;.$data-&gt;{subreddit} if defined $data-&gt;{subreddit};
	if($data-&gt;{type}){
		$subreddit.=&quot;/&quot;.$data-&gt;{type}.&quot;/&quot;;
		carp &quot;REDDIT: Type must be new/top/controversial only&quot; 
			if $data-&gt;{type} ne &quot;new&quot; and $data-&gt;{type} ne &quot;top&quot; and $data-&gt;{type} ne &quot;controversial&quot;;
	}
	my $url=&quot;http://www.reddit.com/&quot;.$subreddit.&quot;.&quot;.$self-&gt;{format};
	my $contents=get($url);
	if($self-&gt;{format} eq &quot;xml&quot;) {
		my $xml=XMLin($contents);
		return $xml;
	}else{
		return $contents;
	}
}

sub get_links_from_xml{
	my $self=shift;
	my $xml=shift;
	croak &quot;REDDIT: Provide an XML hash reference&quot; if not defined ref $xml;
	my $contents=$xml-&gt;{channel}-&gt;{item};
	croak &quot;REDDIT: No data in XML provided&quot; if (not defined $contents);
	my @return_value=();
	if (ref $contents eq &quot;ARRAY&quot;) {
		foreach(@$contents) {
			my $current_value={
				link=&gt;$_-&gt;{link},
				title=&gt;$_-&gt;{title}
			};
		push @return_value,$current_value;
		}
	}else{
		my $current_value={
			link=&gt;$xml-&gt;{channel}-&gt;{item}-&gt;{link},
			title=&gt;$xml-&gt;{channel}-&gt;{item}-&gt;{title}
		};
		push @return_value,$current_value;
	}
	return @return_value;
}

1;
</code></pre>



<p><b>Here is the usage:</b></p>



<pre><code>#!/usr/bin/perl
use lib &quot;.&quot;;

use Reddit;
use Data::Dumper;
my $reddit=Reddit-&gt;new;
my @links=$reddit-&gt;get_links_from_xml(
                        ($reddit-&gt;search(
                                {
                                 site=&gt;&quot;imgur.com&quot;,
                                 over18=&gt;&quot;no&quot;,
                                 query=&gt;&quot;cat&quot;
                                }
                        ))
                   );
foreach(@links){
        print $_-&gt;{title},&quot;\n&quot;;
}</code></pre>



<p>Easy! Isn't it? If you want the fully commented source code, you can find it at <a href="https://github.com/hathibelagal/Reddit-Perl-API">Github</a>.</p>]]>
    </content>
</entry>

</feed>
