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


In reply to Re: Correct pathway to a file by johngg
in thread Correct pathway to a file by Dr Manhattan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.