http://www.perlmonks.org?node_id=755535


in reply to Re^3: XP and slashes?
in thread XP and slashes?

Hmmm... I didn't try that... what worked for me is the somewhat more cumbersome:
my $db_path = ''; for (keys %INC) { if (/parse/i) { $db_path = $INC{$_}; $db_path =~ s/parse\.pm$/db\//i; last; } }
Since the .pm requires the other file it can be found in the %INC hash and the rest follows from that. I'll take a look at your approach since it is more concise than mine.

--hsm

"Never try to teach a pig to sing...it wastes your time and it annoys the pig."

Replies are listed 'Best First'.
Re^5: XP and slashes?
by ikegami (Patriarch) on Apr 05, 2009 at 08:18 UTC
    That fails if the caller also uses (say) Parse::RecDescent. If you did want to use %INC, it would be:
    use Cwd qw( realpath ); use File::Basename qw( dirname ); use File::Spec::Functions qw( catdir ); my $mod = __PACKAGE__; $mod =~ s{::}{/}g; $mod .= '.pm'; $mod = $INC{$mod}; my $module_dir_qfn = dirname(realpath($mod)); my $db_dir_qfn = catdir($module_dir_qfn, 'db');

    But there's no need to waste four lines getting the file name when it's already provided for you.

      Arggggh! Good point! Not just cumbersome, but wrong. Well that is what version numbers are for!! Thanks for the correction.

      --hsm

      "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
      Since File::Spec::Functions doesn't export dirname, what did you mean?

      --hsm

      "Never try to teach a pig to sing...it wastes your time and it annoys the pig."