Playing with shift , @_ , $_

Posted in Perl on July 22, 2008 by vinaychilakamarri

The ‘Shift’ function pops the first element of an array and returns the popped item to the left side variable. There is a way to write cryptic code with “shift”, to impress your family (yep.. only family, sorry). Perl has some internal default variables that get initialized when in some sub routine. Lets say you’re passing an array of elements to some sub routine. You don’t need an explicit variable like in some languages to store that passed variable. You can directly use the implicit variables for getting the passed parameters. Let’s write a subroutine using some implicit variables and “shift”:

sub printFileContent {


printInsideThings ($_);

sub printInsideThings {
open FILE,$_;
while($line = <FILE>){
print $line;

printFileContent (“C:\\Data\\virtual-hosting-howto.html”,”C:\\Data\\virtual-boasting-howto.html”);

As I pass a list of files, the ‘@_’ of my first sub is already initialized with the list of strings that I passed.
So I used ‘foreach’ and passed the “$_” to another subroutine. The ‘$_’ will be initialized to the list item for
each iteration over the list. The other sub takes the string, and using a ‘$_’, which is initialized with the passed value by default, it opens the file and initializes a global variable ‘FILE’ and prints the content line by line in the while loop. The ‘< >’ opens the file and reads it line by line for you (uber cool tool eh?). Other ways we can do the later subroutine:

sub printInsideThings {
open FILE,shift;
print ;

Look at the while loop there. Perl uses the ‘$_’ so implicitly that you don’t even have to specify it. That loop initializes the ‘$_’ for every iteration and assigns it the line it reads from the file. The “print;” is equivalent to “print $_;” Again, the ‘shift’ in that file opener takes out the first of the ‘@_’ and passes it for parsing stuff

This one prints the contents of a single file that is passed:

sub openFu{
open FILE, shift;

openFu “c:\\data\\virtual-hosting-howto.html”;


Subtle things in perl

Posted in Perl on July 13, 2008 by vinaychilakamarri

There are some untold things in Perl, that really caught my attention. Firstly, we can assign an array to a scalar variable. Doing so will initialize that scalar to the size of that array. There is also an explicit function for this. Here is what I am talking about:

@arr = qw(i can be a bad guy);

$var = @arr;

print “$var”;

This will print 6. In the same way we can also use an inbuilt function for this. The function is called ‘scalar’

$var = scalar(@arr); assigns the size of the array. In the same way we can also initialize a variable to a list. But the variable will get initialized to the last variable of that list. For example:

$var = (1,2,3,4); will initialize $var to 4.

Pyramid in perl

Posted in Perl on July 6, 2008 by vinaychilakamarri

Lets say you want to print something like this:

Here is the code I wrote:

# !/usr/bin/perl
@array = (‘A’ .. ‘Z’);
$in = <>;
if($in < 0)
print “Can’t proceed”;
for($i = 0 ; $i < $in ; $i++)
@tmp = (”);
for($k = scalar(@array); $k > $i; $k–)
print ” “;
for($j = 0 ; $j < $i; $j++)
print “@array[$j]”;
@revtmp = reverse(@tmp);
{ print “$_” ;}
print “\n”;

This is a rough attempt at the problem. It can be:
1. Checked for input validity

2. Enhanced with what ever you want to ice on top of it.

Have fun!

Copying and deleting files in multiple directories with perl.

Posted in Perl with tags , , on July 1, 2008 by vinaychilakamarri

Now I came out with a simple program for doing this. I was faced with a situation where I had to copy a file in 20 different folders in order to test some code that I was writing. I also had to change data and copy the file again many times. I figured that this process would kill me. I wanted to automate this process so that i don’t have to struggle further. Initially I played with Batch scripting.. never liked it 😦 And all of sudden I remembered about my old time dabbling with perl.. now that flash of idea came in as a real perl from the sky!. Here is the code:

print “Which file to copy: “;
$fl = <>;
use File::Copy;
opendir DIR, “.”; # . is the current directory

while ( $filename = readdir(DIR) )
if(-d $filename && $filename ne ‘.’ && $filename ne ‘..’)
print “Copied $fl to: ” , $filename,”\n”;

Here I list the directories using ‘opendir’ on the current directory (here is where my 20 folders were). You could also take an input for this from the user. And for each item in the list returned, I copy the file in that directory with a ‘copy’ function. I filtered the “.”, “..” from the list for I don’t need to copy my files there. The “-d” checks for directory.

In the same manner, we can also delete files from the directories:

print “Which file to delete: “;
$fl = <>;
opendir DIR, “.”; # . is the current directory

while ( $filename = readdir(DIR) )
if(-d $filename && $filename ne ‘.’ && $filename ne ‘..’)
print “Deleted $fl in: ” , $filename,”\n”;

#observer the unlink function here. You have to specify the path of the file for each iteration. “\\” is a slash with an escape.

How to configure virtual hosts on Tomcat

Posted in Apache Tomcat with tags , , , on July 1, 2008 by vinaychilakamarri

With virtual hosts, you can serve different domain names on a single instance of Tomcat server. I will be describing the way to do this on this post.

To open a virtual host on Tomcat you need to do three things:

1. Create a appBase for each virtual host you will be adding
2. Edit the server.xml to add the virtual hosts
3. Edit the etc/hosts file to loopback on the virtual hosts

1. Creating an appBase is a very important step for our process. This is like creating multiple “webapps” directories for every virtual host that we have. Every appBase should contain a “ROOT” directory, which will contain all the stuff that we will be needing for providing on a virtual host. This can be servlets, html files, etc etc. Put anything you want to host, in that directory. So this will be the structure of your appBase directory after you complete the above step:

2. Server.xml tells the Tomcat server about the details of the virtual hosts. You can locate the server.xml file under conf directory of Tomcat. Find the following in the server.xml:

– <Engine name=”Catalina” defaultHost=”localhost”>

To this part, we should add our virtual hosts. Here is the way to do it:

– <Engine name=”Catalina” defaultHost=””>
<Host name=”” appBase=”vinayapps” />
<Host name=”” appBase=”chilakapps” />

Make sure that the defaultHost is one of the hosts that you defined in the Host tags. This will complete the configuration that is needed on the Tomcat, to run virtual hosts. But there is another important step that you need to perfom, to tell the browser (client) that it has to loop back to the local machine when you type in the URL that you just created. This is easy

3. In the final step, we should tell our client to connect to loop back to the localhost when we type in the virtual host name. This is a very easy step.

on Windows:
%root%:\WINDOWS\system32\drivers\etc and locate the “hosts” file in that. Edit the hosts file to something like this:
This will tell the machine that it has to loop back on the above domain. You can access the above domain only on the ports that you opened on the localhost. For example, the Tomcat comes with a default 8080 port connnector. To create multiiple connectors do this:
a. Open server.xml in your fav editor.
b. Find a tag like this on the xml file: <Connector port=”8080″ protocol=”HTTP/1.1″ connectionTimeout=”20000″ redirectPort=”8443″ />
c. Add another tag of that sort, now with your custom port. Ex: <Connector port=”666″ protocol=”HTTP/1.1″ connectionTimeout=”20000″ redirectPort=”8443″ />

Voila! you now have your virtual hosts on your favorite ports! Remember that opening a connector will expose all the appBases on the server.

Maximizing a JFrame by default when it comes up

Posted in Java with tags , , , , , on June 30, 2008 by vinaychilakamarri

I’ve been playing around with JFreeCharts for a while.  The API’s perfect; the only problem is with the amount of data with labels that I wanted to show… it didn’t just fit in the dictionary of aesthetics. Best way to delude my users was to show the stuff maximized. I wanted to show a JFrame maximized by default. Here is what I did:




Here I am assuming that you wrote a class by extending from JFrame itself. If this is not the case, you need to use the instantiated object to call the ‘getExtendedState’ method. Also use JFrame to call ‘MAXIMIZED_BOTH’:


JFrame fr = …



Have fun 😉