Hi i am new and grateful to have guys like yourselves around, cause im at a complete loss with understanding this complex language, as im new to it, and only learning it as its in the sylabus of the ciw course im undertaking.

I have been given the following tma to complete and i havent a scoobi doo (clue) any help would be great, but to be honest im not sure ill ever understand Perl (to write) i understand lots of aspects of it but, not enough obviously.

Please help.

You are to write a Perl program that analyses text files to obtain statistics on their content. The program should operate as follows:

1) When run, the program should check if an argument has been provided. If not, the program should prompt for, and accept input of, a filename from the keyboard.

I will help you with point one to get the ball rolling. You should write something similar to the following;

2) The filename, either passed as an argument or input from the keyboard, should be checked to ensure it is in MS-DOS format. The filename part should be no longer than 8 characters and must begin with a letter or underscore character followed by up to 7 letters, digits or underscore characters. The file extension should be optional, but if given is should be \".TXT\" (upper- or lowercase).

If no extension if given, \".TXT\" should be added to the end of the filename. So, for example, if \"testfile\" is input as the filename, this should become \"testfile.TXT\". If \"input.txt\" is entered, this should remain unchanged.

3) If the filename provided is not of the correct format, the program should display a suitable error message and end at this point.

Note how I have left blanks where patterns should be. Refer to page 3-6/3-7. Try this first and then try to answer points 4 & 5.

4) The program should then check to see if the file exists using the filename provided. If the file does not exist, a suitable error message should be displayed and the program should end at this point.

5) Next, if the file exists but the file is empty, again a suitable error message should be displayed and the program should end.

# point 2) # then, perform the specified checks # check if filename is valid, exit if not if ($filename !~ m/[a-zA-Z]{0,7}\.TXT$/i) { die("\"".$filename."\" is not a valid DOS text filename\n"); # point 3) }

# does the filename end with .TXT? if ($filename !~ m/\.TXT$/i) { $filename .= ".TXT"; }

# point 4) # check if filename is actual file, exit if not if (!-e $filename) { die("The file \"".$filename."\" does not exist\n"); }

# point 5) # check if filename is empty, exit if it is if (-z $filename) { die("The file \"".$filename."\" is empty\n"); }

# point 6) # now we can do the work: open(READFILE, "<$filename") or die "Could not open file \"$filename\":$!";

$chars = 0; $words = 0; $lines = 1; # there will always be at least one line, even if only space on it $sentences = 0; $paragraphs = 1; # there will always be at least one paragraph, even if only a space in it $lastchar = '';

# count sentences: if ($ch eq "?" || $ch eq "!" || $ch eq ".") #if character is one of the three end of sentence markers { $sentences++; }

#count paragraphs and lines if ($ch eq "\n") # if newline... { $lines++; # count another line if ($lastchar eq "\n") # if last character also newline { $paragraphs++; # count another paragraph } } $lastchar = $ch; # remember character for next time through the loop } # loop finished, but what if we were in a word at the end? # check if last character not a word ending character, and if not add one to the word count if ($lastchar ne "\t" && $lastchar ne "\n" && $lastchar ne " ") { $words++; } close(READFILE);

The instructor supplied code is using the bareword READFILE for the filehandle. Since perl 5.6 (which is almost 10 years) it has been recommended (and is better) to use a lexical var for the filehandle and the 3 arg form of open.

Code

open my $READFILE, '<', $filename or die "Could not open file '$filename':$!";

The while loop that process the file could be greatly improved. However, if I show you how, the instructor will know that you didn't write it. But I can suggest that you read up on the tr operator in the "Quote-Like-Operators" section of perlop. http://perldoc.perl.org/perlop.html#Quote-Like-Operators

I did actually learn a lot about what you stipulate, think there just easing us into the language quietly, so to speak so we don't get bogged down.

That may be true, but starting out by teaching you poor or questionable coding practices is a disservice to you because it then makes it that much harder to learn good coding practices. In your case, that may not be important, but for those that do want to be good Perl programmers will suffer.

The instructor supplied code is using the bareword READFILE for the filehandle. Since perl 5.6 (which is almost 10 years) it has been recommended (and is better) to use a lexical var for the filehandle and the 3 arg form of open.

Code

open my $READFILE, '<', $filename or die "Could not open file '$filename':$!";

Thanks for the tips! I've been reading "man perlintro" and it must be old. It talked about scalar variables($var), array variables (@var), and hash variables (%var), but then in the file section, suddenly it introduced INPUTFILE: a constant like variable, with no explanation.

Should the filehandle variable be capitalized($READFILE)? Also, isn't the 3 arg form of open() a pain to type? It's not considered good style to put the '<' in the same string as $filename, e.g.

Ok great, so your an expert, must of been in the business a lot longer than i have.

I just been studying for round 2 years, a Web Development course.

I find Perl difficult, and may try my hands at something more easier, syntax i mean.

python would be a breath of fresh air. I don't like ruby so much, and then with ruby you probably need to learn ruby on rails for web development, which is painful. But I guess the way of the world now is to use frameworks for web development, so with python you would have to learn django or turbogears. perl, python, and ruby are competitors in the programming world.

No, the filehandle does not need to be capitalized. The capitalization is a past convention when using a bareword, but even in that case, it's not required.

I normally use lowercase names and often append fh to the name to make it clear that the var is to be used as a filehandle.

Writing the 3 arg form really isn't a pain, but if you've been using barewords for years, it could take a little time to get used to the 3 arg form. The 3 arg form can be safer, especially when the filename is in a var that is a reference.