All posts by WhiteWind

About WhiteWind

I'm a Perl-PHP Developer from India. I'm a great fan of Wordpress & Perl.

How to compress a file in Perl?

To compress a file in Perl, it’s better to use the gzip method. For that we can use the module use IO::Compress::Gzip qw(gzip $GzipError) ;.

Eg:-

use IO::Compress::Gzip qw(gzip $GzipError) ;
my $input="new.pdf";
my $output="new1.pdf.gz";
gzip $input => $output or die "gzip failed: $GzipError\n";

in_array Function in Perl

One of the strengths of PHP when it comes to arrays is considered to be the in_array function (http://php.net/in_array), this function takes two parameters; the array and the item to be searched in the array and then returns a boolean result whether the item exists in the array. Perl inherently lacks this functionality, even popular Perl modules like List::Util lack such a function/method.

As it was said by Plato once, “Necessity… the mother of invention.”, I required to check whether a name existed in an array, bored by using conventional method of iterating the array every time I need to check, I wrote a simple sub-routine to get the job done.

This sub-routine take two arguments, a reference to the array to be searched, and the item that needs to be searched. Simple enough the sub-routine works like magic, even my friend have found it very useful. I thought may be someone might find it useful, here’s it, take a look.

sub in_array
 {
     my ($arr,$search_for) = @_;
     my %items = map {$_ => 1} @$arr; # create a hash out of the array values
     return (exists($items{$search_for}))?1:0;
 }

Sample Usage

my @arr = ('Manu','Pradeep','Shency','Shabbir');

 sub in_array
 {
     my ($arr,$search_for) = @_;
     my %items = map {$_ => 1} @$arr;
     return (exists($items{$search_for}))?1:0;
 }

 if(in_array(\@arr,'Manu '))
 {
     print "Hurray! Manu you are in!!\n";
 }
 else
 {
     print "Ooops! Amlan you are out!!\n";
 }

Output :

 Hurray! Manu you are in!!

How to generate excel in Perl?

We can generate an excel using the module Spreadsheet::WriteExcel.

Ex:-

#!/usr/bin/perl -w
use CGI;
use DBI;
use Data::Dumper;
use Spreadsheet::WriteExcel;
use vars qw(%GLOBALS %GLOB $dbh $q);
$q = new CGI;

my $time=time();
# Create a new workbook called simple.xls and add a worksheet.
my $workbook  = Spreadsheet::WriteExcel->new("$time.xls");
#$format_bold_ul->set_bg_color('gray');
my $worksheet = $workbook->add_worksheet();
for(my $i=0;$i<10;$i++)
{
     my %font    = (
        font  => 'Arial',
        size  => 12,
        color => 'blue',
        bold  => 1,
        align => 'center',
        valign => 'vcenter'
    );
    $format_bold_ul = $workbook->add_format(%font);
    for(my $j=0;$j<6;$j++)
    {
         $worksheet->write($i, $j, qq{$i - $j}, $format_bold_ul);
    }
}

For more details go to http://search.cpan.org/search?query=Spreadsheet%3A%3AWriteExcel&mode=module

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>