Skip to content

Commit e8af8ba

Browse files
committed
perl_{exe,version}: more robust
1 parent 548917b commit e8af8ba

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

+stdlib/normalize.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
function [n, b] = normalize(file, backend)
1414
arguments
1515
file string
16-
backend (1,:) string = ["java", "python", "native"]
16+
backend (1,:) string = ["native", "java", "python"]
1717
end
1818

1919
o = stdlib.Backend(mfilename(), backend);

+stdlib/perl_exe.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
if s == 0 && isfile(r)
1818
exe = r;
1919
perle = r;
20+
else
21+
exe = string.empty;
2022
end
2123

2224
end

+stdlib/perl_version.m

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@
1515
return
1616
end
1717

18-
cmd = sprintf('"%s" -e "print $^V"', stdlib.perl_exe());
18+
v = [];
1919

20-
[s, r] = system(cmd);
20+
exe = stdlib.perl_exe();
2121

22-
if s == 0
23-
v = sscanf(r, 'v%d.%d.%d').';
24-
else
25-
v = [];
22+
if ~stdlib.strempty(exe)
23+
cmd = sprintf('"%s" -e "print $^V"', exe);
24+
25+
[s, r] = system(cmd);
26+
27+
if s == 0
28+
v = sscanf(r, 'v%d.%d.%d').';
29+
end
2630
end
2731

2832
% cache the result - even if empty -- because the check takes up to 1000 ms say on HPC

+stdlib/private/executable.pl

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,24 @@
33
# we do not use $Config{perlpath} as that's a build-time variable and is thus
44
# incorrect when a buildbot was used e.g. Windows Perl from Matlab
55
#
6-
# https://perldoc.perl.org/Cwd#abs_path
7-
# abs_path is effectively realpath(3)
6+
# Cwd::abs_path is not appropriate as it does not actually work like realpath(3)
7+
#
8+
# does not interact with filesystem:
9+
# https://perldoc.perl.org/File::Spec#file_name_is_absolute
10+
#
11+
# works like which() but platform-independent
12+
# https://perldoc.perl.org/IPC::Cmd#$path-=-can_run(-PROGRAM-);
13+
14+
use File::Spec;
15+
use IPC::Cmd 'can_run';
16+
17+
my $perl_exe = $^X;
18+
my $perl_path;
819

9-
use Cwd 'abs_path';
20+
if (File::Spec->file_name_is_absolute($perl_exe)) {
21+
$perl_path = $perl_exe;
22+
} else {
23+
$perl_path = can_run($perl_exe);
24+
}
1025

11-
print abs_path($^X);
26+
print $perl_path;

0 commit comments

Comments
 (0)