Tuesday, 4 October 2011

Configuring Apace Server to run PERL scripts

Follow following steps to run PERL script on Apace Server


Step 1: 
Open <APACHE_HOME>/conf/httpd.conf file


httpd.conf - is the main configuration file for Apache HTTP Server


Step 2: 
Search for "ScriptAlias /cgi-bin/" uncomment i.e. remove '#' if commented. You may need to adjust the path to run your script successfully


ScriptAlias - Provides the location for CGI programs. This should be two parts, separated by whitespace. The first part is the part used in URL that triggers execution of a CGI program; the second part is the path where those CGI programs are stored in the real filesystem


Step 3:
Search for "AddHandler cgi-script" in the document, uncomment it, if committed and append ".pl " after ".cgi"


Step 4: 
Modify your PERL Script. If your script is Windows executable it does't means that it will run on Apache Server as well 'coz script relies on PERL interpreter, so we have to modify the hashbang or the shebang or  the first line of your script. Append perl.exe (For Windows only)


Step 5: 
The line after shebang line should define the MIME-type header.


print "Content-type: text/html; charset=iso-8859-1\n\n"; 


Now you are all set to run your first CGI program. Just open your browser and access http://localhost/cgi-bin/helloWorld.pl


NOTE: helloWorld.pl is the name of the PERL file I created it can be different in your case.


--Dev

Monday, 29 August 2011

Is Binary search tree - Java Program

 /** 
  * Following method can be used to test if a tree meets the conditions to be a binary search tree (BST). 
  */ 
  public boolean isBST(TreeNode root) {
      if (root == null)
          return false;
      return( isBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE) );
  }


  /** 
   * Recursive Method - which validates very sub tree of a given binary tree.
   * Works in O(n) time -- visits each node only once. 


   * @param min   For left sub tree it should be Integer.MIN_Value
   *                        For right sub tree it should be node.data + 1
   * @param max For left sub tree it should be node.data
   *                     For right sub tree it should be Integer.MAX_VALUE


   *
   */ 
  private boolean isBST2(TreeNode node, int min, int max) {
    if (node==null) {
      return(true);
    }
    else {
   
     if (node.getData() < min || node.getData() > max)
    return false;
     // left should be in range  min...node.data
      boolean leftOk = isBST(node.getLeft(), min, node.getData());


      // if the left is not ok, bail out
      if (!leftOk) return(false);


      // right should be in range node.data+1..max
      boolean rightOk = isBST(node.getRight(), node.getData()+1, max);


      return(rightOk);
    }
  }