On 4/5/12 Thu Apr 5, 2012 1:34 PM, "Somu" <[email protected]> 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'
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/