MrStutterZ has asked for the wisdom of the Perl Monks concerning the following question:
Good afternoon, PerlMonks!
I have been given the following assignment and would like to learn how to see if a file or directory exists.
"Prompt the user to type in one parameter (note that this script only takes in one parameter and should report warnings/errors if the user input contains zero or multiple parameters), check if the input parameter is a filename or a directory name.
1) If the user input is a file, exit when the file is an empty file, and print the last line of the file.
2) If the user input is a directory, exit when the directory is empty, use a hash table to record the filenames inside this directory and the assess ages of these files (the number of days since the files were lastly assessed), find the oldest file (based on modification time) in the directory, and print the absolute path to this file."
I have spent the last 2 days looking a detailed explanation of how to use File::Find but cannot it. Below is my code and I'd like to replace -e $input sections with File::Find because with the -e file operator, I can only search in my current directory and not my entire system. I look forward to your replies and greatly appreciate your time.
Again, I have spent hours looking to better understand File::Find. My 4th edition 'Programming Perl' book doesn't do the job and all sample codes online are commented well enough. Perhaps someone here could do a better job or propose another solution to my problem. I don't think I can simply use file/directory test operators or globs to do what I want to do because they are not going to scan the entire system.
#! /usr/bin/perl # Assignment 9, check and read files and directories use strict; use warnings; use Module1; assnintro(); my @parameter; my $input; do { $input = (); @parameter = (); pamcheck( \@parameter ); $input = $parameter[0]; if ( -e $input ) { } else { print color 'red'; print "The file or directory you entered, doesn't exist.\n"; print color 'reset'; } } until ( -e $input ); if ( -d $input ) { print "I am a directory.\n"; } else { } exit; sub pamcheck { my ($parameter) = @_; my $elementcount; do { @$parameter = (); print "Enter one file or directory name: "; @_ = split( /\s+/, <> ); push( @$parameter, @_ ); $elementcount = 0; foreach (@_) { $elementcount++; } if ( $elementcount != 1 ) { print color 'red'; print "Please enter only ONE parameter.\n"; print color 'reset'; } else { } } until ( $elementcount eq 1 ); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: verifying user input with Find::File
by GrandFather (Saint) on Nov 02, 2014 at 21:37 UTC | |
|
Re: verifying user input with Find::File
by Laurent_R (Canon) on Nov 02, 2014 at 21:28 UTC | |
|
Re: verifying user input with Find::File
by dissident (Beadle) on Nov 02, 2014 at 21:15 UTC | |
|
Re: verifying user input with Find::File
by boftx (Deacon) on Nov 03, 2014 at 04:34 UTC | |
|
Re: verifying user input with Find::File
by james28909 (Deacon) on Nov 02, 2014 at 21:39 UTC |