Answer to Chapter 15 Exercise
Here’s one way to do it.
#!/usr/bin/perl use Module::CoreList; my %modules = %{ $Module::CoreList::version{5.006} }; print join "\n", keys %modules;This answer uses a hash reference (which you’ll have to read about in Alpaca, but we gave you the part to get around that. You don’t have to know how it all works as long as you know it does work. You can get the job done and learn the details later.)
There are a couple ways to approach this problem. For our solution, we used the
Cwd(current working directory) module to find out where we were in the filesystem. We used a glob to get the list of all the files in the current directory; the names don’t have the directory information, so we have to add that. You could have usedopendirtoo, butglobis less typing. Our glob pattern includes.*to get the Unix hidden files, which don’t match the*pattern.Once we have all the filenames, we go through them with
foreach. For every name, we callFile::Spec->catfile()just like what that module shows in its documentation. We save the result in$path, then print that to standard output:#!/usr/bin/perl use Cwd; # Current Working Directory use File::Spec; my $cwd = getcwd; my @files = glob ".* *"; foreach my $file ( @files ) { my $path = File::Spec->catfile( $cwd, $file ); print "$path\n"; }This answer is much easier than the previous one, even though you had to write the last program to use this one. The work happens in the
whileloop. For every line of input, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access