CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
PERL cgi script

 



mykolg
User

Jan 3, 2011, 12:21 PM

Post #1 of 24 (3322 views)
PERL cgi script Can't Post

I'm trying to execute my script from a HTML form submit button and when it runs my script it just shows up my code in the browser. I'm thinking I have a permissions issue, but not sure what it might be. I have given execute permissions and read permissions.

Any ideas?

Here is what my script looks like. I used CYGWIN to build my code.


Code
#!/usr/bin/perl -w 
use strict;

my ($minute, $hour, $day_in_month, $month, $year, $timestamp,);

my @month_names = qw /January February March April May June July August
September October November December/;

($minute, $hour, $day_in_month, $month, $year) = (localtime) [1..5];
$year += 1900;

$timestamp = "The local time on this server is $hour:$minute on
$month_names[$month] $day_in_month, $year";

print "Content-type: text/html\n\n";
print "<html>";
print "<head>";
print "<title>The State-Journal</title>";
print "</head>";
print "<body bgcolor=#000000>";
print "<font color=#FFFFFF>";
print "$timestamp";
print "</body>";
print "</html>";



(This post was edited by mykolg on Jan 3, 2011, 1:15 PM)


FishMonger
Veteran / Moderator

Jan 3, 2011, 3:31 PM

Post #2 of 24 (3316 views)
Re: [mykolg] PERL cgi script [In reply to] Can't Post

In what directory is the script located?

If the source code of the script is being shown, then that means the web server isn't configured to execute scripts in that directory.

If you're using the apache web server the default location for the scripts is the cgi-bin directory, not the html directory.


mykolg
User

Jan 3, 2011, 6:12 PM

Post #3 of 24 (3311 views)
Re: [FishMonger] PERL cgi script [In reply to] Can't Post

In the same directory as the HTML file

C:\cygwin\home\mykol\*.*


shawnhcorey
Enthusiast


Jan 4, 2011, 6:35 AM

Post #4 of 24 (3293 views)
Re: [mykolg] PERL cgi script [In reply to] Can't Post

You may want to change your shebang line. Some web servers ignore the Registry and used the shebang. Change it to the exact path on your machine.

Example:

Code
#!C:\PERL\BIN\PERL.EXE


Also, don't use the -w switch in the shebang. Use

Code
use warnings;

instead. Sometimes, it's necessary to turn off warnings and you can't do that if you have the -w switch.

Code
{ 
no warnings; # warnings temporarily turned off
# more code
}
# warnings back on


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


mykolg
User

Jan 5, 2011, 12:49 PM

Post #5 of 24 (3283 views)
Re: [shawnhcorey] PERL cgi script [In reply to] Can't Post

Would that be the location if I'm using cygwin?

Because it's located in C:\cygwin, therefore what should I put there in order to properly execute the cgi script?

I have changed my code below, It's still not executing my perl when I open a browser and link to it.

Also I was wondering if there was a way to use perl variables to alter the date when using wget. Instead of it being a set day like 20110105 Maybe somehow get it to use $year$month$day_in_month. Any ideas?

Code
#!C:\cygwin\bin\perl.exe 
use warnings;
use strict;
use LWP::Simple;

my ($file, $minute, $hour, $day_in_month, $month, $year, $timestamp);

my @month_names = qw /January February March April May June July August
September October November December/;

($minute, $hour, $day_in_month, $month, $year) = (localtime) [1..5];
$year += 1900;

$month = sprintf("%02d", $month);
$day_in_month = sprintf("%02d", $day_in_month);

$timestamp = "The local time on this server is $hour:$minute on
$month_names[$month] $day_in_month, $year";


#my $url = 'http://www.state-journal.com/images/sj_media';
#my $date = $year$month$day_in_month;
#http://www.state-journal.com/images/sj_media/20110103/pdf/A01'

system ('rm *.pdf');

system ('wget "http://state-journal.com/images/sj_media/20110105/pdf/A01.pdf"');
system ('wget "http://state-journal.com/images/sj_media/20110105/pdf/A02.pdf"');
system ('wget "http://state-journal.com/images/sj_media/20110105/pdf/A03.pdf"');
system ('wget "http://state-journal.com/images/sj_media/20110105/pdf/A04.pdf"');
system ('wget "http://state-journal.com/images/sj_media/20110105/pdf/A05.pdf"');
system ('wget "http://state-journal.com/images/sj_media/20110105/pdf/A06.pdf"');
system ('wget "http://state-journal.com/images/sj_media/20110105/pdf/A07.pdf"');
system ('wget "http://state-journal.com/images/sj_media/20110105/pdf/A08.pdf"');
system ('wget "http://state-journal.com/images/sj_media/20110105/pdf/A09.pdf"');
system ('wget "http://state-journal.com/images/sj_media/20110105/pdf/A10.pdf"');
system ('wget "http://state-journal.com/images/sj_media/20110105/pdf/B01.pdf"');
system ('wget "http://state-journal.com/images/sj_media/20110105/pdf/B02.pdf"');
system ('wget "http://state-journal.com/images/sj_media/20110105/pdf/B03.pdf"');
system ('wget "http://state-journal.com/images/sj_media/20110105/pdf/B04.pdf"');
system ('wget "http://state-journal.com/images/sj_media/20110105/pdf/B05.pdf"');
system ('wget "http://state-journal.com/images/sj_media/20110105/pdf/B06.pdf"');
system ('wget "http://state-journal.com/images/sj_media/20110105/pdf/B07.pdf"');
system ('wget "http://state-journal.com/images/sj_media/20110105/pdf/B08.pdf"');
system ('wget "http://state-journal.com/images/sj_media/20110105/pdf/B09.pdf"');
system ('wget "http://state-journal.com/images/sj_media/20110105/pdf/B10.pdf"');

system ('pdftk *.pdf cat output Today.pdf');

print "Content-type: text/html\n\n";
print "<html>";
print "<head>";
print "<title>The State-Journal</title>";
print "</head>";
print "<body bgcolor=#000000>";
print "<font color=#FFFFFF>";
print "$timestamp";
print "</body>";
print "</html>";



(This post was edited by mykolg on Jan 5, 2011, 2:27 PM)


FishMonger
Veteran / Moderator

Jan 5, 2011, 3:56 PM

Post #6 of 24 (3272 views)
Re: [mykolg] PERL cgi script [In reply to] Can't Post

Did you move the script to the proper directory?

Is the script working now, or is it still just showing the source code of the script?

Why are you doing all of those system calls to wget instead of using the getstore fuction from the LWP::Simple module, which you already have loaded?

It would be cleaner and IMO better to download the files in a loop.

If you use strftime function from the POSIX module, you could simplify your timestamp calculation down to 1 simple line.

Instead of using a system call to rm, you could use Perl's unlink function.


mykolg
User

Jan 5, 2011, 4:10 PM

Post #7 of 24 (3268 views)
Re: [FishMonger] PERL cgi script [In reply to] Can't Post

The script is in C:\cygwin\home\MyKoL\SJscript.cgi

The script runs when I put it in the shell and call perl to run it, but when loading it from FireFox it just shows my code.

I wasn't exactly sure how to use the LWP::Simple module because i can look up the files I have to pull each one directly.

I wanted to do a loop but I also what to have the date position of the URL to change with the current day and I'm having trouble trying to see how to do so.

After I figure that then I figure I could try a loop to run through each of the files.


FishMonger
Veteran / Moderator

Jan 5, 2011, 4:28 PM

Post #8 of 24 (3267 views)
Re: [mykolg] PERL cgi script [In reply to] Can't Post

Which web server are you using?


FishMonger
Veteran / Moderator

Jan 5, 2011, 4:39 PM

Post #9 of 24 (3265 views)
Re: [mykolg] PERL cgi script [In reply to] Can't Post


Code
use warnings; 
use strict;
use POSIX;
use LWP::Simple;

my $today = strftime("%Y%m%d", localtime);

for my $i ( '01'..'10' ) {
getstore("http://state-journal.com/images/sj_media/$today/pdf/A$i.pdf", "A$i.pdf");
getstore("http://state-journal.com/images/sj_media/$today/pdf/B$i.pdf", "B$i.pdf");
}



mykolg
User

Jan 5, 2011, 4:40 PM

Post #10 of 24 (3264 views)
Re: [FishMonger] PERL cgi script [In reply to] Can't Post

I'm running the html file out of the same directory. So basically it is on my computer.

I just really want to run the page off my computer so that I can create my own homepage to be loaded when I open my browser.

As for the current day item, is that code going to keep the day and month in two digit format or should I use the code that I had used to convert it and reassign it to the variable?


(This post was edited by mykolg on Jan 5, 2011, 4:42 PM)


FishMonger
Veteran / Moderator

Jan 5, 2011, 4:50 PM

Post #11 of 24 (3261 views)
Re: [mykolg] PERL cgi script [In reply to] Can't Post

You still haven't answered my question. Which web server are you using, IIS, apache, or something else?

If you're not running a web server, then you can't expect to run cgi scripts via your browser.


mykolg
User

Jan 5, 2011, 4:56 PM

Post #12 of 24 (3258 views)
Re: [FishMonger] PERL cgi script [In reply to] Can't Post


Code
MyKoL@MyKoL-HP ~ 
$ perl SJscript.cgi
Not enough arguments for LWP::Simple::getstore at SJscript.cgi line 11, near ""h
ttp://state-journal.com/images/sj_media/$today/pdf/A$i.pdf")"
Not enough arguments for LWP::Simple::getstore at SJscript.cgi line 12, near ""h
ttp://state-journal.com/images/sj_media/$today/pdf/B$i.pdf")"
Execution of SJscript.cgi aborted due to compilation errors.




Hmmm... should I therefore add apache to the cygwin package?


FishMonger
Veteran / Moderator

Jan 5, 2011, 5:01 PM

Post #13 of 24 (3257 views)
Re: [mykolg] PERL cgi script [In reply to] Can't Post

Copy/paste the script I posted, then test it.



Quote
should I therefore add apache to the cygwin package?


If you want to run cgi script, then YES.


(This post was edited by FishMonger on Jan 5, 2011, 5:02 PM)


mykolg
User

Jan 5, 2011, 5:03 PM

Post #14 of 24 (3254 views)
Re: [FishMonger] PERL cgi script [In reply to] Can't Post

I added apache and then tested your script, in my prior post I have shown the results. Seems to error out with the getstore command.


FishMonger
Veteran / Moderator

Jan 5, 2011, 5:06 PM

Post #15 of 24 (3252 views)
Re: [mykolg] PERL cgi script [In reply to] Can't Post

The error clearly shows that you didn't run the script I posted.


mykolg
User

Jan 5, 2011, 5:11 PM

Post #16 of 24 (3250 views)
Re: [FishMonger] PERL cgi script [In reply to] Can't Post

This is the code I used


Code
#!C:\cygwin\bin\perl.exe 
use warnings;
use strict;
use LWP::Simple;

my $today = strftime("%Y%m%d", localtime);

system ('rm *.pdf');

for my $i ('01'..'10'){
getstore("http://state-journal.com/images/sj_media/$today/pdf/A$i.pdf");
getstore("http://state-journal.com/images/sj_media/$today/pdf/B$i.pdf");
}

system ('pdftk *.pdf cat output Today.pdf');

print "Content-type: text/html\n\n";
print "<html>";
print "<head>";
print "<title>The State-Journal</title>";
print "</head>";
print "<body bgcolor=#000000>";
print "<font color=#FFFFFF>";
print "Testing";
print "</body>";
print "</html>";


And here is the result

Code
MyKoL@MyKoL-HP ~ 
$ perl SJscript.cgi
Not enough arguments for LWP::Simple::getstore at SJscript.cgi line 11, near ""h
ttp://state-journal.com/images/sj_media/$today/pdf/A$i.pdf")"
Not enough arguments for LWP::Simple::getstore at SJscript.cgi line 12, near ""h
ttp://state-journal.com/images/sj_media/$today/pdf/B$i.pdf")"
Execution of SJscript.cgi aborted due to compilation errors.

MyKoL@MyKoL-HP ~
$



FishMonger
Veteran / Moderator

Jan 5, 2011, 5:20 PM

Post #17 of 24 (3249 views)
Re: [mykolg] PERL cgi script [In reply to] Can't Post

If you think those getstore calls are the same as what I gave you, then you must think $1 is equal to $10 and if so, please send me all of your $10 bills and I'll give you a $1 bill for each and then you can see if you end up with the same amount of money.


mykolg
User

Jan 5, 2011, 5:36 PM

Post #18 of 24 (3243 views)
Re: [FishMonger] PERL cgi script [In reply to] Can't Post

Ok, I corrected the errors.

I guess my next question is about my first line of code. I'm still not able to get my program to show the HTML. it's still just showing straight code.


FishMonger
Veteran / Moderator

Jan 5, 2011, 5:46 PM

Post #19 of 24 (3242 views)
Re: [mykolg] PERL cgi script [In reply to] Can't Post

Did you move the script to the directory that is configured in apache for running cgi scripts? If not, then there's no reason to expect it to work.


mykolg
User

Jan 5, 2011, 5:49 PM

Post #20 of 24 (3240 views)
Re: [FishMonger] PERL cgi script [In reply to] Can't Post

Ok, well I added the apache to cygwin, just not sure where to put my script if so, also couldn't I just change my first line of code to it's location?

Sorry, normally I use a putty program to login to my account at school. So I'm kinda learning the cygwin on my own.

Any ideas?

Below is what I changed my code to, trying to adjust for the certain day of the week since the paper isn't up till after 4pm my time and there is no paper on Saturdays. It didn't like my first bit where I tried to get the individual parts of the current time, though I did that part right, but I'm not sure what I'm doing wrong.


Code
#!c:/cygwin/bin/perl.exe 
use warnings;
use strict;
use POSIX;
use LWP::Simple;


($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

my $today = strftime("%Y%m%d", localtime);
my $yesterday = strftime("%Y(%m-01)%d", localtime);
my $dayB4yesterday = strftime("%Y(%m-02)%d", localtime);

system ('rm *.pdf');

if ($wday == 6){
for my $i ('01'..'10'){
getstore("http://state-journal.com/images/sj_media/$yesterday/pdf/A$i.pdf", "A$i.pdf");
getstore("http://state-journal.com/images/sj_media/$yesterday/pdf/B$i.pdf", "B$i.pdf");
}
}

elsif ($wday != 6){
if ($hour >= 16){
for my $i ('01'..'10'){
getstore("http://state-journal.com/images/sj_media/$today/pdf/A$i.pdf", "A$i.pdf");
getstore("http://state-journal.com/images/sj_media/$today/pdf/B$i.pdf", "B$i.pdf");
}
}

elsif ($hour < 16){
if ($wday == 7){
for my $i ('01'..'10'){
getstore("http://state-journal.com/images/sj_media/$dayB4yesterday/pdf/A$i.pdf", "A$i.pdf");
getstore("http://state-journal.com/images/sj_media/$dayB4yesterday/pdf/B$i.pdf", "B$i.pdf");
}
}

elsif ($wday != 7){
for my $i ('01'..'10'){
getstore("http://state-journal.com/images/sj_media/$yesterday/pdf/A$i.pdf", "A$i.pdf");
getstore("http://state-journal.com/images/sj_media/$yesterday/pdf/B$i.pdf", "B$i.pdf");
}
}
}
}


system ('pdftk *.pdf cat output Today.pdf');
system ('rm *.pdf');

print "Content-type: text/html\n\n";
print "<html>";
print "<head>";
print "<title>The State-Journal</title>";
print "</head>";
print "<body bgcolor=#000000>";
print "<font color=#FFFFFF>";
print "Testing";
print "</body>";
print "</html>";



(This post was edited by mykolg on Jan 5, 2011, 11:26 PM)


Karazam
User

Jan 6, 2011, 2:36 AM

Post #21 of 24 (3211 views)
Re: [mykolg] PERL cgi script [In reply to] Can't Post

In Apache, CGI scripts must reside in the directory called 'cgi-bin'.


mykolg
User

Jan 6, 2011, 9:57 AM

Post #22 of 24 (3202 views)
Re: [Karazam] PERL cgi script [In reply to] Can't Post

I ran a search for apache in my cygwin directory and I don't seem to see it in a cgi-bin folder. Am I looking for apache.exe? Because if so it's not in my cygwin folder.


Karazam
User

Jan 6, 2011, 12:02 PM

Post #23 of 24 (3192 views)
Re: [mykolg] PERL cgi script [In reply to] Can't Post

Try to run this in a shell:

Code
find / -name cgi-bin



mykolg
User

Jan 6, 2011, 2:32 PM

Post #24 of 24 (3179 views)
Re: [Karazam] PERL cgi script [In reply to] Can't Post


Code
MyKoL@MyKoL-HP ~ 
$ find / -name cgi-bin
/srv/www/cgi-bin
/usr/share/apache2/cgi-bin
/var/www/cgi-bin


and it still seems to be searching, it hasn't finished the command yet, but it's been hung up there for about 10 minutes.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives