Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: reading files from a directory

by toolic (Bishop)
on Oct 28, 2010 at 02:20 UTC ( [id://867885]=note: print w/replies, xml ) Need Help??


in reply to reading files from a directory

Get rid of the whitespace surrounding your IN filehandle. Change:
while ( < IN > ){

To:

while ( <IN> ){

B::Deparse can shine a little more light upon the situation (Tip #6 from the Basic debugging checklist):

perl -MO=Deparse 867882.pl my $dir = 'input_files'; die "can't opendir $!" unless opendir DIR, $dir; while (defined(my $file = readdir DIR)) { do { print "The directory and file are $dir/$file\n"; die "Can't open input file $!" unless open IN, "< $dir/$file"; use File::Glob (); while (defined($_ = glob(' IN '))) { print $_; } }; } closedir DIR;

Here is an explanation from perlop:

Even <$x > (note the extra space) is treated as glob("$x ") , not readline($x).

Replies are listed 'Best First'.
Re^2: reading files from a directory
by kevyt (Scribe) on Oct 28, 2010 at 06:01 UTC
    I got it thanks! This will help with something called Indivo. http://wiki.chip.org/indivo/index.php/Schemas
    while (defined($_ = glob(' IN '))) { print "Hello $_"; } while (<IN>){ print "Bye " . $_ ; }
    I need to read about glob because I don't understand that line. Which print statement has better style? Should I use the . (join) in a print? Thanks, Kevin

      There's no need to concatenate arguments to print as that function takes a list. In fact, you're actually requesting additional processing: do the concatenation and then do the print. Even worse, if you concatenate multiple arguments you're performing multiple operations; in sort of pseudocode: print A.B.C.D performs A.B, then AB.C, then ABC.D, then print ABCD, while print A,B,C,D is one operation.

      As far as style is concerned, I'd say do whatever is easiest to read and maintain. print "X ", $_, "\n"; and print "X $_\n"; are equally valid but, as mentioned above, avoid print "X " . $_ . "\n";.

      I note there's a few places where you haven't included a newline (\n) at the end of your print statement. In case you didn't know, print doesn't automatically add one for you; say, on the other hand, does.

      -- Ken

Re^2: reading files from a directory
by kevyt (Scribe) on Oct 28, 2010 at 05:49 UTC
    Thanks very much. I am not able to print the contents of the file. I'll read about glob and the explanation in the perlop. Thank you!
    my $dir = 'input_files'; die "can't opendir $!" unless opendir DIR, $dir; while (defined(my $file = readdir DIR)) { do { print "The directory and file are $dir/$file\n"; die "Can't open input file $!" unless open IN, "< $dir/$file"; use File::Glob (); while (defined($_ = glob(' IN '))) { print $_; } }; } closedir DIR;
    Output
    perl create_indivo_schemas.pl Name "main::IN" used only once: possible typo at create_indivo_schemas +.pl line 18. The directory and file are input_files/allergies INThe directory and file are input_files/immunizations INThe directory and file are input_files/.. INThe directory and file are input_files/.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2026-02-11 06:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.