Tag Archives: Perl Scripts

How to get array length in perl?

It’s as follows :

 

#!/uer/bin/perl

@array = (1,2,3);
$array[50] = 4;

$size = @array;
$max_index = $#array;

print "Size:  $size\n";
print "Max Index: $max_index\n";

O/P

Size: 51
Max Index: 50

How to submit a html form using javascript or JS?

It can be done as follows :

document.forms["Form_Name"].submit();

How to stop exit an exicuting loop while it meets an inner condition?

We use last for exiting from an execution loop.

It’s as follows :

for(my $m=0;$m<=$country_tot;$m++) {
    if($country_id[$m]==$row_detail->{country}) {
         $cntry=$country_name[$m];
         last;
    }
}

Get the column names from table in database(DB)

To get the table column name from the database, we should use the following SQL query:

SHOW COLUMNS FROM table_name;

To get the values in perl we can use the following code:

my $sql_rep_x = 'SHOW COLUMNS FROM '.$table;
my $getkey_x = $dbh->prepare($sql_rep_x);
$getkey_x->execute;
while( my @row_x = $getkey_x->fetchrow_array ) {
    push @names, $row_x[0];
}
$getkey_x->finish;

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;