Monday, December 28, 2009

Perl regex, substitution, file reading/writing

Learning by doing is the best motto. With this motto when I was exploring perl I wrote few scripts to extract and use parts of learning materials on perl from the web.

Just because there are a lot of resource out there on the web doesn't mean they are going to help you be on a fast track of learning perl. There are a lot of redundant materials which we don't want to go over, often times we don't know how to cut to the chase.

I used to go to this site often: http://perldoc.perl.org

There are a lot of materials out there. In this article I would like to explore about pattern matching and substitution, reading from file and substituting certain parts of the lines in the file and writing the desired result to another file.

The program below builds the very fundamental concepts about:

1. Opening file.(a webpage from http://perldoc.perl.org)
2. Reading from the file line by line
3. Substituting into the line by getting rid of some interesting materials.
4. Writing to the file and to the console.

If you open http://perldoc.perl.org/perlrequick.html, you will see that the page tutors on pattern matching. It is a good material but personally I am only interested on things like:

1. "Hello World" =~ /World/; # matches

2. /[^a]at/; # doesn't match 'aat' or 'at', but matches # all other 'bat', 'cat, '0at', '%at', etc.

3. /[^0-9]/; # matches a non-numeric character

4. /[a^]at/; # matches 'aat' or '^at'; here '^' is ordinary

Looking at above line it is so easy to quickly build the idea without having to read all of the explanation.

So, as I continue to believe in "learning by doing", I wrote a couple of scripts to extract only info I wanted from such tutorial.


--The following script reads the input text file we created by coping/pasting from the url: http://perldoc.perl.org/perlrequick.html

--looks for only those lines which have a pattern as in: "1. blah or 1. blah etc"

--writes the matches lines from file input into a separate file and to the console.

*to successfully run the program you will have to select the page at http://perldoc.perl.org/perlrequick.html
and copy/paste into a text file (in my case the text file is called: perl_regex.txt located at my personal directory
on my mac which is "Users/my_name/perlscripts/"

#########################################################

#!/usr/bin/perl –w

$data_file = "/Users/my_name/perlscripts/perl_regex.txt"; #replace this line with location/name of your file

$out_file = "out_file.txt";

open(FHAND, $data_file) or die $!;

open(OUTHAND, ">out_file.txt") || die $!;

while(){
if($_ =~/^(\s.*[1-9]|[1-9])\./){
print "$_\n";
print OUTHAND "$_\n";
}
}

close(FHAND);
close(OUTHAND);

#########################################################


If you can successfully run the above you will be at a great relief(at least I was) when you see the simple to read concepts about pattern matching.

The out put looks something like

#########################################################

1. print "It matches\n" if "Hello World" =~ /World/;

1. print "It doesn't match\n" if "Hello World" !~ /World/;

1. $greeting = "World";

2. print "It matches\n" if "Hello World" =~ /$greeting/;

1. $_ = "Hello World";

2. print "It matches\n" if /World/;

1. "Hello World" =~ m!World!; # matches, delimited by '!'

2. "Hello World" =~ m{World}; # matches, note the matching '{}'

3. "/usr/bin/perl" =~ m"/perl"; # matches after '/usr/bin',

4. # '/' becomes an ordinary char

1. "Hello World" =~ /world/; # doesn't match, case sensitive

#########################################################

Still, it is a little unreadable if you look at the numbering and spacing. Those dotted numbers don't make sense. So?
Guess what we, we write another script to get rid of those as in the following:



To read the earlier output file, replace the numbering and space and to write the extracted text into a new file:

#########################################################
#!/usr/bin/perl -w

$read = "/users/nirmalksingh/perlscripts/out_file.txt";

open(READHANDLE, $read) || die $!;
open(WRITEHANDLE, ">out_file2.txt") || die $!;

while(){
# $_ =~ s/^([1-9]/.)\s.*/\s/g;
$_ =~ s/^([1-9]\.)\s+//g;
print WRITEHANDLE $_;
print "$_\n";
}

close READHANDLE;
close WRITEHANDLE;

#########################################################

Now, in essence, the output file/console looks more or less like as follows:

#########################################################

'cathouse' =~ /cat$foo/; # matches

'housecat' =~ /${foo}cat/; # matches

"housekeeper" =~ /keeper/; # matches

"housekeeper" =~ /^keeper/; # doesn't match

"housekeeper" =~ /keeper$/; # matches

"housekeeper\n" =~ /keeper$/; # matches

"housekeeper" =~ /^housekeeper$/; # matches

/cat/; # matches 'cat'

#########################################################

That's all.

As I succeeded along, I modified the input further on and on doing other substitutions and writing them to file/console accordingly.

It can get really really addictive because with perl it is always DWIS (do what i say) and that's very satisfying for programmers.

Thanks for visiting and reading, please feel free to write to me directly if you have any question/s. I will be glad to answer as you wish.

~nirmal

Friday, December 25, 2009

Perl man

One of the best ways to learn programming tools and languages which fall under *nix domain is using "MAN PAGES".
The following is a list of key words to explore using the "perldoc" command in your shell.

Usually you can enter keywords for most of the perl related manual pages followed by "perldoc".
For eg. if you enter as in below:

################################

blah-blah$perldoc perlsyn

################################

it will output a buffer of text about perl syntax as in below:

################################

PERLSYN(1) User Contributed Perl Documentation PERLSYN(1)



NAME
perlsyn − Perl syntax

DESCRIPTION
A Perl program consists of a sequence of declarations and statements
which run from the top to the bottom. Loops, subroutines and other
control structures allow you to jump around within the code.

Perl is a free‐form language, you can format and indent it however you
like. Whitespace mostly serves to separate tokens, unlike languages
like Python where it is an important part of the syntax.

Many of Perl’s syntactic elements are optional. Rather than requiring
you to put parentheses around every function call and declare every
variable, you can often leave such explicit elements off and Perl will
figure out what you meant. This is known as Do What I Mean,
abbreviated DWIM. It allows programmers to be lazy and to code in a
style with which they are comfortable.

Perl borrows syntax and concepts from many languages: awk, sed, C,
Bourne Shell, Smalltalk, Lisp and even English. Other languages have
borrowed syntax from Perl, particularly its regular expression
extensions. So if you have programmed in another language you will see
familiar pieces in Perl. They often work the same, but see perltrap
for information about how they differ.

Declarations

The only things you need to declare in Perl are report formats and
subroutines (and sometimes not even subroutines). A variable holds the
undefined value ("undef") until it has been assigned a defined value,
which is anything other than "undef". When used as a number, "undef"
is treated as 0; when used as a string, it is treated as the empty
string, ""; and when used as a reference that isn’t being assigned to,
it is treated as an error. If you enable warnings, you’ll be notified...

################################

just like perlsyn below are other important keywords which you can use to put yourself on fast track of
learning and using perl...

perlpod -perlpod − the Plain Old Documentation format

perlpodspec − Plain Old Documentation: format specification and notes

perldata − Perl data types

perlop − Perl operators and precedence

perlunicode − Unicode support in Perl

perluniintro − Perl Unicode introduction

perlref − Perl references and nested data structures

perlfunc − Perl builtin functions

perlsub − Perl subroutines

perlmod − Perl modules (packages and symbol tables)

As you can see above, many of the keywords for pulling the man pages are intuitive. It can be hard to memorize all of the keywords(i don't know if they are termed as keyword, I learnt perl on my own:) )
So doing a little guesswork to spell the keyword can be a faster way to quickly pull up related man pages when you need to get some info on a perl construct that you intend to use in your program.

For eg. if you want to get to manual pages which has examples on "opening" things in perl then you could do construct a key word like this: perl open tut (for perl opne tutorial) and could enter with perldoc as in:

####################

blah-blah$ perldoc perlopentut

####################

Another example can be: perl object or perl obj as in perlobject or perlobj.
Trying each of your guessworks as in above will cater to a faster learning process, talking from my personal experience, I can say, I am sure :)


perlmodlib − constructing new Perl modules and finding existing ones

Few good example of using acronyms in your guess work is "perlboot" (boot=beginner's object oriented tutorial)

perlboot − Beginner’s Object−Oriented Tutorial

perltoot − Tom’s object−oriented tutorial for perl

perltooc − Tom’s OO Tutorial for Class Data in Perl


Some of my favorite keywords for getting info on perl regular expression:

perlre − Perl regular expressions

perlretut − Perl regular expressions tutorial

perlrequick − Perl regular expressions quick start


Some more keywords:

perlpacktut − tutorial on "pack" and "unpack"

PerlIO − On demand loader for PerlIO layers and root of PerlIO::* name
space

perlform − Perl formats

perltie − how to hide an object class in a simple variable

perlipc − Perl interprocess communication (signals, fifos, pipes, safe
subprocesses, sockets, and semaphores)

perlvar − Perl predefined variables

perlthrtut − tutorial on threads in Perl

perlcc − generate executables from Perl programs

perlcompile − Introduction to the Perl Compiler−Translator

perldebug − Perl debugging

perldebtut − Perl debugging tutorial

I will be composing more and more stuff on perl in future.
Thanks for reading from my page.

~nirmal

Monday, July 20, 2009

iTunes Store Continuous Play

Hola Amigos e Amigas :)

The following is an AppleScript utility for making the iTunes Store continuous play. Every now and then when we want to check out some new songs, we go to the iTunes Store on iTunes and we find that it is tormenting because we have to be hitting next song every 30 seconds.

This script is a solution for that problem. It has its limitations though. First of all, it will need to be run for each list(i will enhance it and repost when I can).

To get it work for you: copy and paste this script into "Script Editor".
1. Locate the "Script Editor" first by pressing "Command + Space" and typing "Script Editor"

2. Save the script as "iTunes Store Continuous.scpt"

3. Bring up the list you want to have continuous play on itunes store.

4. Toggle back to the script editor and hit "Play" button. (get used to the function by hitting Stop/Play a twice or thrice, just to see example of how it is working)

very important:

5. No need to install the script.

6. Always stop the script in the "script editor" by hitting the red "Stop" button before shutting down your computer or exiting itunes.

Enjoy!

~nirmal

repeat
tell application "iTunes"
next track
playpause
end tell
delay 32
end repeat

Monday, July 13, 2009

sh devLaunchYOUR_APPLICATION_NAME.sh

Voila!!

Finally recalled that invoke the shell command from putty when you are in some server machine is always: sh devLaunch_name_of_your_application.sh

I kept trying to just do:

[blah..blah...date..etc][qualifying_name/blah/blah/my_work_server_name]>devLaunchNAME_OF_APPLICATION.sh

and it won't work. I can't believe I took one hour to finally recall that it is always "sh" then devLaunch_NAME_OF_APPLICATION.sh

So all of you folks if you got here because of this is what you were missing, my pleasure and you are most welcome.

~nirmal