Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Windows - dirname(__FILE__)

by demichi (Beadle)
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

Replies are listed 'Best First'.
Re: Windows - dirname(__FILE__)
by demichi (Beadle) 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 (Chancellor) 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 (Monk) 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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2017-05-27 01:35 GMT
Find Nodes?
    Voting Booth?