Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Windows - dirname(__FILE__)

by demichi (Sexton)
on Apr 11, 2014 at 11:33 UTC ( #1081949=perlquestion: print w/ replies, xml ) Need Help??
demichi has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have the following code.

use strict; use warnings; use File::Basename; my $script_dirname = dirname(__FILE__); print "dirname: $script_dirname\n";

When I run the script I get the following output:

C:\Data\scripting\misc> dirname: C:\Data\scripting\misc C:\Data\scripting\misc>

This is what I expect. I get the full path of the directory. All is good.

However when I run the script with the interpreter I get the following:

C:\Data\scripting\misc>perl dirname: . C:\Data\scripting\misc>

I just get the "." for the current directory.

Why is this the case? How can I always get easily the full path of the directoy where my script is running - no matter how the user calls the script?

(It must work on Windows and would be nice if this also runs on Unix).

Thanks for your help.

regards deMichi

Comment on Windows - dirname(__FILE__)
Select or Download Code
Replies are listed 'Best First'.
Re: Windows - dirname(__FILE__)
by demichi (Sexton) on Apr 11, 2014 at 11:51 UTC
    Might that be an idea?
    use strict; use warnings; use Cwd 'abs_path'; use File::Basename; my $script_dirname = dirname(abs_path($0)); print "dirname: $script_dirname\n";


    C:\Data\scripting\misc> dirname: C:/Data/scripting/misc C:\Data\scripting\misc>perl dirname: C:\Data\scripting\misc C:\Data\scripting\misc>

    Just a bit strange for me that it change / to \ when using the interpreter ...

Re: Windows - dirname(__FILE__)
by choroba (Canon) on Apr 11, 2014 at 12:31 UTC
    Are you looking for FindBin?
    use FindBin; print "Script running from ", $FindBin::Bin, "\n";
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Windows - dirname(__FILE__) ( Path::Tiny::path(__FILE__)->realpath->parent
by Anonymous Monk on Apr 11, 2014 at 21:46 UTC
    use Path::Tiny qw/ path /; my $thisfile = path( __FILE__ )->realpath; my $thisdir = path( $thisfile )->parent;
Re: Windows - dirname(__FILE__)
by fishy (Scribe) on Apr 11, 2014 at 16:48 UTC
    While your script don't chdir()
    #!/usr/bin/perl use strict; use warnings; use Cwd; my $script_dirname = cwd; print "dirname: $script_dirname\n";

      Sadly, this won't work when script is called from another directory by full path:

      $ pwd /home/aitap $ cat /tmp/ #!/usr/bin/perl use strict; use warnings; use Cwd; my $script_dirname = cwd; print "dirname: $script_dirname\n"; use Cwd 'abs_path'; use File::Basename; $script_dirname = dirname(abs_path($0)); print "script dirname: $script_dirname\n"; $ perl /tmp/ dirname: /home/aitap script dirname: /tmp

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1081949]
Approved by marto
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2015-11-26 00:04 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (693 votes), past polls