comparison scripts/miscellaneous/perl.m @ 31132:0f4d16af143b

perl.m, python.m: Redo input validation. * perl.m, python.m: Redo input validation to check for correct number of arguments as well as type. Issue more informative error messages when validation fails. Add more BIST tests for input validation.
author Rik <rik@octave.org>
date Sat, 09 Jul 2022 11:11:53 -0700
parents 796f54d4ddbf
children 597f3ee61a48
comparison
equal deleted inserted replaced
31131:6cfc7af8eacc 31132:0f4d16af143b
34 ## @var{scriptfile} is not an absolute filename it is searched for in the 34 ## @var{scriptfile} is not an absolute filename it is searched for in the
35 ## current directory and then in the Octave loadpath. 35 ## current directory and then in the Octave loadpath.
36 ## @seealso{system, python} 36 ## @seealso{system, python}
37 ## @end deftypefn 37 ## @end deftypefn
38 38
39 function [output, status] = perl (scriptfile = "-e ''", varargin) 39 function [output, status] = perl (scriptfile, varargin)
40 40
41 ## VARARGIN is initialized to {}(1x0) if no additional arguments are 41 if (nargin < 1)
42 ## supplied, so there is no need to check for it, or provide an 42 print_usage ();
43 ## initial value in the argument list of the function definition. 43 endif
44 44
45 if (ischar (scriptfile) 45 if (! ischar (scriptfile) || isempty (scriptfile))
46 && ( (nargin == 1 && ! isempty (scriptfile)) 46 error ("perl: SCRIPTFILE must be a non-empty string");
47 || (nargin != 1 && iscellstr (varargin)))) 47 endif
48 if (! strcmp (scriptfile(1:2), "-e")) 48 if (nargin > 1 && ! iscellstr (varargin))
49 ## Attempt to find file in loadpath. No effect for absolute filenames. 49 error ("perl: ARGUMENTS must be strings");
50 scriptfile = file_in_loadpath (scriptfile); 50 endif
51 endif
52 51
53 [status, output] = system (["perl " scriptfile ... 52 if (numel (scriptfile) < 2 || ! strcmp (scriptfile(1:2), "-e"))
54 sprintf(" %s", varargin{:})]); 53 ## Attempt to find file in loadpath. No effect for absolute filenames.
55 else 54 scriptfile = file_in_loadpath (scriptfile);
56 error ("perl: invalid arguments");
57 endif 55 endif
56
57 [status, output] = system (["perl " scriptfile ...
58 sprintf(" %s", varargin{:})]);
58 59
59 endfunction 60 endfunction
60 61
61 62
62 %!error <invalid arguments> perl (123) 63 ## Test input validation
64 %!error <Invalid call> perl ()
65 %!error <SCRIPTFILE must be a non-empty string> perl (123)
66 %!error <SCRIPTFILE must be a non-empty string> perl ("")
67 %!error <ARGUMENTS must be strings> perl ("perlfile", 123)