That looks a bit off to me. First of all, $string contains the information, but you are splitting $filename. Secondly, every wc implementation that I've ever seen outputs the linecount before the filename, but you have them reversed....
If I were going to rewrite this snippet and was forced to use wc, I might do something like:
#!/usr/bin/perl -wT
use strict;
%ENV = (PATH => '/bin:/usr/bin'); # give us a happy enviornment
my $filename = '/etc/services'; # filename to be checked
my $length = do {
my $string = `wc -l $filename`; # get output of wc
die "wc error $?" if $?; # die if wc chokes for some reason
no warnings 'numeric'; # turn off a pesky warning ;-)
$string + 0; # numerify it with +0
};
print "length = $length\n"; # "length = 331" on my machine
Update:
*sigh* /g still gives me trouble some times.... The numerify line above could also have been:
($string =~ /\d+/g)[0];
which would eliminate the need to turn off the warning. Therefore, the above can all be squeezed down into:
my $length = (`wc -l $filename` =~ /\d+/g)[0]; # get output of wc
die "wc error $?" if $?; # die if wc chokes for some reason
-Blake
|