Skip to content

Commit a610f1a

Browse files
committed
samepath: perl backend
perl backend add has_perl, perl_version perl: use -e calls
1 parent 8f26ec1 commit a610f1a

File tree

12 files changed

+101
-5
lines changed

12 files changed

+101
-5
lines changed

+stdlib/+perl/device.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function [r, cmd] = device(file)
2+
3+
cmd = sprintf('''%s'' -e ''($f=shift) && -e $f or exit 1; print +(stat $f)[0]'' ''%s''', stdlib.perl_exe(), file);
4+
5+
[s, r] = system(cmd);
6+
if s == 0
7+
r = str2double(r);
8+
else
9+
r = [];
10+
end
11+
12+
r = uint64(r);
13+
14+
end

+stdlib/+perl/inode.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function [r, cmd] = inode(file)
2+
3+
cmd = sprintf('''%s'' -e ''($f=shift) && -e $f or exit 1; print +(stat $f)[1]'' ''%s''', stdlib.perl_exe(), file);
4+
5+
[s, r] = system(cmd);
6+
if s == 0
7+
r = str2double(r);
8+
else
9+
r = [];
10+
end
11+
12+
r = uint64(r);
13+
14+
end

+stdlib/+perl/samepath.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function [r, cmd] = samepath(file1, file2)
2+
3+
cmd = sprintf('''%s'' -e ''(@ARGV==2) or exit 1; @s1 = stat shift or exit 1; @s2 = stat shift or exit 1; exit(($s1[0]==$s2[0] && $s1[1]==$s2[1]) ? 0 : 1)'' "%s" "%s"', ...
4+
stdlib.perl_exe(), file1, file2);
5+
6+
s = system(cmd);
7+
8+
r = s == 0;
9+
10+
end

+stdlib/Backend.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
classdef Backend < matlab.mixin.SetGet
22

33
properties (Constant)
4-
optionalBackends = ["python", "dotnet", "java"]
4+
optionalBackends = ["perl", "python", "dotnet", "java"]
55
namespace = "stdlib"
66
end
77

+stdlib/device.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
function [i, b] = device(file, backend)
1010
arguments
1111
file
12-
backend (1,:) string = ["java", "python", "sys"]
12+
backend (1,:) string = ["java", "perl", "python", "sys"]
1313
end
1414

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

+stdlib/has_perl.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
%% HAS_PERL checks if Perl is available in the current environment.
2+
3+
function y = has_perl()
4+
5+
y = ~isempty(stdlib.perl_version());
6+
7+
end

+stdlib/inode.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
function [i, b] = inode(file, backend)
1010
arguments
1111
file string
12-
backend (1,:) string = ["java", "python", "sys"]
12+
backend (1,:) string = ["java", "perl", "python", "sys"]
1313
end
1414

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

+stdlib/perl_exe.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function exe = perl_exe()
2+
3+
persistent perle
4+
5+
if ~isempty(perle)
6+
exe = perle;
7+
return
8+
end
9+
10+
cwd = fileparts(mfilename("fullpath"));
11+
12+
[r, s] = perl(cwd + "/private/executable.pl");
13+
14+
if s == 0 && isfile(r)
15+
exe = r;
16+
perle = r;
17+
end
18+
19+
end

+stdlib/perl_version.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
%% PERL_VERSION get the Perl version used by MATLAB
2+
%
3+
%%% Output
4+
% * v: 1x3 vector of major, minor, micro version e.g. Perl 5.32.1 = [5, 32, 1]
5+
6+
function v = perl_version()
7+
8+
persistent perlv perlv_cached
9+
10+
if isempty(perlv_cached)
11+
perlv_cached = false;
12+
elseif perlv_cached
13+
v = perlv;
14+
return
15+
end
16+
17+
cmd = sprintf('"%s" -e "print $^V"', stdlib.perl_exe());
18+
19+
[s, r] = system(cmd);
20+
21+
if s == 0
22+
v = sscanf(r, 'v%d.%d.%d').';
23+
else
24+
v = [];
25+
end
26+
27+
% cache the result - even if empty -- because the check takes up to 1000 ms say on HPC
28+
perlv = v;
29+
perlv_cached = true;
30+
31+
end

+stdlib/private/executable.pl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print $^X;

0 commit comments

Comments
 (0)