Page 1 of 1

CGI translation into php

Posted: Thu Jul 03, 2008 8:09 am
by theighost
please i need some help here: i am suppose to translate this text!

i will take it step by step.

this is the text:

when the first "if" starts the command is &js; ...what does this mean in this language...and the other if has &generate;





#!/usr/bin/perl
use CGI;
use CGI::Carp qw(fatalsToBrowser);

$q= new CGI;
$action = $q->param('action');

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime time;
$yearnow = $year-100;
$yearnow = 2000 + $yearnow;
$mon=$mon+1;
$curdate = "$yearnow\-0$mon";

if($action eq "js"){
&js;
}
elsif($action eq "generate"){
&generate;
}
else{&list;}

############
sub list{
############
print $q->header;
print qq~<ul>~;
opendir(DIR, "$curdate")|| die("Can't read $curdate");
@dirlist = readdir(DIR);
closedir(DIR);

$curtime = time;
$curtime = $curtime - 86400;
foreach $diritem(@dirlist){
next if($diritem eq "." || $diritem eq ".." || -d $diritem);
($filesize, $filedate, $fileperm) = (stat("$curdate/$diritem"))[7,9,2];
next if($filedate < $curtime);
$filename=$diritem;
$filename =~ s/\_/ /ig;
print qq~<li><a href="$curdate/$diritem">$filename</a>~;
}
print qq~</ul><br>~;
}

############
sub js{
############
print $q->header;
print qq~document.write('<ul>');~;
opendir(DIR, "$curdate")|| die("Can't read $curdate");
@dirlist = readdir(DIR);
closedir(DIR);

$curtime = time;
$curtime = $curtime - 86400;
foreach $diritem(@dirlist){
next if($diritem eq "." || $diritem eq ".." || -d $diritem);
($filesize, $filedate, $fileperm) = (stat("$curdate/$diritem"))[7,9,2];
next if($filedate < $curtime);
$filename=$diritem;
$filename =~ s/\_/ /ig;
$filename =~ s/\.html//ig;
print qq~document.write('<li><a href="/vidgen/$curdate/$diritem">$filename</a>');~;
}
print qq~document.write('</ul><br>');~;
}

###################
sub generate {
###################
opendir(DIR, "$curdate")|| die("Can't read $curdate");
@dirlist = readdir(DIR);
closedir(DIR);

$curtime = time;
$curtime = $curtime - 86400;

open(FILE, ">vidlist.html");
foreach $diritem(@dirlist){
next if($diritem eq "." || $diritem eq ".." || -d $diritem);
($filesize, $filedate, $fileperm) = (stat("$curdate/$diritem"))[7,9,2];
next if($filedate < $curtime);
$filename=$diritem;
$filename =~ s/\_/ /ig;
$filename =~ s/\.html//ig;
print FILE qq~<li><a href="/vidgen/$curdate/$diritem">$filename</a>~;
}
close(FILE);
print $q->header;
print qq~List file generated~;
exit;
}

Re: CGI translation into php

Posted: Tue Jul 08, 2008 12:33 pm
by Ollie Saunders
Do you know Perl? Because I don't. But I do know PHP :-). If you can tell me what &js does I can tell you what the PHP equivalent is.

Also place surround your code in [syntax=perl]code here[/syntax] so that it's nice to read thanks :)

Re: CGI translation into php

Posted: Tue Jul 15, 2008 5:18 am
by alex.barylski
In Perl you prefix your sub-routine invocation with the '&' character. Stupid syntax and totally foriegn to most programmers, but Perl is wonky that way. To make matters worse I am pretty sure it's optional.

Code: Select all

&callSubroutine
I don't think there is a return statement either, I believe it's whatever expression is evaluated last is what is returned. It's been so long since I used Perl though I couldn't tell you. Good thing too, cause I hated that language with a passion and I will forever hate regular expressions because of that language. :P