On Mon May 25 2009 @  4:34, sanket vaidya wrote:
> Hi all,
> 
>  
> 
> Kindly look at the code below:
> 
>  
> 
> use warnings;
> 
> use strict;
> 
> opendir(DIR, "D:\\test") || die "can't opendir:  $!";
> 
> my @dots = readdir(DIR);
> 
> print map{"$_.\n"}...@dots;
> 
>  
> 
> ..
> 
> ...
> 
> Test1
> 
> Test2
> 
> Test3
> 
>  
> 
> Where Test1, Test2, Test3 are files within test directory.
> 
>  
> 
> Apart from that I also got dots (2 in 1st line & 3 in 2nd line). I know that
> '..' stands for parent directory what does '...' mean?

Your real output must have looked more like this:

  ..
  ...
  Test1.
  Test2.
  Test3.

You put a period ('.') near the end of your call to print, so instead of
seeing '.' and '..' (current and parent directory), you're seeing '..' and
'...' (current and parent directory plus the period).

> In perldoc of readdir() I am unable to follow below line. Can anyone explain
> me the below line with example?
> 
>  
> 
> 'If you're planning to filetest the return values out of a readdir
> <http://perldoc.perl.org/functions/readdir.html> , you'd better prepend the
> directory in question. Otherwise, because we didn't chdir
> <http://perldoc.perl.org/functions/chdir.html>  there, it would have been
> testing the wrong file.'

Consider this:

    use strict;
    use warnings;

    opendir(my $dh, '/home/telemachus/practice') || die "can't opendir:  $!";
    my @contents = readdir($dh);

    my @files = grep { -f "/home/telemachus/practice/$_" } @contents;
    my @noprepend = grep { -f $_ } @contents;

    print "With prepending:\n";
    print "\t$_\n" for @files;

    print "Without prepending:\n";
    print "\t$_\n" for @noprepend;

The first grep prepends (adds at the beginning) the path to each file. If
you don't do that, you're testing for files in the directory where you call
the script (or wherever the script happens to be, if you've changed
directory at some point). Change the path to something reasonable for your
system (in both the call to opendir and the my @files line) and compare the
output you get for the two different uses of grep.

Hope this helps, T

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to