Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How do I read from a file, that was read from a directory?

( #6735=categorized question: print w/ replies, xml ) Need Help??
Contributed by C_Mangano on Apr 03, 2000 at 21:14 UTC
Q&A  > files


Description:

I know how to use the while (<>){} method of reading from a file, but my question is this. What if ARGV is given a folder name. Then, a subroutine parses all the folders below this folder, until it gets to a file. Now I want to be able to read a line from that file. Any help would be greatly appreciated. Thanks.

Answer: How do I read from a file, that was read from a directory?
contributed by chromatic

Use the -d test to see if the argument is a directory (perldoc -f -X). Use glob to get a list of all files in that directory. Repeat until you reach a file. (-d $file will be false). Then, open that file and read a line from it. Code might look something like this:

my $possible_file = shift; my $found_file = find_file($possible_file); { local *INPUT; open(INPUT, $found_file) or die "Can't open $found_file: $!"; my $line = <INPUT>; # do something with $line close INPUT or die "Can't close $found_file. Bogosity: $!"; } sub find_file { my $file = shift; while (-d $file) { # the next line may differ on Win/Mac/Un*x my @files = glob("$possible_file/*.*"); foreach (@files) { find_file($_); } # we're not dealing with a file anymore, so return } return $file; }

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (7)
    As of 2015-07-07 08:09 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (87 votes), past polls