Reputation: 609
Why is this example not working?
#!/usr/bin/perl
use POSIX qw(strftime);
use Time::Local;
my $date = strftime "%Y-%m-%d", localtime;
my $command = "ls clients/*/ERRORi/" . $date . "/*s";
@result = `$command`;
foreach $group (@result) {
my $file = '/opt/' . $group;
open( my $input_fh, '<', $file) || die "Can't open $file: $!";
print $input_fh;
}
it will return:
Can't open /opt/clients/cli8832/ERRORi/2014-06-25/file.564159972s
: No such file or directory at ./my.pl line 12.
but if I do ls /opt/clients/cli8832/ERRORi/2014-06-25/file.564159972s
it works
Upvotes: 2
Views: 1865
Reputation: 35208
That's a poor example to be learning from.
use strict;
and use warnings
in EVERY perl script.use autodie;
anytime you're doing file processingls
, just use a file glob
. This will remove the need to chomp
.Then following is a cleaned up version of that example, although the last line is probably also a bug.
#!/usr/bin/perl
use strict;
use warnings;
use autodie;
use POSIX qw(strftime);
use Time::Local;
my $date = strftime "%Y-%m-%d", localtime;
for my $group (glob "clients/*/ERRORi/$date/*s") {
my $file = '/opt/' . $group;
open my $input_fh, '<', $file;
print $input_fh;
}
Upvotes: 2
Reputation: 42154
Your $file
variable contains a trailing linefeed character, whereas your actual file name doesn't.
Your ought to chomp
it out.
Upvotes: 8