Skip to content

Commit 22c0258

Browse files
committed
perl: more general/robust function calls
1 parent f815663 commit 22c0258

File tree

6 files changed

+31
-25
lines changed

6 files changed

+31
-25
lines changed

+stdlib/+perl/device.m

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,16 @@
22

33
function [r, cmd] = device(file)
44

5-
c = '($f=shift) && -e $f or exit 1; print +(stat $f)[0]';
6-
if ispc()
7-
k = sprintf('"%s"', c);
8-
else
9-
k = sprintf('''%s''', c);
10-
end
11-
125
r = uint64.empty;
136

147
exe = stdlib.perl_exe();
158
if stdlib.strempty(exe)
169
return
1710
end
1811

19-
cmd = sprintf('"%s" -e %s "%s"', exe, k, file);
12+
c = stdlib.perl.perl2cmd('($f=shift) && -e $f or exit 1; print +(stat $f)[0]');
13+
14+
cmd = sprintf('"%s" -e %s "%s"', exe, c, file);
2015

2116
[s, r] = system(cmd);
2217
if s == 0

+stdlib/+perl/inode.m

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
function [r, cmd] = inode(file)
22

3-
c = '($f=shift) && -e $f or exit 1; print +(stat $f)[1]';
4-
if ispc()
5-
k = sprintf('"%s"', c);
6-
else
7-
k = sprintf('''%s''', c);
8-
end
9-
103
r = uint64.empty;
114

125
exe = stdlib.perl_exe();
136
if stdlib.strempty(exe)
147
return
158
end
169

17-
cmd = sprintf('"%s" -e %s "%s"', exe, k, file);
10+
c = stdlib.perl.perl2cmd('($f=shift) && -e $f or exit 1; print +(stat $f)[1]');
11+
12+
cmd = sprintf('"%s" -e %s "%s"', exe, c, file);
1813

1914
[s, r] = system(cmd);
2015
if s == 0

+stdlib/+perl/perl2cmd.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
%% PERL2CMD prepare Perl cmd for Matlab sprintf
2+
%
3+
% Windows wants double quotes, Unix wants single quotes
4+
5+
function c = perl2cmd(p)
6+
7+
if ispc()
8+
c = sprintf('"%s"', p);
9+
else
10+
c = sprintf('''%s''', p);
11+
end
12+
13+
end

+stdlib/+perl/samepath.m

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
function [r, cmd] = samepath(file1, file2)
22

3-
c = '(@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)';
4-
if ispc()
5-
k = sprintf('"%s"', c);
6-
else
7-
k = sprintf('''%s''', c);
8-
end
9-
103
r = logical.empty;
114

125
exe = stdlib.perl_exe();
136
if stdlib.strempty(exe)
147
return
158
end
169

17-
cmd = sprintf('"%s" -e %s "%s" "%s"', exe, k, file1, file2);
10+
c = stdlib.perl.perl2cmd('(@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)');
11+
12+
cmd = sprintf('"%s" -e %s "%s" "%s"', exe, c, file1, file2);
1813

1914
s = system(cmd);
2015

+stdlib/perl_version.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
%% PERL_VERSION get the Perl version used by MATLAB
22
% cached for speed
3+
% to clear cache do "clear functions"
34
%
45
%%% Output
56
% * v: 1x3 vector of major, minor, micro version e.g. Perl 5.32.1 = [5, 32, 1]
@@ -20,7 +21,7 @@
2021
exe = stdlib.perl_exe();
2122

2223
if ~stdlib.strempty(exe)
23-
cmd = sprintf('"%s" -e "print $^V"', exe);
24+
cmd = sprintf('"%s" -e %s', exe, stdlib.perl.perl2cmd('print $^V'));
2425

2526
[s, r] = system(cmd);
2627

test/TestPlatform.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ function test_platform_tell(tc)
5151
end
5252

5353

54+
function test_perl(tc)
55+
tc.verifyNotEmpty(stdlib.perl_exe())
56+
tc.verifyNotEmpty(stdlib.perl_version())
57+
tc.verifyTrue(stdlib.has_perl(), "Matlab docs indicate that Perl should always be available")
58+
end
59+
60+
5461
function test_is_cygwin(tc)
5562
tc.verifyFalse(stdlib.is_cygwin())
5663
end

0 commit comments

Comments
 (0)