7

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David B
  • 29,258
  • 50
  • 133
  • 186
  • Possible duplicate of [How do I find a user's home directory in Perl?](https://stackoverflow.com/questions/1475357/how-do-i-find-a-users-home-directory-in-perl) – Cœur Jul 10 '18 at 14:13

6 Answers6

9

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

Wooble
  • 87,717
  • 12
  • 108
  • 131
  • 2
    Breaks on `~/a filename with spaces.txt`. – j_random_hacker Oct 28 '10 at 14:38
  • 1
    @Ven'Tatsu: So it does! But weirdly, create 2 files, `abcd.txt` and `e gh.txt`. `glob "\Qabc?.txt"` returns just `abc?.txt` (i.e. unexpanded, `?` doesn't match `d`) while `glob "\Qe g?.txt"` returns `e gh.txt` (i.e. the `?` matches `h` now for some reason). My brain hurts. – j_random_hacker Oct 28 '10 at 22:37
  • @j_random_hacker, strange, `bash` and `csh` shell expansion treats the `\?` that results from `quotemeta` in both cases as a literal `?`, not match any one character. I don't know why `perl` is giving differing results. – Ven'Tatsu Oct 29 '10 at 16:02
  • 2
    j_random_hacker, the [`glob` documentation](http://p3rl.org/glob) clearly says that the function splits on whitespace. Therefore you must quote it. `glob '~/a\ filename\ with\ spaces.txt'` works as intended. – daxim Nov 02 '10 at 12:03
6

The glob function understands standard Unix file globbing.

my $file = glob('~/Documents/file.txt');
Dave Cross
  • 68,119
  • 3
  • 51
  • 97
6

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

use File::HomeDir;
print File::HomeDir->my_home;
Dave Sherohman
  • 45,363
  • 14
  • 64
  • 102
  • I except this as my original phrasing of the question was not that bright. For my 'true' question - see update 2 at the OP. – David B Nov 01 '10 at 07:02
4

Try to replace ~ with $ENV{HOME}

Bruno
  • 119,590
  • 31
  • 270
  • 376
3

Use angle-quotes, not double-quotes:

open(my $fh, "< :crlf :encoding(cp1252)",
              <~/Documents/file.txt>)
    || die "couldn't open Winblose text file ~/Documents/file.txt: $!";

The <xxx> iteration operator, which people usually think of as syntactic sugar for the readline function, instead calls the glob function if there are shell metachars in xxx, and tilde count as one of those.

You might prefer an open mode of <:encoding(Latin1). It just depends on the file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tchrist
  • 78,834
  • 30
  • 123
  • 180
3

From Bruno's comment, you could do it this way:

$path =~ s/^~(\w*)/ ( getpwnam( $1 || $ENV{USER} ))[7] /e;

The e flag replaces the expression with the result of executing or evaluating the replace expression.

Axeman
  • 29,660
  • 2
  • 47
  • 102