#!/usr/bin/env perl use strict; use warnings; unless (@ARGV){ #part A print "Enter the name of an input file: \n"; chomp (my $in_file = ); -e $in_file and -s _ and -r _ or die "File doesn't exist, is empty, or isn't readable\n"; open my $in_fh, '<', $in_file or die "Could not open $in_file: $!\n"; chomp (my $line = <$in_fh>); my ($n1, $n2) = split ' ', $line; print "Enter the name of an output file:\n"; chomp (my $out_file = ); open my $out_fh, '>', $out_file or die "Could not open $out_file: $!\n"; my $old_fh = select $out_fh; print "$n1 + $n2 = ", $n1 + $n2, "\n"; print "$n1 - $n2 = ", $n1 - $n2, "\n"; print "$n1 * $n2 = ", $n1 * $n2, "\n"; print "$n1 / $n2 = ", $n1 / $n2, "\n"; close $out_fh or die "Could not close $out_file: $!\n"; } else { my %files_starting_with; my %files_named; for my $directory (@ARGV){ unless (-e $directory and -d _){ warn "$directory doesn't exist or isn't a directory. Moving on\n"; next; } opendir my $dir_handle, $directory or die "Can't open $directory: $!\n"; while (my $filename = readdir $dir_handle){ if (-f "$directory/$filename"){ $files_named{$filename}++; $files_starting_with{lc substr($filename, 0, 1)}++; } } } for my $filename (keys %files_named){ print "There were $files_named{$filename} files named $filename\n"; } print "\n\n"; for my $letter ('a'..'z'){ if (exists $files_starting_with{$letter}){ print "There were $files_starting_with{$letter} files starting with $letter\n"; } else { print "There were not any files starting with $letter\n"; } } }