Perl and current directory

Discussion in 'Perl Misc' started by Jørn Dahl-Stamnes, Dec 9, 2006.

  1. How can a perl script find the absolute path to itself?

    I have tried to do a 'print $0;', but $0 only tells me the command used to
    invoke the script.

    What I need to find is the absolute path to the directory where the perl
    script is located.

    --
    Jørn Dahl-Stamnes
    http://www.dahl-stamnes.net/dahls/
     
    Jørn Dahl-Stamnes, Dec 9, 2006
    #1
    1. Advertising

  2. Jørn Dahl-Stamnes wrote:
    > How can a perl script find the absolute path to itself?
    >
    > I have tried to do a 'print $0;', but $0 only tells me the command used to
    > invoke the script.
    >
    > What I need to find is the absolute path to the directory where the perl
    > script is located.


    print $0 =~ /(.+)\//;

    Or more portable:

    use FindBin '$Bin';
    print $Bin;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Dec 9, 2006
    #2
    1. Advertising

  3. Gunnar Hjalmarsson wrote:

    > Jørn Dahl-Stamnes wrote:
    >> How can a perl script find the absolute path to itself?
    >>
    >> I have tried to do a 'print $0;', but $0 only tells me the command used
    >> to invoke the script.
    >>
    >> What I need to find is the absolute path to the directory where the perl
    >> script is located.

    >
    > print $0 =~ /(.+)\//;


    $0 does not contain the absolute path. If I do a:

    cd ~/some_dir
    ../some_perl_script.pl

    $0 will contain "./some_perl_script.pl".

    What I need is to get exactly the same as the pwd command prints out.

    > Or more portable:
    >
    > use FindBin '$Bin';
    > print $Bin;


    Seems like I need to download some kind of module. FindBin is not installed
    at my system.

    --
    Jørn Dahl-Stamnes
    http://www.dahl-stamnes.net/dahls/
     
    Jørn Dahl-Stamnes, Dec 9, 2006
    #3
  4. Jørn Dahl-Stamnes wrote:
    > Gunnar Hjalmarsson wrote:
    >>Jørn Dahl-Stamnes wrote:
    >>>How can a perl script find the absolute path to itself?
    >>>
    >>>I have tried to do a 'print $0;', but $0 only tells me the command used
    >>>to invoke the script.
    >>>
    >>>What I need to find is the absolute path to the directory where the perl
    >>>script is located.

    >>
    >> print $0 =~ /(.+)\//;

    >
    > $0 does not contain the absolute path. If I do a:
    >
    > cd ~/some_dir
    > ./some_perl_script.pl
    >
    > $0 will contain "./some_perl_script.pl".
    >
    > What I need is to get exactly the same as the pwd command prints out.


    Then, maybe, that is what you should use:

    print qx(pwd);

    >>Or more portable:
    >>
    >> use FindBin '$Bin';
    >> print $Bin;

    >
    > Seems like I need to download some kind of module. FindBin is not installed
    > at my system.


    Strange, since it's part of the standard Perl distribution.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Dec 9, 2006
    #4
  5. Jørn Dahl-Stamnes

    kens Guest

    Jørn Dahl-Stamnes wrote:
    > How can a perl script find the absolute path to itself?
    >
    > I have tried to do a 'print $0;', but $0 only tells me the command used to
    > invoke the script.
    >
    > What I need to find is the absolute path to the directory where the perl
    > script is located.
    >
    > --
    > Jørn Dahl-Stamnes
    > http://www.dahl-stamnes.net/dahls/


    You do not mention which OS you are using.
    If you are on a Win32 system, you can use the Win32::GetFullPathName
    instead of FindBin. Of course it would be less portable.

    use strict;
    use warnings;
    use Win32;
    use File::Basename;
    my $tmp = dirname( "$0" );
    my $runDir = Win32::GetFullPathName($tmp);

    HTH, Ken
     
    kens, Dec 9, 2006
    #5
  6. Gunnar Hjalmarsson wrote:

    > Then, maybe, that is what you should use:
    >
    > print qx(pwd);


    THANKS... :)

    >>>Or more portable:
    >>>
    >>> use FindBin '$Bin';
    >>> print $Bin;

    >>
    >> Seems like I need to download some kind of module. FindBin is not
    >> installed at my system.

    >
    > Strange, since it's part of the standard Perl distribution.


    Yes... I agree. The machine is using FC4.

    --
    Jørn Dahl-Stamnes
    http://www.dahl-stamnes.net/dahls/
     
    Jørn Dahl-Stamnes, Dec 9, 2006
    #6
  7. kens wrote:

    >
    > Jørn Dahl-Stamnes wrote:
    >> How can a perl script find the absolute path to itself?
    >>
    >> I have tried to do a 'print $0;', but $0 only tells me the command used
    >> to invoke the script.
    >>
    >> What I need to find is the absolute path to the directory where the perl
    >> script is located.
    >>
    >> --
    >> Jørn Dahl-Stamnes
    >> http://www.dahl-stamnes.net/dahls/

    >
    > You do not mention which OS you are using.


    Sorry. That was a *big* mistake of me. I'm using Fedora Core 4.

    --
    Jørn Dahl-Stamnes
    http://www.dahl-stamnes.net/dahls/
     
    Jørn Dahl-Stamnes, Dec 9, 2006
    #7
  8. Jørn Dahl-Stamnes

    Tintin Guest

    "Jørn Dahl-Stamnes" <> wrote in message
    news:...
    > Gunnar Hjalmarsson wrote:
    >
    >> Jørn Dahl-Stamnes wrote:
    >> Or more portable:
    >>
    >> use FindBin '$Bin';
    >> print $Bin;

    >
    > Seems like I need to download some kind of module. FindBin is not
    > installed
    > at my system.


    If you have FC4, then your Perl installation is broken if FindBin is
    missing. Show us the exact error message you received.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    Tintin, Dec 9, 2006
    #8
  9. Jørn Dahl-Stamnes

    Tintin Guest

    "Jørn Dahl-Stamnes" <> wrote in message
    news:...
    > Gunnar Hjalmarsson wrote:
    >
    >> Then, maybe, that is what you should use:
    >>
    >> print qx(pwd);

    >
    > THANKS... :)


    The above will give you incorrect results. You originally stated "How can a
    perl script find the absolute path to itself?"

    Running pwd will just return your current working directory, which may or
    may not be the same path as where the Perl script lives.

    The correct answer is still to use the FindBin module, however, we'll need
    to work out why your Perl installation is broken first.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    Tintin, Dec 9, 2006
    #9
  10. Jørn Dahl-Stamnes

    Joe Smith Guest

    Jørn Dahl-Stamnes wrote:

    >> You do not mention which OS you are using.

    >
    > Sorry. That was a *big* mistake of me. I'm using Fedora Core 4.


    mathras> cat /etc/redhat-release
    Fedora Core release 4 (Stentz)
    mathras> locate FindBin
    /usr/lib/perl5/5.8.6/FindBin.pm
     
    Joe Smith, Dec 9, 2006
    #10
  11. Tintin wrote:

    >
    > "Jørn Dahl-Stamnes" <> wrote in
    > message news:...
    >> Gunnar Hjalmarsson wrote:
    >>
    >>> Jørn Dahl-Stamnes wrote:
    >>> Or more portable:
    >>>
    >>> use FindBin '$Bin';
    >>> print $Bin;

    >>
    >> Seems like I need to download some kind of module. FindBin is not
    >> installed
    >> at my system.

    >
    > If you have FC4, then your Perl installation is broken if FindBin is
    > missing. Show us the exact error message you received.


    Strange... it works now. Perhaps I had some kind of typo-error that I did
    not see.

    Thanks to all.

    --
    Jørn Dahl-Stamnes
    http://www.dahl-stamnes.net/dahls/
     
    Jørn Dahl-Stamnes, Dec 10, 2006
    #11
  12. Tintin wrote:

    >
    > "Jørn Dahl-Stamnes" <> wrote in
    > message news:...
    >> Gunnar Hjalmarsson wrote:
    >>
    >>> Then, maybe, that is what you should use:
    >>>
    >>> print qx(pwd);

    >>
    >> THANKS... :)

    >
    > The above will give you incorrect results. You originally stated "How can
    > a perl script find the absolute path to itself?"
    >
    > Running pwd will just return your current working directory, which may or
    > may not be the same path as where the Perl script lives.


    I found out this morning that it would not work as expected. It did work
    when I did a './script.pl', but it did not work when the perl script was
    invoked through cron.

    > The correct answer is still to use the FindBin module, however, we'll need
    > to work out why your Perl installation is broken first.


    Seems to work after all. :)
    --
    Jørn Dahl-Stamnes
    http://www.dahl-stamnes.net/dahls/
     
    Jørn Dahl-Stamnes, Dec 10, 2006
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    193
  2. lucy
    Replies:
    6
    Views:
    177
    Michele Dondi
    Sep 3, 2004
  3. Replies:
    10
    Views:
    228
    Ted Zlatanov
    Aug 15, 2006
  4. andipfaff
    Replies:
    6
    Views:
    197
    andipfaff
    May 13, 2008
  5. Jack
    Replies:
    3
    Views:
    673
    Tad J McClellan
    Dec 22, 2008
Loading...

Share This Page