Tuesday, 15 July 2014

Perl open file with variable in name -



Perl open file with variable in name -

why illustration not working?

#!/usr/bin/perl utilize posix qw(strftime); utilize time::local; $date = strftime "%y-%m-%d", localtime; $command = "ls clients/*/errori/" . $date . "/*s"; @result = `$command`; foreach $group (@result) { $file = '/opt/' . $group; open( $input_fh, '<', $file) || die "can't open $file: $!"; print $input_fh; }

it return:

can't open /opt/clients/cli8832/errori/2014-06-25/file.564159972s : no such file or directory @ ./my.pl line 12.

but if ls /opt/clients/cli8832/errori/2014-06-25/file.564159972s works

that's poor illustration learning from.

include use strict; , use warnings in every perl script. include use autodie; anytime you're doing file processing and finally, instead of shelling out ls, utilize file glob. remove need chomp.

then next cleaned version of example, although lastly line bug.

#!/usr/bin/perl utilize strict; utilize warnings; utilize autodie; utilize posix qw(strftime); utilize time::local; $date = strftime "%y-%m-%d", localtime; $group (glob "clients/*/errori/$date/*s") { $file = '/opt/' . $group; open $input_fh, '<', $file; print $input_fh; }

perl

No comments:

Post a Comment