in reply to Getting the output of a shell command
Be careful what you ask for -- you may just wind up getting it. Are you really sure you want the exact output of the dir command, or do you just want the names of the files, or the date and/or the size as well. That is, do you expect to have to parse the output downstream to do something else with it?
If this is the case, you are better off doing to internally in Perl, thereby saving the time it takes to launch off the sub-process as well as the time it takes to parse the results.
For instance, if you want to gather the names of the files and their respective sizes, you could do something like:
#! /usr/bin/perl -w use strict; my $dir = shift || '.'; opendir D, $dir or die "Cannot open dir $dir: $!\n"; my %size; while( defined( my $file = readdir(D) )) { # only makes sense to ask for the size of a file next unless -f $file; $size{$file} = (stat $file)[7]; } closedir D;
And there you have it. If you want to save different information, look at the stat function stat. If you need to recurse directories (i.e. emulate dir's -s switch), take a look at File::Find. If you need to store more than one thing (e.g. the date and the size) then it would be a good time to learn about references (perldoc perlreftut and perldoc perlref).
|
|---|