Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image rotation script 1

Status
Not open for further replies.

xmassey

Programmer
Apr 9, 2007
62
GB
I have created a script designed to change an image depending on what hour it is (i.e. their are 24 hours in a day therefore I have 24 images, 1 for each hour). The syntax seems okay however I am recieving an error 500. Any ideas?


#! /usr/bin/perl
use strict;
use CGI ':standard';

##################################################################################################################################################
#######################
## GMT time and date ##
#######################
my ($gmttime, $hour2);

my ($sec, $min, $hour, $mday, $mon, $year)=gmtime;
$gmttime = sprintf ('%02d/%02d/%4d <b>(time:</b> %02d:%02d:%02d<b>)</b>', $mday, $mon+1, $year+1900, $hour+1, $min, $sec);

$hour2 = $hour+1;
##################################################################################################################################################

##################################################################################################################################################
##################################
## Conditions of image rotation ##
##################################
my ($counter);

while ($counter < 24) {

$counter = 0;

if ($hour2 eq $counter) {

print "Content-type: text/html\n\n";
print <<"HTML code";

<HTML><HEAD>
<TITLE>Image Rotation</TITLE>
</HEAD><BODY>
<p><img border="0" src="Images/$counter.bmp" width="100" height="100"><br>
<font size="2" face="Arial">$gmttime</font></p>
</BODY></HTML>

HTML code
print "";
}

$counter = $counter + 1;
}

##################################################################################################################################################
#########
## End ##
#########
 
Didn't we just do this??
$hour2 eq $counter
should be
$hour2 == $counter

#! /usr/bin/perl
should be
#!/usr/bin/perl

Also you can do
$counter++;
to increase counter by 1.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
eq is the same as == (it makes no difference, but ill try it anyway just in case it works in this instance)

my server uses #! /usr/bin/perl (with a space, otherwise there is an error 500)

And thanks, I saw there was a way to decrease the count using --$counter and I assumed it would be ++$counter, but I didn't try
 
$counter-- subtracts.
eq and == are not the same thing.

Code:
$x = "01";
$y = "1";

if ($x == $y) {
	print "Test1 Equals\n";
}
else {
	print "Test1 Doesn't Equal\n";
}


if ($x eq $y) {
	print "Test2 Equals\n";
}
else {
	print "Test2 Doesn't Equal\n";
}
Output:
Test1 Equals
Test2 Doesn't Equal


You are getting 500 errors now.. so how does fixing the #! line make you get more 500 errors?



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Have you tried running my script on your server after changing the code to how you think it should look? If so did it work?

I recieive error 500 if the syntax is incorrect, though the syntax is fine I still get an error 500.

I'm just running script through a CGI output monitor to see if it outputs fine, but im getting a network error message now :s
 
No idea what could be causing a 500 error other than what travs69 pointed out. One suggestion that I can make though is to always run your cgi scripts from the command line first and see if they at least compile. Secondly, make sure that their permissions are correct (ie they're executable).

Also, try to keep your code no wider than 78 cols. This is just a common standard, so the large ###### blocks should be cut down. Here's your code reformatted and simplified.

Code:
[gray]#! /usr/bin/perl[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]CGI[/green] [red]'[/red][purple]:standard[/purple][red]'[/red][red];[/red]
[black][b]use[/b][/black] [green]POSIX[/green] [red]qw([/red][purple]strftime[/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[gray][i]##########################[/i][/gray]
[gray][i]# GMT time and date [/i][/gray]
[gray][i]##########################[/i][/gray]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@today[/blue] = [url=http://perldoc.perl.org/functions/gmtime.html][black][b]gmtime[/b][/black][/url][red];[/red]
[black][b]my[/b][/black] [blue]$gmttime[/blue] = strftime [red]"[/red][purple]%d/%m/%Y <b>(time:</b> %H:%M:%S<b>)</b>[/purple][red]"[/red], [blue]@today[/blue][red];[/red]
[black][b]my[/b][/black] [blue]$hour[/blue] = strftime [red]"[/red][purple]%H[/purple][red]"[/red], [blue]@today[/blue][red];[/red]
[black][b]my[/b][/black] [blue]$hour2[/blue] = [red]([/red][blue]$hour[/blue] + [fuchsia]1[/fuchsia][red])[/red] [blue]%[/blue] [fuchsia]24[/fuchsia][red];[/red]

[gray][i]##########################[/i][/gray]
[gray][i]# Conditions of image rotation [/i][/gray]
[gray][i]##########################[/i][/gray]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Content-type: text/html[purple][b]\n[/b][/purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[black][b]print[/b][/black] [red]<<"END_HTML"[/red][red];[/red]
[purple]<HTML><HEAD>[/purple]
[purple]<TITLE>Image Rotation</TITLE>[/purple]
[purple]</HEAD><BODY>[/purple]
[purple]<p><img border="0" src="Images/[blue]$hour2[/blue].bmp" width="100" height="100"><br>[/purple]
[purple]<font size="2" face="Arial">[blue]$gmttime[/blue]</font></p>[/purple]
[purple]</BODY></HTML>[/purple]

[red]END_HTML[/red]

[fuchsia]1[/fuchsia][red];[/red]

[teal]__END__[/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]CGI - Simple Common Gateway Interface Class[/li]
[li]POSIX - Perl interface to IEEE Std 1003.1[/li]
[/ul]
[/tt]

Good luck learning,
- Miller
 
Miller, thanks. I have created another script similar to the one you have posted and it works fine now. Though i'm interested in why my other script wasn't working, so i'm still trying the ideas trevor had because im confused :s
 
I just realised... My script read...

my ($counter);

while ($counter < 24) {

$counter = 0;



Therefore each time the "while loop" looped round, $counter was returned to 0 therefore because there was no "else" statement, the script produced nothing. I have now put the $counter variable above and outside of the while loop
 
You are correct.

However, the counter was never needed in first place. Since all you're doing is testing for equivalence to $hour2, you could just use $hour2 instead of this algorithmically constructed variable. Hence why there is no $counter in the version I created.

- M
 
eq is the same as == (it makes no difference, but ill try it anyway just in case it works in this instance)

If they are the same, and it maks no difference, why would you try it just in case?

The simple fact is they are not the same. You can often get away with using 'eq' in place of '==' but if you continue to think they do the same thing you will eventually believe perl is full of bugs when your code does not work how you think it should because you are using the wrong operator.

The point is, use math operators when dealing with numbers, use string operators when dealing with strings. [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hey Kevin,

Once you had given me the example of using both == and eq and showing that it won't work, I understand now why I cannot always use eq. However for all the situations I have used eq/== for this wouldn't be an issue. But as you said one day I will realise that it has an importance when I need it
 
I don't know how you have your images numbered, 0-23 or 1-24, but here is a way that uses no variables or modules:

Code:
[gray]#! /usr/bin/perl[/gray]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]qq{[/red][purple]Content-type: text/html[/purple]

[purple]<HTML><HEAD>[/purple]
[purple]<TITLE>Image Rotation</TITLE>[/purple]
[purple]</HEAD><BODY>[/purple]
[purple]<p><img border="0" src="Images/@{[(split(/:/,(split(/\s+/,scalar localtime(gmtime)))[3]))[0]]}.gif"[/purple]
[purple]width="100" height="100"><br>[/purple]
[purple]<font size="2" face="Arial">$gmttime</font></p>[/purple]
[purple]</BODY>[/purple]
[purple]</HTML>[/purple]
[purple][/purple][red]}[/red][red];[/red]



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin++

Very amusing code. I wouldn't claim no variables though, as no "named" variables would be closer to the truth. Also, there are two bugs.

There's this $gmttime variable that you missed.

And more subtle is the "scalar localtime(gmtime)", which should actually be just "scalar gmtime". Or if you want to simplify the code a lot, just use "(gmtime)[2]". However even that isn't right as he actually wanted one hour greater than the current hour. So that leaves us with:

Code:
[gray]#! /usr/bin/perl[/gray]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]<<"END_OF_PAGE"[/red][red];[/red]
[purple]Content-type: text/html[/purple]

[purple]<HTML><HEAD>[/purple]
[purple]<TITLE>Image Rotation</TITLE>[/purple]
[purple]</HEAD><BODY>[/purple]
[purple]<p><img border="0" src="Images/[blue]@[/blue]{[ (1 + (gmtime)[2]) % 24 ]}.gif"[/purple]
[purple]width="100" height="100"><br>[/purple]
[purple]<font size="2" face="Arial">[blue]$gmttime[/blue]</font></p>[/purple]
[purple]</BODY>[/purple]
[purple]</HTML>[/purple]
[red]END_OF_PAGE[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
[/tt]

Which still breaks because $gmttime isn't set. But I'm not going to bother playing with that right now :)

- Miller

 
hehehe... I missed that $gmttime, but this is precious:

src="Images/@{[ (1 + (gmtime)[2]) % 24 ]}.gif"

nicely scaled down [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top