Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Read entire file from given folder

by samirpatry (Acolyte)
on Aug 29, 2006 at 09:58 UTC ( #570117=perlquestion: print w/replies, xml ) Need Help??

samirpatry has asked for the wisdom of the Perl Monks concerning the following question:


To read the files present in the given folder i write this program.
use warnings; use strict; my ($listitem,$dir); $dir="C:/Temp"; opendir(DIR, $dir) or die "Can't open $dir: $!"; my @contents = grep !/^\.\.?$/, readdir DIR; foreach $listitem ( @contents ) { print "$listitem\n"; }

It will work fine .but when i use the <STDIN> and give the option to enter the folder name or path to the specified folder then it will not work.Plz help me where i lack somthing

Replies are listed 'Best First'.
Re: Read entire file from given folder
by prasadbabu (Prior) on Aug 29, 2006 at 10:02 UTC

    samirpatry,

    I think, you have to use chomp to remove the newline character at the end of the given string (directory) through <STDIN>. If you show the input, it will be easy to give the exact solution else you will get answers only in assumption.

    Prasad

Re: Read entire file from given folder
by holli (Abbot) on Aug 29, 2006 at 10:03 UTC
    You're reading the $dir from STDIN? My guess is you forgot to get rid of the newline. Try
    $dir = <STDIN>; chomp $dir;
    And next time, please post the code that does *not* work.


    holli, /regexed monk/

      Can also be written as:

      chomp(my $dir = <STDIN>);

      It's nice because you don't have to write two statements, and it is very clear.

      Igor 'izut' Sutton
      your code, your rules.

Re: Read entire file from given folder
by ambrus (Abbot) on Aug 29, 2006 at 10:37 UTC

    Also note that you may want to change the regexp /^\.\.?$/ to /^\.\.?\z/ so that it doesn't match a filename consisting of a dot an a newline.

Re: Read entire file from given folder
by planetscape (Chancellor) on Aug 30, 2006 at 14:04 UTC

    This page on Recursive Editing of files has a very nice example of a script (below) that does (I believe) just what you want. It also demonstrates some nice features such as skipping directories...

    #!/usr/bin/perl use File::Find; @ARGV = ('.') unless @ARGV; $dir = shift @ARGV; find( \&edits, $dir ); sub edits() { return unless -f; #skip directories $seen = 0; $file = $_; #Uncomment next line if you want multi-line edits #undef $/; local $^I = ".backup"; #Warning - heavy magic here local @ARGV = ($file); while (<>) { #Remember to use the s option if doing multiline edits $seen++ if s/Lesson/Chapter/; print; } print "Found in $File::Find::name\n" if $seen > 0; #Comment out if you want to keep the backup #unlink $file.".backup"; }

    The title of your node (Read entire file from given folder) made me think that perhaps you wanted to read an entire file in one go, in which case you may want to take a look at File::Slurp.

    HTH,

    planetscape

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2021-04-21 11:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?