Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make the fits tilde expansion more cross-platform friendly.
Instead of the pwnam etc which is not super portable, we can
use glob(~) to get the home directory.  That should be more portable.

Alternatively we could add a dependency and use File::HomeDir.
  • Loading branch information
d-lamb committed Mar 1, 2018
1 parent 628aa95 commit 2ad3e4f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
12 changes: 6 additions & 6 deletions IO/FITS/FITS.pm
Expand Up @@ -118,7 +118,7 @@ Suffix magic:
@pdls = rfits('file.fits'); # Read primary data and extensions
Tilde expansion:
#guesses home directory (from getpwnam(getlogin||getpwuid($<)))
#expand leading ~ to home directory (using glob())
$pdl = rfits '~/filename.fits';
$hdr = rfits('file.fits',{data=>0}); # Options hash changes behavior
Expand Down Expand Up @@ -347,7 +347,7 @@ sub PDL::rfits {
my $explicit_extension = ($file =~ m/\[\d+\]$/ ? 1 : 0);
$extnum = ( ($file =~ s/\[(\d+)\]$//) ? $1 : 0 );

$file =~ s/^~/((getpwnam(getlogin||getpwuid($<)))[7])/e; #tilde expansion
$file =~ s/^(~)/glob($1)/e; #tilde expansion.

$file = "gunzip -c $file |" if $file =~ /\.gz$/; # Handle compression
$file = "uncompress -c $file |" if $file =~ /\.Z$/;
Expand Down Expand Up @@ -1569,7 +1569,7 @@ Suffix magic:
wfits $pdl, 'filename.fits.Z';
Tilde expansion:
#guesses home directory (from getpwnam(getlogin||getpwuid($<)))
#expand leading ~ to home directory (using glob())
wfits $pdl, '~/filename.fits';
=over 3
Expand Down Expand Up @@ -1872,10 +1872,10 @@ sub PDL::wfits {
}

my ($k, $buff, $off, $ndims, $sz);

local $SIG{PIPE};

$file =~ s/^~/((getpwnam(getlogin||getpwuid($<)))[7])/e; #tilde expansion
$file =~ s/^(~)/glob($1)/e; # tilde expansion

local $SIG{PIPE};

if ($file =~ /\.gz$/) { # Handle suffix-style compression
$SIG{PIPE}= sub {}; # Prevent crashing if gzip dies
Expand Down
7 changes: 5 additions & 2 deletions t/fits.t
Expand Up @@ -319,9 +319,12 @@ SKIP:{

###############################
# Check that tilde expansion works
eval { sequence(3,5,2)->wfits('~/tmp.fits'); };
my $tildefile = cfile('~',"PDL-IO-FITS-test_$$.fits");
eval { sequence(3,5,2)->wfits($tildefile); };
ok(!$@, "wfits tilde expansion didn't fail");
eval { rfits('~/tmp.fits'); };
eval { rfits($tildefile); };
ok(!$@, "rfits tilde expansion didn't fail");
$tildefile =~ s/^(~)/glob($1)/e; #use the same trick as in FITS.pm to resolve this filename.
unlink($tildefile) or warn "Could not delete $tildefile: $!\n"; #clean up.

1;

0 comments on commit 2ad3e4f

Please sign in to comment.