how to find the path of the file

A

Amaninder

Hi everyone

I have a perl file called abc.pl and when i run it i want it to print
the complete path of the folder in which it is located. For example,
if abc.pl is in c:\temp\xyz then i want to print "c:\temp\xyz" on the
screen when i run abc.pl. Does any one know how to do it?

Any help will be appreciated.

Regards
Amaninder Saini
 
B

Ben Morrow

Quoth "Amaninder said:
Hi everyone

I have a perl file called abc.pl and when i run it i want it to print
the complete path of the folder in which it is located. For example,
if abc.pl is in c:\temp\xyz then i want to print "c:\temp\xyz" on the
screen when i run abc.pl. Does any one know how to do it?

Any help will be appreciated.

See the FindBin module.

Ben
 
A

Amaninder

Ben said:
See the FindBin module.

Ben

--
For far more marvellous is the truth than any artists of the past imagined!
Why do the poets of the present not speak of it? What men are poets who can
speak of Jupiter if he were like a man, but if he is an immense spinning
sphere of methane and ammonia must be [email protected]

Hi
Can anyone tell whats the difference between first two and last two of
the following variables. What does it mean by "all links resolved" . I
am new to perl.

$Bin - path to bin directory from where script was invoked
$Script - basename of script from which perl was invoked
$RealBin - $Bin with all links resolved
$RealScript - $Script with all links resolved

I cut it from the cpan page
http://search.cpan.org/~nwclark/perl-5.8.7/lib/FindBin.pm

Regards
Amaninder
 
G

Gunnar Hjalmarsson

Amaninder said:
Can anyone tell whats the difference between first two and last two of
the following variables. What does it mean by "all links resolved" . I
am new to perl.

$Bin - path to bin directory from where script was invoked
$Script - basename of script from which perl was invoked
$RealBin - $Bin with all links resolved
$RealScript - $Script with all links resolved

I cut it from the cpan page
http://search.cpan.org/~nwclark/perl-5.8.7/lib/FindBin.pm

$RealBin and $RealScript probably convert possible symlinks in $Bin
respective $Script to the real locations.
 
B

Ben Morrow

[please don't quote signatures]

Quoth "Amaninder said:
Hi
Can anyone tell whats the difference between first two and last two of
the following variables. What does it mean by "all links resolved" . I
am new to perl.

$Bin - path to bin directory from where script was invoked
$Script - basename of script from which perl was invoked
$RealBin - $Bin with all links resolved
$RealScript - $Script with all links resolved

This is a concept that doesn't apply on Windows[1]. Under Unix a
'symbolic link' can be created to a file, a bit like a shortcut but
properly integrated into the OS. Then, if your script was invoked as

/foo/bar/baz

but /foo/bar is a symlink to /quux then you will get

$Bin = '/foo/bar';
$Script = '/foo/bar/baz';
$RealBin = '/quux';
$RealScript = '/quux/baz';

Ben

[1] Yes, I know about NTFS junctions. I'm fairly sure FindBin doesn't
:).
 
P

Paul Lalli

Amaninder said:
Can anyone tell whats the difference between first two and last two of
the following variables. What does it mean by "all links resolved" . I
am new to perl.

Not especially relevant, because "links" have nothing to do with Perl.
They are a generic Unix concept.
$Bin - path to bin directory from where script was invoked
$Script - basename of script from which perl was invoked
$RealBin - $Bin with all links resolved
$RealScript - $Script with all links resolved

It means that if $Bin or $Script contain any symlinks, $RealBin and
$RealScript will have all those symlinks replaced with whatever they
actually link to. A "link" is a way of giving a different name to an
existing file or directory.

For example, consider a directory with three folders:
$ ls -l
total 6
drwxrwxr-x 3 plalli devel 512 Jul 13 14:31 f1
drwxrwxr-x 2 plalli devel 512 Jul 13 14:31 f2
drwxrwxr-x 2 plalli devel 512 Jul 13 14:31 f3

The f2 folder contains two files:
$ ls -l f2
total 0
-rw-rw-r-- 1 plalli devel 0 Jul 13 14:31 four.txt
-rw-rw-r-- 1 plalli devel 0 Jul 13 14:31 three.txt

Now I create a symlink to the f2 directory:
$ ln -s f2 new_folder

If I look at the main directory again, I see:
$ ls -l
total 8
drwxrwxr-x 3 plalli devel 512 Jul 13 14:31 f1
drwxrwxr-x 2 plalli devel 512 Jul 13 14:31 f2
drwxrwxr-x 2 plalli devel 512 Jul 13 14:31 f3
lrwxrwxrwx 1 plalli devel 2 Jul 14 11:41 new_folder -> f2

Notice how the new_folder entry is "pointing" to f2? That means it's a
different name for an existing entry. If I then look at the contents
of new_folder, I'll see the contents of f2:
$ ls -l new_folder/*
-rw-rw-r-- 1 plalli devel 0 Jul 13 14:31
new_folder/four.txt
-rw-rw-r-- 1 plalli devel 0 Jul 13 14:31
new_folder/three.txt

So that's your brief overview of symlinks. In your specific case, if
FindBin had told you that $Bin contains a path with new_folder in it,
$RealBin would contain that same path, but with new_folder replaced by
f2.

Hope this helps,
Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,635
Latest member
SkyePurves

Latest Threads

Top