Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Full path to the current file?

by davidrw (Prior)
on Jan 08, 2008 at 14:59 UTC ( #661100=note: print w/replies, xml ) Need Help??

in reply to Full path to the current file?

File::Spec, File::Basename
use File::Spec; use File::Basename; my $f = '../blah.txt'; my $dir = dirname( File::Spec->rel2abs($f) );

Replies are listed 'Best First'.
Re^2: Full path to the current file?
by svenXY (Deacon) on Jan 08, 2008 at 15:02 UTC
    Hi, ++davidrw
    or even
    $ perl -MFile::Spec -le 'print File::Spec->rel2abs( __FILE__ ) ;' /home/svenXY/-e # for a one-liner

    File::Basename is not necessary here.
      OP said I have a module that needs to know its path in order to access another file in the same location. -- the second step of File::Basename is to fulfill that requirement (even though he didn't directly ask for it).

      Other shell (bash) solutions:
      # setup cd /var/tmp/ touch foo f='../../var/tmp/foo' # for just the directory: d=`(cd ${f%/*} && pwd)` #or: d=`dirname $f` d=`cd $d && pwd` # for the filename (rel2abs): d=`dirname $f` d=`cd $d && pwd` f=`basename $f` f=`/bin/ls $d/$f`
Re^2: Full path to the current file?
by RoUS (Initiate) on Jan 08, 2008 at 16:42 UTC

    This works; thanks! The 'funky' aspect to which I referred is that when invoked from a script, the module's __FILE__ is 'XML/'. When invoked from the command line:

    perl -e "use XML::Foo; $x = new XML::Foo;'

    its __FILE__ is reported as '/XML/' -- which is clearly bogus.

    It has been suggested that the latter behaviour is due to Red Hat's patches for Fedora Core 8.

    Whatever, you've given me the solution. I guess I'll just have to wait and see if it works in all cases, or if there are edge conditions where it goes blooey.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://661100]
[Discipulus]: Almosen is Elemosina in Eatalian
[erix]: yeah, you mediteraaneans are one big family :)
[Discipulus]: greece had to copyrigth etymologies to fill the debt..
[erix]: good no-nonsense american approach to these soft european practices: "Upon entering the Almshouses in Connecticut, patients were whipped up to 10 times"
Discipulus should have..
[erix]: the almshouse generally seems to have become an anglosaxon relic :P
[Discipulus]: just to be sure was not for a free lunch.. sbaaam!
[Discipulus]: here now we call them Cities
[erix]: 'cities'? in italian? are they funded by charity?
[LanX]: The New Testament was first "published" in Greek, that's why

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2017-11-23 20:26 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (338 votes). Check out past polls.