Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Correct pathway to a file

by johngg (Abbot)
on Jan 10, 2013 at 14:12 UTC ( #1012665=note: print w/replies, xml ) Need Help??

in reply to Correct pathway to a file

Prepend your directory name to the filename so as to give the path to the file, in your case an absolute path so it wouldn't matter where your script was. If you are using a relative path for your directory then that must be the path to the directory from where your script is running or you must do a chdir in your script to the place from where that relative path would succeed.

Here is a trivial example.

$ ls test/dir1/ fileA fileB $ head -99 test/dir1/file* ==> test/dir1/fileA <== fileA - line 1 fileA - line 2 ==> test/dir1/fileB <== fileB - line 1 fileB - line 2 fileB - line 3

This script, run from the parent directory of "test"

use strict; use warnings; use 5.010; my $dir = q{test/dir1}; opendir my $dirHandle, $dir or die qq{opendir: $dir: $!\n}; while ( my $file = readdir $dirHandle ) { next unless -f qq{$dir/$file}; say qq{*** $file ***}; print do { open my $fh, q{<}, qq{$dir/$file} or die qq{open: < $dir/$file: $!\n}; <$fh>; }; }

Gives this output

*** fileA *** fileA - line 1 fileA - line 2 *** fileB *** fileB - line 1 fileB - line 2 fileB - line 3

I hope this is helpful.



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1012665]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2018-05-25 11:32 GMT
Find Nodes?
    Voting Booth?