Archive

Archive for February, 2011

Perl study note: why using references in functions is the preferred way (mostly)

February 26, 2011 Leave a comment

Using references in Perl has a big advantage over the traditional passing-value way in performance. The reason is simple – no copying is performed when parameters are passed as references to functions. Below is the code I used to test on my Acer AOD 255 netbook:

#!/usr/bin/perl
use Time::Local;
use POSIX qw/strftime/;

sub d {
	my $msg=shift;
	print strftime("[%m/%d/%Y %H:%M:%S] ", localtime(time)).$msg."\n";
}

sub trim_ref {
	my $ref=shift;
	$$ref =~ s/(^\s+|\s+$)//g;
}

sub trim {
	my $str=shift;
	$str =~ s/(^\s+|\s+$)//g;
	return $str;
}

print "Purpose of this program is to illustrate the advantage of using
references in perl functions. Please note the traditional way might take a while
to finish.\n";


d('running traditional way');

my $str="   this   ";
my $dest;
for my $i (1..1_000_000) {
	$dest=trim($str);
}
d('traditional way done');

my $str2="   that   ";
d('using reference method');
for my $j (1..1_000_000) {
	trim_ref(\$str2);
}
d('ref. done');


Result:
...
[02/26/2011 18:37:43] running traditional way
[02/26/2011 18:37:59] traditional way done
[02/26/2011 18:37:59] using reference method
[02/26/2011 18:38:06] ref. done

The reference way (7 seconds) clearly beat the value-passed way (16 seconds). But this conclusion doesn’t mean passing by reference is always the best way especially when modifying the arguments passed is not desired.

Simple logging (to screen) in Perl

February 26, 2011 Leave a comment

Very Simple but useful, esp. for simple scripts:

#!/usr/bin/perl
use Time::Local;
use POSIX qw/strftime/;

sub d {
	my $msg=shift;
	print strftime("[%m/%d/%Y %H:%M:%S] ", localtime(time)).$msg."\n";
}

print "this is my first program written in  perl!\n";
d('some test message');

Categories: perl, Programming

Oracle: auto numbering result (1-based)

February 18, 2011 Leave a comment

This is like adding an auto-incremented column to a query result:

select result.*, rownum from ( select … ) result;

select … is the sub query that doesn’t include the row number. This method is esp. useful when the sub query uses union.

Categories: oracle, sql

Oracle: a quick way to get number of rows using sqlplus

February 18, 2011 Leave a comment

There are times when we need to do some quick test to see if the select query that we build returns reasonable results, here’s a quick solution, type the following into the sql console:
select count(*) from ( select… )

The select… is the query being tested. This is useful when the number of rows returned by select… is huge.

Categories: oracle, sql

A few useful BASH shortcuts

February 17, 2011 Leave a comment

Here are some of the shortcuts that I use quite often:

$_ ( $_ is the last argument of the last command )
Example 1:
ls /a/very/deep/and/not/easy/to/find/folder/
(once confirm that’s the folder I want, I might want to go there, so I use the following command, right after the ls command)
cd $_
(it’ll take me right to the folder /a/very/deep/and/not/easy/to/find/folder/)

Example 2:
vi ~/work/html/todaysnote.html
cp $_ ~/public_html/pub

(In the above example, $_ is actually ~/work/html/todaysnote.html)

cd – (dash)
when working between two different pwd’s, cd – makes toggling between current and last visited folders a lot easier

cd (without arguments)
will change to ~ (home folder)

empty a file from command line
cat /dev/null > somefile
(
note this command will be equivalent to the following if somefile doesn’t already exist:
touch somefile
)
Ref: http://lifehacker.com/#!270790/empty-a-text-file-from-the-command-line

Categories: Bash, Tip

Perl study note 2: using unpack() to parse formatted data

February 17, 2011 Leave a comment

Example code: formatting 10-digit us phone numbers
#!/usr/bin/perl
# just for the purpose of illustrating the usage of unpack() function
# of course there are other ways such as regex, substring to get the
# same result
use strict;
use warnings;

my @numbers=(
'2223334444',
'2223456789',
'2228889999',
);

for my $n (@numbers) {
my ($area, $phone1, $phone2)=unpack('a3a3a4', $n);
print "$n => ($area)$phone1-$phone2\n";
}

Result:
2223334444 => (222)333-4444
2223456789 => (222)345-6789
2228889999 => (222)888-9999

This might not be the best solution to get this kind of task done but definitely it’s neater than using the substring method.

Categories: perl, Programming

Perl DBI atomic fetch

February 17, 2011 Leave a comment

When getting a single row of data from a database table is desired, using atomic fetch makes life a lot easier, for example (assuming $dbh connection is established)
Example 1:

my ($name, $country) = $dbh->selectrow_array( "SELECT name, country FROM tbl_customer" );
print "Customer $name is from $country.\n";

Example 2:

my ($cnt) = $dbh->selectrow_array( "select count(*) from tbl_customer");
print "There are $cnt record(s).\n";

Reference:
http://docstore.mik.ua/orelly/linux/dbi/ch05_06.htm

Categories: perl, Programming