in reply to absolute pathname

use Cwd "abs_path"; my $path = abs_path($0);

Works for me ^^

I'm so adjective, I verb nouns!

chomp; # nom nom nom

Replies are listed 'Best First'.
Re^2: absolute pathname
by maletin (Sexton) on Aug 12, 2008 at 23:29 UTC

    thank you. here is the difference between abs_path and rel2abs():

    $ cd $HOME $ ln -s /tmp softlink $ perl softlink/mylog.pl
    main:: path/mylog.pl (23)> abs_path=/tmp/mylog.pl main:: path/mylog.pl (26)> rel2abs=/home/maletin/softlink/mylog.pl MyLog::init /home/maletin/MyLog.pm (11)> INIT MyLog::path /home/maletin/MyLog.pm (16)> /home/maletin/MyLog.pm

    $ cat /tmp/mylog.pl

    #! perl -w # mylog.pl # using a package that print it's absolute filename. use strict; use warnings; use Log::Log4perl; use Cwd qw( abs_path ); use lib $ENV{HOME}; # location of MyLog.pm use MyLog; my $conf = q( log4perl.category.Log = INFO, Screen log4perl.appender.Screen = Log::Log4perl::Appender::Screen log4perl.appender.Screen.layout = Log::Log4perl::Layout::PatternLayout log4perl.appender.Screen.layout.ConversionPattern = %l> %m %n ); Log::Log4perl->init( \$conf ); my $log4perl_logger = Log::Log4perl->get_logger('Log'); my $abs_path = abs_path($0); $log4perl_logger->info("abs_path=$abs_path"); my $rel2abs = File::Spec->rel2abs($0); $log4perl_logger->info("rel2abs=$rel2abs"); MyLog->init($log4perl_logger); MyLog->path();

    $ cat $HOME/MyLog.pm

    package MyLog; use strict; use warnings; my $my_logger = undef; sub init { my ( $class, $logger ) = @_; $my_logger = $logger; $my_logger->info('INIT'); } sub path { my $path = ( eval { caller(0) } )[1]; $my_logger->info($path); return $path; } 1;

    but because of Windows, i will use the version with "softlink" in the filename.
Re^2: absolute pathname
by ikegami (Patriarch) on Aug 12, 2008 at 23:24 UTC
    Not for me
    D:\>perl c:script.pl D:\C:script.pl

    File::Spec::Win32's rel2abs is a wrapper around Cwd that solves this problem.