Category Archives: Uncategorized

How to make download a file in perl?

It can be done as :

my $file_size=-s "folder/Excel_12345.xls" ;
	print $q->header(
		-type=>"application/vnd.ms-excel",     #MIME type
		-attachment=> "folder/Excel_12345.xls",
		-Content_Length=>"$file_size"          #File size
		);
		open (INFILE,"folder/Excel_12345.xls" ) or die("FAILED TO OPEN Excel FILE"); 
		binmode(INFILE);
		while (<INFILE>){
			print $_;
		}
		close(INFILE);          
		unlink ("folder/Excel_12345.xls" ); #delete file from server
		exit;

It’s important that it should be written before printing the header. ie., print $q->header();

Javascript to check the maxlength.

Since IE 8 doesn’t support the attribute maxlength, we need to use java script to workout this functionality. We can write the script as follows.

<textarea id="clm_add_lin_dta" maxlength="80" name="clm_add_lin_dta" style="resize:none;" onchange="testLength(this,80)" onkeyup="testLength(this,80)" onpaste="testLength(this,80)"></textarea>

<script>
   function testLength(ta,maxLength ) {
        if (ta.value.length > maxLength) {
            ta.value = ta.value.substring(0, maxLength);
        }
    }
</script>

How to get values from the URL using javascript?

When we are in need to get values from the URL using java script as we get them using $_REQUEST[‘name’]; in PHP, $q->param(‘name’); in Perl etc. we can use the following function to get the values in java script :

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
         vars[key] = value;
    });
    return vars;
}

To get the values we should call the above function as follows :

var val1=getUrlVars()["name1"];
alert(val1);

If the URL is http://ww.example.com/page.php?name1=value1&name2=value2&name3=value3, then the

var val1=getUrlVars()["name1"]; //value1
var val2=getUrlVars()["name2"]; //value2
var val2=getUrlVars()["name2"]; //value3

How to get the current URL using javascript?

The way to get the current URL using java script is as follows:

var thisUrl = window.location.href;
alert( thisUrl );

The output will be :

 http://www.example.com/page.php?name=value

How can we break out from a loop in perl?

It’s can be done as :-

my @a=(1,2,3,4,5,6,7,8,9,12);
my $b=3;
#print "ttt:-".$a[$b-2];
foreach my $c (@a)
{
    print $c."\n";
    if($c==$b)
    {
        last;
    }
}

The output will be as :

  1
 2
 3