LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-11-2004, 04:54 AM   #1
Crashed_Again
Senior Member
 
Registered: Dec 2002
Location: Atlantic City, NJ
Distribution: Ubuntu & Arch
Posts: 3,503

Rep: Reputation: 57
Perl Error Handling


I'm trying to learn perl and the first script I wrote was designed out of necessity. A site that I belong to requires us to upload two images via ftp to their web server for our webcams. Rather then do all this manually I thought it would be neat to try and right it in a perl script.

In a nutshell, the program
1. reads specific values from a configuration file(i.e. web address, username, etc.)
2. prompts the user to see if the values are correct
3. prompts the user for their password
4. uploads the images to the server.

The problem I'm having is that I don't know how to put any error handling in my script. If I happen to type an incorrect password the program will exit giving some nasty perl error message rather then a simple "Your password is incorrect.". If for some reason the server address specified in the .conf file is not accepting ftp connections the program will just hang. I'd like to know how to handle these types of errors in a more professional way rather then having the program throw nasty perl errors or just hanging and doing nothing.

Here is the perl script:

Code:
#!/usr/bin/perl -w

# Shiner_ftp Client Program
# This program will open up a connection to an ftp server and upload two files.
# All values are taken from shiner_ftp.conf

$ConfigFile = "shiner_ftp.conf"; # Set the configuration file to shiner_ftp.conf

open(CONFIG, $ConfigFile) or die "The file $ConfigFile could " . # Open the shiner_ftp.conf file or end the program
 "not be found. \n\a";


while (<CONFIG>) { # Loop through shiner_ftp.conf
    chomp;                  # no newline
    s/#.*//;                # no comments
    s/^\s+//;               # no leading white
    s/\s+$//;               # no trailing white
    next unless length;     # anything left?
    my ($var, $value) = split(/\s*=\s*/, $_, 2); # Split up the configuration file values to the parts we need
    $User_Preferences{$var} = $value; # Set the variable names
} 

# Set each value read from shiner_ftp.conf to a local variable
$WEB_ADDRESS = $User_Preferences{"WEB_ADDRESS"};
$USERNAME = $User_Preferences{"USERNAME"};
$WEBCAM_IMAGE = $User_Preferences{"WEBCAM_IMAGE"};
$WEBCAM_IMAGE_THUMBNAIL = $User_Preferences{"WEBCAM_IMAGE_THUMBNAIL"};

# Print out the values we got from shiner_ftp.conf
print "According to your $ConfigFile you have:\n\n";
print "FTP server is: $WEB_ADDRESS\n";
print "Username is: $USERNAME\n";
print "First image is: $WEBCAM_IMAGE\n";
print "Second image is: $WEBCAM_IMAGE_THUMBNAIL\n\n";

print "Are these values correct? Y or N: ";

while(1) { # Infinite loop
  $DoSearch = <STDIN>; # Get keyboard input
  chomp($DoSearch); # Remove the newline character
  $DoSearch =~ tr/A-Z/a-z/; # Converts input to lowercase
  
  if($DoSearch eq 'y') # If the user enters 'y' then do this stuff
    {
    print "What is your password? "; # Asks the user for the password
    system("stty -echo"); # Turns off character echo so the password can not be seen
    $password = <STDIN>; # Gets the users password
    chomp($password); # Removes the carriage return off the end of the password
    system("stty echo"); # Turns character echo back on
    print "\n"; # Prints a charage return
    use Net::FTP; # Initiliase the module
    $ThisFTP = Net::FTP->new($User_Preferences{"WEB_ADDRESS"}) or die "I can't connect to $WEB_ADDRESS.\n\a"; # Establish a connection to the server
    $ThisFTP->login($User_Preferences{"USERNAME"}, $password) or die "Username and password incorrect.\n\a"; # Login to the server
    $ThisFTP->cwd ('webcam') or die "I can't change to the directory webcam because it doesn't exist\n\a"; # Change directory to /webcam
    $ThisFTP->binary or die "Can't do a binary transfer for some reason"; # Do a binary transfer rather then ASCII
    $ThisFTP->put($User_Preferences{"WEBCAM_IMAGE"}) or die "$WEBCAM_IMAGE doesn't exist!\n\a"; # Upload the first image
    $ThisFTP->put($User_Preferences{"WEBCAM_IMAGE_THUMBNAIL"}) or die "$WEBCAM_IMAGE_THUMBNAIL doesn't exist!\n\a"; # Upload the second image
    die "Your file(s) where uploaded successfully.\n";
    } # End if($DoSearch eq 'y') statement
  if($DoSearch eq 'n') # If the user enters 'n' then exit the program
    {
    die "Please edit $ConfigFile, correct the values, and restart the program.\n";
    } # End if($DoSearch eq 'n') statement
  print "Please enter either Y or N: "; # Make sure they either put 'y' or 'n'
  next; # Returns to the beginning of the while(1) loop
    } # End the while(1) loop
Here is the configuration file:

Code:
# shiner_ftp.conf
# 
# This is the configuration file for the shiner_ftp.pl perl script.  All lines
# beginning with the # symbol are ignored by shiner_ftp.pl.  This configuration 
# file is used to set 4 variables.

# What is the address of the ftp server you want to connect to?
# EXAMPLE: 
# WEB_ADDRESS = www.somesite.com

WEB_ADDRESS = www.sitetouploadto.org

# What is the username for the ftp server?
# EXAMPLE: 
# USERNAME = myusername

USERNAME = Shiner_Man

# What file(s) would you like to upload to the server?
# EXAMPLE: 
# WEBCAM_IMAGE = /path/to/webcam_image.jpg
# Note: Please give the full path to the files you wish to upload.

WEBCAM_IMAGE = /home/vince/gimp/shiner.jpg
WEBCAM_IMAGE_THUMBNAIL = /home/vince/gimp/thb_shiner.jpg
This is my first perl script so feel free to comment on it if you see anything I did wrong. Thanks in advance.
 
Old 01-21-2004, 02:25 PM   #2
esben
Member
 
Registered: Jun 2003
Location: Copenhagen, Denmark
Distribution: Gentoo
Posts: 48

Rep: Reputation: 15
The script looks sortof fine to me The only hole I can see right of is that there is no checking that the configuration file actually provide the values you assume it does. That would result in something about "using an undefined var to string concat".

Could you post an actual error message? The problem with the "hang" when a non-responding server is entered can't be helped; after some time the connection will time out and you'll get your error message.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ opendir() handling error bendeco13 Programming 1 11-06-2005 02:37 AM
Bash Script and Loop error handling Kedelfor Programming 5 05-22-2005 02:22 PM
libjpeg error handling luigi Programming 1 04-19-2005 04:43 AM
xinetd error handling iftiuk Linux - Networking 0 04-21-2004 02:32 PM
eval and try / error handling rajatgarg Programming 1 04-12-2004 08:01 PM


All times are GMT -5. The time now is 03:21 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration