For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2005 > Dot and Dubble Dot directory names









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Dot and Dubble Dot directory names
Tony Frasketi

2005-08-26, 9:55 pm


In a program such as the following....
-----------------------------------------------------------------
#!/usr/local/bin/perl -w

$windir = "/home/users/tony";
opendir(NT, $windir) || die "no $windir?: $!";
while ($name = readdir(NT)) { # scalar context, one per loop
print "$name\n"; # prints ., .., system.ini, and so on
}
closedir(NT);
exit;
-------------------------------------------------------------------
$name returns each of the files in the given directory. To form the
absolute path of the files given by $name, you would simply combine the
$windir and the $name variables like so: "$windir/$name". For example
"/home/users/tony/x.txt".

My question relates to the two files '.' and '..'. Is there any function
that will give the absolute path of these directories when fed $name
values of '.' and '..' ? Without having to chdir to these directories,
that is.

I hope I've posed my question so as to be understandable...
I've looked online and in the O'reilly books but can't find the answer.

TIA
Tony Frasketi

JupiterHost.Net

2005-08-26, 9:55 pm

> In a program such as the following....
> -----------------------------------------------------------------
> #!/usr/local/bin/perl -w
>


# always always always use strict and warnings!!!
use strict;
use warnings;
use File::Spec;

> $windir = "/home/users/tony";


my $windir = '/home/users/tony';

> opendir(NT, $windir) || die "no $windir?: $!";


"or die" not "|| die"

> while ($name = readdir(NT)) { # scalar context, one per loop


while(my $name...

or better yet:

while(readir NT) {
my $abs = File::Spec->rel2abs($_);
print "$abs\n";
}

> print "$name\n"; # prints ., .., system.ini, and so on
> }
> closedir(NT);
> exit;


exit not needed

> -------------------------------------------------------------------
> $name returns each of the files in the given directory. To form the
> absolute path of the files given by $name, you would simply combine the
> $windir and the $name variables like so: "$windir/$name". For example
> "/home/users/tony/x.txt".
>
> My question relates to the two files '.' and '..'. Is there any function
> that will give the absolute path of these directories when fed $name
> values of '.' and '..' ? Without having to chdir to these directories,
> that is.


perldoc -f File::Spec

HTH :)

Lee.M - JupiterHost.Net
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com