Write a “mini-file manager” program that will process commands typed from the command line. Note that the perl program is executed once only, with no command line arguments. The program prompts you for commands repeatedly until you tell it to quit. See sample output from a run of this program.
Prints the current working directory. Use this code:
use Cwd; # this goes at the top of your file my $working_dir; # variable declaration $working_dir = getcwd(); # this gives you what you want
Changes directory to the given name. If no name is given, then changes directory to the home directory. See more details.
For each name given on the command line:
if it’s a simple file, the command will simply print
the file name and its file size. If the name is a directory, it will list
all the files in that directory and their file sizes. The command must
give an error message if a file name does not exist. You may choose to have
the .
and ..
directories appear in a
directory listing or not, as you wish.
Deletes the given files. If a file doesn’t exist, the command gives an error message. This command does not ask the user if she wishes to delete the files; it just goes ahead and deletes them.
Creates the given files if they don’t already exist. To create
a file, just open
it and close it immediately afterwards. If
you can’t create a file, give an error message. (This will happen if
you try to create a file in a directory that doesn’t exist
or where you don’t have permission). If a file already exists,
the program does nothing to it.
If the user enters an invalid command, give an error message. Blank lines and lines beginning with a # should be ignored. You do not need to handle file names with blanks in them.
There is no single part of this program that is particularly difficult; it just has a lot of parts.
Unfortunately, the %ENV
entries for home directory are different on UNIX
and Windows. Here is code that will reliably get the name of your home directory:
if (exists($ENV{"HOME"})) { $home_dir = $ENV{"HOME"}; # UNIX } else { $home_dir = $ENV{"HOMEDRIVE"} . $ENV{"HOMEPATH"}; # Windows }
Name your file lastname_firstname_filemgr.pl and email it to the instructor.