Solved

PERL Script

Posted on 2003-10-31
4
189 Views
Last Modified: 2010-03-05
Hi

I am new to this site and i am new to PERL programming also.

I am looking for a help from you.

I am having a requirement which goes like this

I have to write a perl script to read a text file(This text file is having a single column)  -- But before that

1: First I have to check whether that file exist in the path given.If it exist then return 0 otherwise send an email to xyz@xyz.com stating that file not found in the given directory but return 0 right now also.
2: If that file is read permission then return 0 if not then send an email to xyz@xyz.com stating that file is not having read permission but return 0
3: Read the file and if it is having wild characters(*,(,),%,$,@,!,.,{,},[,],",:,;) then send an email to xyz@xyz.com regarding the wild card presence (Along with the line numbers) in the file and return 0.

Means - after running this script - We have to always return a zero but if we find any errors which are mentioned above then we have to send an email.

Can somebody help ???

nbo
0
Comment
Question by:nbotension
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
jmcg earned 25 total points
ID: 9661883
You're saying "return 0" when I think you mean "continue on to the next step" at several stages in your description.

There is no point to separately checking for the existence and readability of the file. That leaves us with just two things to check: 1) can we open the file for reading? and 2) does the file contain any forbidden characters?

Let's assume you have already figured out how to send an email and that knowledge is embedded in a subroutine named "send_error_email" which takes as its single argument a string containing the error message that is to be sent.

Let's assume the "given path" is in the variable $path.

Let's assume the file name is in the variable $file.

That means the basic script will look something like this one:

use strict;

my $badchars = q/*()%$@!.{}[]":;/;

open FILE, "$path/$file" or do {
               send_error_email( "could not open $path/$file for reading because $!";
               exit 0;
               };

while( <FILE> ) {
              if ( tr/$badchars// ) {
                  send_error_email( "Bad characters found in $path/$file at line $.");
                  last;
                }
              }
exit 0;
0
 
LVL 48

Expert Comment

by:Tintin
ID: 9662052
Sounds very much like homework, which is not permitted under EE guidelines.

Please clarify what the "requirement" is.
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10038315
Nothing has happened on this question in over 2 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by jmcg (or delete, forfeit points because it's homework).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now