Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Seeking Feed back

by azatoth (Curate)
on Jul 05, 2001 at 12:34 UTC ( #94037=note: print w/ replies, xml ) Need Help??


in reply to Seeking Feedback on Chat Program (was Seeking Feed back)

For starters, all people new to Perl should be made aware of strict.pm. This is a Perl Module that ensures your program runs smoothly with regard to Scoping. You should also read up on use warnings; or the -w switch, which basically adds an extra level of integrity to your program.

Also on your filehandle call, you should always finish the statement off with or die "Could not open $fh : $!\n"; The die function enables error handling, storing the error value in the special variable $!. So your code should now look like

open CHAT, ">>../chatty2.html" or die "Could not open file : $!\n"; # your script will fail if you can't open the file, and print the reas +on why
In fact, you should use die on most function calls. Read up on that, it'll save you a lot of heartache in the future.

Finally, enjoy your time on Perlmonks. It's a very nice place.

Azatoth a.k.a Captain Whiplash

Make Your Die Messages Full of Wisdom!
Get YOUR PerlMonks Stagename here!
Want to speak like a Londoner?


Comment on Re: Seeking Feed back
Select or Download Code
Re: Re: Seeking Feed back
by orkysoft (Friar) on Jul 05, 2001 at 13:04 UTC
    Actually, in my 1200-line Mapster program (currently down), I used constructs like:
    if(open FILE, ">$filename") { flock FILE, 2; ... # Write stuff to FILE close FILE; } else { print "An I/O error occurred!"; log_error $!; # Call a sub }
    This allows my program to exit gracefully in the unlikely event of an error.

      what?  if () {} else {} ?

      what about:

      open FILE, ">$filename" or &exit_with_error ('An I/O error occured!', +$!);
      and in your &exit_with_error sub you do more stuff and exit

      just a thought


      He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

      Chady | http://chady.net/
      And so you have an error, you know that there is no such file or directory. What then? How does this help in debugging.

      As perlstyle says:

      o Always check the return codes of system calls. Good error messages should go to STDERR, include which program caused the problem, what the failed system call and arguments were, and (VERY IMPORTANT) should contain the standard system error message for what went wrong. Here's a simple but sufficient example: opendir(D, $dir) or die "can't opendir $dir: $!";
      By not including debugging information on your arguments you will make such basic things as a renamed directory painful to track down.

        And as a side-note, something I learned the other day for error handling in system calls :

        system("/usr/bin/scp $file $username\@$host:$remoteFile"); die "System call to scp command failed! : $!\n" if ($? != 0);

        The special variable $? returns a boolean value for success or fail on system calls. So the above snippet performs a reliable "or die" check on your command. A handy one to know, especially as system tends to be a little unreliable in terms of error checking / return values when using a straight die, IMHO.

        Update: as per comments below, not a "boolean" in the strict sense of the word. Call it an "azatoth boolean" :)

        Azatoth a.k.a Captain Whiplash

        Make Your Die Messages Full of Wisdom!
        Get YOUR PerlMonks Stagename here!
        Want to speak like a Londoner?

        Well, my CGI program was installed in a controlled environment, where errors should be very rare, and users were generally _very_ clueless. I can't just have my program die on them! Instead, my program would skip the file operation in question, but log an error, and display an error message with a possible solution, print out the rest of the page layout, and then end at the same spot the program would normally.

        Maybe it's a matter of taste, but I just don't like the or die construct, it doesn't give me the ability to neatly and readably handle the exception.

        Example:

        sub display_values_from_file { my $filename = shift; if(open FILE, "<$filename") { # check return value of open flock FILE, 1; while(<FILE>) { print &process_line_from_file; } close FILE; } else { print "The file could not be opened. Please try again later!"; log_error "open $filename : $!"; } } # main program ... # do something print $some_nice_html; display_values_from_file "/foo/bar.baz"; print $rest_of_html_page; # end

        This way, when an error occurs, the user will be informed of the error, and all the links the user would need would still be there.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://94037]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (16)
As of 2014-07-24 15:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (161 votes), past polls