develooper Front page | perl.beginners | Postings from April 2012

Re: chomp

Thread Previous
From:
Jim Gibson
Date:
April 5, 2012 16:47
Subject:
Re: chomp
Message ID:
CBA37CF8.19142%JimSGibson@gmail.com
On 4/5/12 Thu  Apr 5, 2012  1:34 PM, "Somu" <som.ctc@gmail.com> scribbled:

> Hello everyone,
> 
> #this code works for any valid input
> #
> use strict;
> use warnings;
> 
> print "\n\n\tEnter directory : ";
> my $path = <>;
> chomp($path);                           #but if this line is eliminated, it
> shows d drive(current drive) for any input
> my @files = glob "$path/*" or die;
> print "\n\n\tReading file names in $path";
> print "\n\n\tFiles are :\n";
> foreach ( @files ){print;print"\n";}
> print "\n\n";
> system('pause');
> 
> 
> What's going on??

glob will split the string you give it on whitespace, allowing you to use
such patterns as glob("*.h *.c"). When you don't remove the newline from
your input line, you sre giving glob the string "directory\n/*". This is
parsed into the two strings "directory" and "/*". The second of these will
expand to all of the files in your top-level directory.

See 'perldoc -f glob' and 'perldoc File::Glob'





Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About