By: Kevin T. Duraj, Los Angeles, California

Object Oriented Perl Tutorials


#!/usr/bin/perl

package Person;

sub new
{
my $class = shift;
my $self = {
_firstName => shift,
_lastName => shift,
_ssn => shift,
};
# Print all the values just for clarification.
print "First Name is $self->{_firstName}\n";
print "Last Name is $self->{_lastName}\n";
print "SSN is $self->{_ssn}\n";
bless $self, $class;
return $self;
}
sub setFirstName {
my ( $self, $firstName ) = @_;
$self->{_firstName} = $firstName if defined($firstName);
return $self->{_firstName};
}

sub getFirstName {
my( $self ) = @_;
return $self->{_firstName};
}
1;



#!/usr/bin/perl

package Employee;
use Person;
use strict;
our @ISA = qw(Person); # inherits from Person

# Override constructor
sub new {
my ($class) = @_;

# Call the constructor of the parent class, Person.
my $self = $class->SUPER::new( $_[1], $_[2], $_[3] );
# Add few more attributes
$self->{_id} = undef;
$self->{_title} = undef;
bless $self, $class;
return $self;
}

# Override helper function
sub getFirstName {
my( $self ) = @_;
# This is child class function.
print "This is child class helper function\n";
return $self->{_firstName};
}

# Add more methods
sub setLastName{
my ( $self, $lastName ) = @_;
$self->{_lastName} = $lastName if defined($lastName);
return $self->{_lastName};
}

sub getLastName {
my( $self ) = @_;
return $self->{_lastName};
}

1;



#!/usr/bin/perl

use Employee;

$object = new Employee( "Mohammad", "Saleem", 23234345);
# Get first name which is set using constructor.
$firstName = $object->getFirstName();

print "Before Setting First Name is : $firstName\n";

# Now Set first name using helper function.
$object->setFirstName( "Mohd." );

# Now get first name set by helper function.
$firstName = $object->getFirstName();
print "After Setting First Name is : $firstName\n";

This will produce following result
First Name is Mohammad
Last Name is Saleem
SSN is 23234345
This is child class helper function
Before Setting First Name is : Mohammad
This is child class helper function
After Setting First Name is : Mohd.

Reference:
http://www.tutorialspoint.com/perl/perl_oo_perl.htm

Perl Support IDE


$ cd perl-support.zip ~/.vim
$ unzip /usr/src/perl-support.zip
$ vim ~/.vimrc
filetype plugin on


$ vim ~/.vim/perl-support/templates/Templates
|AUTHOR| = Kevin Duraj
|AUTHORREF| = pacific-design.com
|EMAIL| = president@whitehouse.com
|COMPANY| = pacific-design.com

Reference:

main.pl

#!/usr/bin/perl
use strict;
use warnings;
use My::Multirun; 

for (1..3)
{
   print "Running $_ times\n";
   run();
}

My/Multirun.pm

package My::Multirun;
use strict;
use warnings;
use base qw( Exporter );
our @EXPORT = qw( run ); 

our $counter; 

sub run
{
    $counter = 0;
    for(1..10) { increment_counter(); }
} 

sub increment_counter
{
    $counter++;
    print "Counter is equal to $counter !\n";
}
1 ;

Reference: http://perl.apache.org/docs/general/perl_reference/perl_reference.html

Search recursively through Bash and Perl source code

.bashrc

#-----------------------------------------------------------------#
# Search recursively through code omitting svn and comments.
#-----------------------------------------------------------------#
function fs()
{
find . -exec grep -il "$1" {} \; -print | grep -0v .svn \
| xargs grep -i "$1" | grep -v "\#" | sed -e "s/:.*//g" | sort -u
}
#-----------------------------------------------------------------#
# Search recursively through code comments only omitting svn.
#-----------------------------------------------------------------#
function fsc()
{
find . -exec grep -il "$1" {} \; -print | grep -0v .svn \
| xargs grep -i "$1" | grep "\#" | sed -e "s/:.*//g" | sort -u
}
#-----------------------------------------------------------------#

ssh login with rsa pub key

  1. ssh-keygen -t rsa
  2. cat ~/.ssh/id_rsa.pub | ssh REMOTE_SERVER ‘cat – >> ~/.ssh/authorized_keys’
  3. ssh REMOTE_SERVER ‘chmod 700 .ssh’
  4. ssh REMOTE_SERVER

JSON Data Structure Unit Test

$ prove -v –merge 000-json-test.t

#!/usr/bin/perl
use strict;
use warnings;

use Test::More;
use Test::Deep;
use JSON;

json_scalar_test();

done_testing();

sub json_scalar_test {
    my $rec = {
        title => 'JSON Unit Test',
        task  => 'Data Structure',
        langs => [ 'C/C++', 'Perl', 'Java', 'Python' ],
    };

    my $json_text = JSON->new->utf8->encode($rec);
    print "JSON=" . $json_text . "\n";

    open( FILE, ">/tmp/some_data.json" ) or die "$!";
    print FILE $json_text;
    close(FILE);

    my $text = `cat /tmp/some_data.json`;
    my $read = decode_json($text);
    print "title="    . $read->{title}    . "\n";
    print "task="     . $read->{task}     . "\n";
    print "langs[0]=" . $read->{langs}[0] . "\n";
    print "langs[1]=" . $read->{langs}[1] . "\n";

    cmp_deeply( $rec, $read );

}

Eclipse Wicked Shell Plugin

Wicked Shell is an Eclipse plugin providing direct access to your system’s shell. To be honest, Wicked Shell is not evil at all, although it calls itself wicked…

Wicked Shell is a small plugin trying to support your daily work with the Eclipse IDE. Although it is called Wicked Shell, it’s not a real shell itself. Wicked Shell is an eclipse plugin which functions as mediator between a Runtime process and a SWT Text widget. That means the output of the Runtime process is transferred to the widget and the text typed in the widget is transferred to the process as input. So Wicked Shell is not an actual shell but only uses an existing shell implementation.

Reference: http://www.wickedshell.net/

Eclipse C++ MySQL Project

#include <iostream>
#include <mysql.h>
using namespace std;

MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int query_state;

int main()
{
  mysql_init(&mysql);
  connection = mysql_real_connect(&mysql,"localhost","user","password","database",0,0,0);
  if (connection == NULL) { cout << mysql_error(&mysql) << endl; }

  query_state = mysql_query(connection, "select fname, lname from users limit 10;");
  if (query_state !=0) { cout << mysql_error(connection) << endl; }

  result = mysql_store_result(connection);
  while ( ( row = mysql_fetch_row(result)) != NULL )
  {
     cout << row[0] << " " << row[1] << endl;
  }

  mysql_free_result(result);
  mysql_close(connection);

  return 1;
}

Mac OS X Source Code Libraries

http://www.opensource.apple.com/release/mac-os-x-1062/