Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make Perl recognize paths with '~'?

Possible Duplicate:
How do I find a user's home directory in Perl?

I'm running Ubuntu.

Whenever I pass a Perl script a path that starts with ~ (e.g. ~/Documents/file.txt) it fails finding it. I must pass the canonical path (e.g. /home/dave/Documents/file.txt).

Why is that?

Can I make Perl recognize ~ paths?

UPDATE

All the suggested solutions include changing the code in the scripts. I would like for a solution that would not involve any changes to the scripts themselves (since not all of them are mine). Perhaps something in the way Bash works?

The updated version of the question was posted at Super User.

like image 362
David B Avatar asked Oct 28 '10 14:10

David B


People also ask

How do I assign a path to a variable in Perl?

$path = $ENV{'PATH'}; As you may remember, %ENV is a special hash in Perl that contains the value of all your environment variables. Because %ENV is a hash, you can set environment variables just as you'd set the value of any Perl hash variable.

How do I get the directory path in Perl?

The dirname() method in Perl is used to get the directory of the folder name of a file.


2 Answers

You can pass the path to glob to get it expanded.

like image 163
Wooble Avatar answered Oct 14 '22 08:10

Wooble


For a reliable (and easy) cross-platform method, try File::HomeDir:

use File::HomeDir;
print File::HomeDir->my_home;
like image 31
Dave Sherohman Avatar answered Oct 14 '22 07:10

Dave Sherohman