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 ); }

In reply to verifying user input with Find::File by MrStutterZ

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.