All posts by WhiteWind

About WhiteWind

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

Downloading files in perl script

The files can be donloaded as follows

my $file_size=-s "report_generator/".$file;
print $q->header(
    -type=>"text/csv",
    -attachment=> "report_generator/$file",
    -Content_Length=>"$file_size"
);
open (INFILE,"report_generator/".$file) or die("FAILED TO OPEN CSV FILE");
binmode(INFILE);
binmode(STDOUT);
while (){
    print $_;
}
close(INFILE);
unlink ($file);
exit;

CSV generation in Perl Script

We can create csv files using perl script. It should be as follows.

my $table=$q->param('table');
my $field_name=defined($q->param('column'))?$q->param('column'):"*";
my $file_name="Report".time();
my $sql_rep = 'SELECT '.$field_name.' FROM '.$table;
$file = "$file_name.csv";
my $getkey = $dbh->prepare($sql_rep);
$getkey->execute;
my (@names, @values);
my $xi=0;
my $xii=0;
my @datas;
while( my @row = $getkey->fetchrow_array ) {
    $xii=0;
    foreach my $data (@row) {
        push @{$datas[$xi]}, $data;
        $xii++;
    }
    $xi++;
}
$getkey->finish;
open my $OUT, '>', "report_generator/".$file or die $!;
for(my $yi=0;$yi<$xi;$yi++) {
    @values=();
    for(my $yii=0;$yii<$xii;$yii++) {
        $datas[$yi][$yii] =~ s/"/""/g;
        push @values, qq{"$datas[$yi][$yii]"};
    }
    print {$OUT} (join ",", @values) . "\n";
}
close $OUT;

 

How to create multidimensional arrays in Perl?

To make an array of arrays, or more accurately an array of arrayrefs, try something like this:

my @array = ();
foreach my $i ( 0 .. 10 ) {
  foreach my $j ( 0 .. 10 ) {
    push @{ $array[$i] }, $j;
  }
}

It pushes the value onto a dereferenced arrayref for you. You should be able to access an entry like this:

print $array[3][2];

Zooming image with jquery

Check the following links to see how it work

To download it please click the below links

Progress bars for file uploads and avoiding temp files

CGI.pm gives you low-level access to file upload management through a file upload hook. You can use this feature to completely turn off the temp file storage of file uploads, or potentially write your own file upload progress meter.

This is much like the UPLOAD_HOOK facility available in Apache::Request, with the exception that the first argument to the callback is an Apache::Upload object, here it’s the remote filename.

$q = CGI->new(\&hook [,$data [,$use_tempfile]]);
sub hook {
my ($filename, $buffer, $bytes_read, $data) = @_;
print “Read $bytes_read bytes of $filename\n”;
}

The $data field is optional; it lets you pass configuration information (e.g. a database handle) to your hook callback.

The $use_tempfile field is a flag that lets you turn on and off CGI.pm‘s use of a temporary disk-based file during file upload. If you set this to a FALSE value (default true) then $q->param(‘uploaded_file’) will no longer work, and the only way to get at the uploaded data is via the hook you provide.

If using the function-oriented interface, call the CGI::upload_hook() method before calling param() or any other CGI functions:

CGI::upload_hook(\&hook [,$data [,$use_tempfile]]);

This method is not exported by default. You will have to import it explicitly if you wish to use it without the CGI:: prefix.