Trying to pass a subdirectory as a parameter in Perl -
i have perl program read .html's , works if program in same directory .html's.
able start in different directories , pass html's location parameter. program (shell example below) traverses subdirectory "sub" , subdirectories .html's, works when perl file in same subdirectory "sub". if put perl file in home directory, 1 step subdirectory "sub", doesn't work.
in shell, if type "perl project.pl ./sub" home directory, says not open ./sub/file1.html. no such file or directory. yet file exist in exact spot. file1.html first file trying read.
if change directories in shell subdirectory , move .pl file there , in shell: "perl project.pl ./" ok.
to search directories, have been using file::find concept found here: how traverse files in directory; if has subdirectories, want traverse files in subdirectories too find::file search directory of list of files
#!/usr/bin/perl -w use strict; use warnings; use file::find; find( \&directories, $argv[0]); sub directories { $_ = $file::find::name; if(/.*\.html$/){#only read file on local drive if .html $file = $_; open $info, $file or die "could not open $file: $!"; while(my $line = <$info>) { #perform operations on file } close $info; } return; }
in documentation of file::find says:
you chdir()'d $file::find::dir when function called, unless no_chdir specified. note when changing directories in effect root directory (/) special case inasmuch concatenation of $file::find::dir, '/' , $_ not literally equal $file::find::name.
so @ ~/sub
already. use filename, $_
. not need overwrite it. remove line:
$_ = $file::find::name;
Comments
Post a Comment