Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

If/else problems

by Anonymous Monk
on Nov 18, 2003 at 23:12 UTC ( [id://308174]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Wisdom is sought from the monks for a beginner. I have installed various cgi/perl packages but I have decided to write what I thought would be a relatively easy script - oh how naive for one so old! I am trying to determine output based on the month, the codse I have been working with is as follows:-
#!/usr/bin/perl #$myname="Merlin"; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime (tim +e + ($timediff * 3600)); @months = ('01','02','03','04','05','06','07','08','09','10','11', +'12'); $month = $months[$mon]; print "Content-type: text/html\n\n"; if ($month==11){ undef $/; open (FILE, 'hours.txt'); print <FILE>; close FILE; }
If i exclude the if statement and the relevant { or } the script displays the contents of hours.txt but as soon as I use the if statement the 500 Internal Server Error is displayed.

I have searched the monastary and from the examples I have found I thought I had got the syntax correct. Perhaps I have looked at this for too long and can't see the obvious errors. Hopefully someone can spot my problem and offer a solution and set me on the path to enlightenment.

Many thanks for any help that may be offered

Julian

Edit by castaway, fixed closing code tag, added some p tags.

Replies are listed 'Best First'.
Re: If/else problems
by JamesNC (Chaplain) on Nov 18, 2003 at 23:36 UTC
    Your code is a good example of why "use strict;" is a good thing. I would recommend you adding it to all of your code, it will help you debug it. Here is your code fixed... see if you can see the differences:
    #!/usr/bin/perl use strict; #$myname="Merlin"; my $DEBUG = 0; # my added here my $timediff = 0; # ? my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime ( +time + ($timediff * 3600)); print "$mon $hour, $mday, $mon, $year, $wday\n" if $DEBUG == 1; my @months = ('01','02','03','04','05','06','07','08','09','10','11',' +12'); my $month = $months[$mon]; print "Content-type: text/html\n\n"; if ($month eq '11'){ # use 'eq' to compare strings Nov eq '10' btw undef $/; open (FILE, 'hours.txt'); print <FILE>; close FILE; }

    1) Comment your code
    2) Place debug print statements in, or learn the debugger
    3) use strict;
    4) use warnings; until you know when you don't want it
Re: If/else problems
by castaway (Parson) on Nov 18, 2003 at 23:32 UTC
    Some questions:

    1. Is that $timediff variable actually set to anything anywhere?
    2. Have you checked the value in $month, $mon? Try printing it
    3. Try adding 'use warnings;' to the top of your script, which will probably tell you something useful.
    4. Look in the server logs to see what the error was, or run the script on the command line.

    My guess would be that the $month variable is still unset (cset to undef) when you reach the if line, which is why it is complaining..

    C.

Re: If/else problems
by Roger (Parson) on Nov 18, 2003 at 23:49 UTC
    The other monks have made suggestions to add 'use warnings', add 'use strict' to your script.

    Just want to point out that you can have a look at your Apache error.log to see what might be the cause of the error. And also you can also insert a few print statements to check your $month variable, and run your perl script on the command line. If it doesn't run on the command line, it will not run on the Web server.

    The following is a more Perl-ish way to retrieve the month.
    #!/usr/bin/perl -w use strict; my $timediff = 1; # 1 hour in the future my $month = (localtime(time+$timediff*3600))[4] # month + 1; # we need to add 1 to month print "Content-type: text/html\n\n"; if ($month==11) { local $/; open (FILE, 'hours.txt'); print <FILE>; close FILE; }
Re: If/else problems
by !1 (Hermit) on Nov 18, 2003 at 23:37 UTC

    Hmm. That's odd. It works for me. At first I thought it might be a problem of uploading the file as binary instead of ascii from a windows machine. But then you noted that if you remove the if, everything works fine. Of course, I don't know if that's local or on the server. My suggestion is pretty much to look at the server's error logs. Nothing pops out beyond $timediff not being declined (which shouldn't affect the script's operation) and your declaration of some possibly unneeded variables. In any case, I hope this helps you with your problem.

Re: If/else problems
by injunjoel (Priest) on Nov 19, 2003 at 01:02 UTC
    Greetings all,
    This may be a silly question, but have you checked the permission on the file you are attempting to execute. Also you may need to adjust the owner.group ownership definitions if you are running on Linux.
    Your file must be executable in order to run (chmod 755 if needed).
    Also your web server (Hopefully Apache) must be configured to execute CGI scripts.
    Though these may be obvious questions to some, they are not implicitly obvious to all.
    Hope that helps
    -injunjoel

      Another quick note: The text file may also need permissions tweaked. This can be deceptive when run from the command line -- you're running the script as your primary user, but the Apache (or whatever your webserver is) may run as user 'nobody' or 'www'.

Re: If/else problems
by Paulster2 (Priest) on Nov 19, 2003 at 03:15 UTC

    As a veteran (the hard way) of using Date::Manip, I can't stress it's use enough in manipulating the date-time continuum (sp). Although, this doesn't answer your question, it will allow you to clean up your code. Do a perldoc on that mod and check it out. I think that you will find it's uses very cool.

    Paulster2

Re: If/else problems
by tfrayner (Curate) on Nov 19, 2003 at 12:21 UTC
    Another (very minor) stylistic point: you can use sprintf to good effect and avoid having to set up a temporary array.
    use strict; my $timediff=0; # set it to whatever # just get the month my $month = (localtime(time + ($timediff * 3600)))[4]; # move 0..11 month range into line with human convention $month = sprintf("%02d", ++$month); print ("$month\n");
    Not to worry though, if you're comfortable with your method (TIMTOWTDI, after all).

    Tim

    Update: simplified to better make the point.

Re: If/else problems
by bschmer (Friar) on Nov 19, 2003 at 14:42 UTC
    While you don't tell us much about your environment, you can typically check the server logs to find any error messages that your script printed. I've done this many times and tend to keep a "tail -f /var/log/httpd/error_log" going while I'm developing my script. Makes debugging quick and easy.
Re: If/else problems
by Anonymous Monk on Nov 19, 2003 at 08:01 UTC
    Thank you for your help and the fixed code from JamesNC works fine - I'm not sure I understand all the differences yet but I intend to find out and understand. A couple of other comments - I had tried printing the $month variable and it displayed ok - I even tried incrementing it's value ($month++) and printing that value and that also worked. The 500 problem arose as soon as I used the if statement and that's what I couldn't understand and at the moment I don't really understand wht the code fix does work but once I have added a couple of other bits to the code I will be coming back to this to try and understand why things happened or not as the case may be. Thanks again for your shared wisdom it has bee very much appreciated Julian

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://308174]
Approved by castaway
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-25 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found