Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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.

Cheers,

JohnGG


Comment on Re: Correct pathway to a file
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1012665]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2014-09-17 18:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (94 votes), past polls