• Home
  • Amoeba
  • ASK
  • Community
  • Source Codes
  • Contact Us
Blue Orange Green Pink Purple

Error Handling in Perl & EVAL Block

Posted in Perl Programming, Programming Techniques. on Thursday, January 28th, 2010 by Aneeska Tags: directory existance check, error handling, error handling in perl, ftp directory exists, ftp in perl, Net:FTP, perl
Jan 28

This article covers two things.

1) Error handling in Perl using EVAL Block
2) Using the EVAL Block feature to check whether a directory exists on a remote FTP location.

The first time ever I had to work in Perl was last month when I was asked to look in to an issue with sending a file to a remote FTP from the server. Basically, the perl script fetches a file from the server and put it into a remote FTP location. Before copying the file to FTP, the script changes the current working directory of the remote destination using the $ftp-cwd command. We are using Net:FTP library to work with FTP. If the folder doesn’t exist on the FTP, perl needs to create the folder and FTP the file there. We faced basically two problems. 1) Using Net:FTP, there is no way to know that a folder exists or not. 2) The old versions of Perl doesn’t feature a dedicated error handling mechanism.

Since we don’t have a way to know whether a folder exists on the FTP or not, the only other way is to try changing the directory and then catch the error if the directory didn’t exist.  We can use “or die” with the $ftp->cwd command to terminate the script if the director doesn’t exist. But that doesn’t solve our problem. The script should not be terminated, and it should create the directory and upload the file.  All I wanted was something like a try{} catch(){} which is available in most of the programming languages.

Finally we found an alternative way of implementing an error catching mechanism in perl. That is using the EVAL block.

Here is a sample use of EVAL Blcok.

# Sample code to demonstrate EVAL usage
eval {
$ftp->cwd($ftpdir) or die "Directory doesn't exist. ".$ftp->message;  //the script doesn't get terminated here because its in an eval block.
}
if ($@) {
$ftp->mkdir($ftpdir);
$ftp->cwd($ftpdir);
}
#Continue your script

If the CWD command caused an error “$@” will hold the string value passed to the die command. If “$@” is null, we know that there is no error and just don’t do anything to handle the error. What I found out was we really need the die command inside the block and without the die command “$@” returned null though there was an error.

Thanks,
Anees


ASK a Question!

Bookmark and Share

Leave a Reply

CAPTCHA Image CAPTCHA Audio
Refresh Image
  • Search a Topic


  • Got a Question?
      ASK and get a Solution! ASK
  • Categories
    • CakePHP
    • Computers
    • CSS & XHTML
    • Enterprise Web
    • Flash & Action Script
    • Flex
    • Javascript & Libraries
    • Perl Programming
    • PHP/MySQL
    • Programming Techniques
    • Regular Expression
    • Social Media
  • Recent Visitors
  • Recent Articles
    • How to Protect your SWF (AS2 or AS3). Prevent SWF Decompile using SWF Protector
    • PHP – Search in an Array for Values Matching a Pattern – Regex, Wildcard
    • PHP – How to Get Browser Properties and Capabilities – get_browser()
    • CakePHP – Search for records between two dates inclusively
    • Notebook/Laptop hangs frequently/random (on Low Battery when AC power is connected)
    • Swap Values Without Temp or A Third Variable in PHP
    • CakePHP – Auth Login Redirect Problem with Plugins
    • Convert PHP array to Javascript array
    • CakePHP – Accessing a model in AppController or in any Controllers
    • Save Images from Flash – Actionscript 3, Filereference.save, JPGEncoder
  • Archives
    • September 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
  • Tags
      vertically center horizontally align dead center div amoeba ask credit card workbench decryption solutions kiosk questions horizontal center align vertical center align css trick solution css encryption as2 flash regex as3 Regular Expression action script mysql array CakePHP php

  • Donate

      If you find this site helpful, please donate to Amoeba Solution Kiosk. We will be using the money received for conducting technology awareness camps and seminars for school and college students in rural areas of Kerala, India.

  • Links
    • Amoeba Solutions
    • Blog Catalog
  • Home
  • Amoeba
  • ASK
  • Community
  • Source Codes
  • Contact Us

© Copyright Amoeba Solution Kiosk. All rights reserved.
An Amoeba Concept!

Back to Top