diff 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
line wrap: on
line diff
--- a/scripts/miscellaneous/perl.m	Sat Jul 09 10:22:57 2022 -0700
+++ b/scripts/miscellaneous/perl.m	Sat Jul 09 11:11:53 2022 -0700
@@ -36,27 +36,32 @@
 ## @seealso{system, python}
 ## @end deftypefn
 
-function [output, status] = perl (scriptfile = "-e ''", varargin)
+function [output, status] = perl (scriptfile, varargin)
 
-  ## VARARGIN is initialized to {}(1x0) if no additional arguments are
-  ## supplied, so there is no need to check for it, or provide an
-  ## initial value in the argument list of the function definition.
+  if (nargin < 1)
+    print_usage ();
+  endif
 
-  if (ischar (scriptfile)
-      && (   (nargin == 1 && ! isempty (scriptfile))
-          || (nargin != 1 && iscellstr (varargin))))
-    if (! strcmp (scriptfile(1:2), "-e"))
-      ## Attempt to find file in loadpath.  No effect for absolute filenames.
-      scriptfile = file_in_loadpath (scriptfile);
-    endif
+  if (! ischar (scriptfile) || isempty (scriptfile))
+    error ("perl: SCRIPTFILE must be a non-empty string");
+  endif
+  if (nargin > 1 && ! iscellstr (varargin))
+    error ("perl: ARGUMENTS must be strings");
+  endif
 
-    [status, output] = system (["perl " scriptfile ...
-                                sprintf(" %s", varargin{:})]);
-  else
-    error ("perl: invalid arguments");
+  if (numel (scriptfile) < 2 || ! strcmp (scriptfile(1:2), "-e"))
+    ## Attempt to find file in loadpath.  No effect for absolute filenames.
+    scriptfile = file_in_loadpath (scriptfile);
   endif
 
+  [status, output] = system (["perl " scriptfile ...
+                              sprintf(" %s", varargin{:})]);
+
 endfunction
 
 
-%!error <invalid arguments> perl (123)
+## Test input validation
+%!error <Invalid call> perl ()
+%!error <SCRIPTFILE must be a non-empty string> perl (123)
+%!error <SCRIPTFILE must be a non-empty string> perl ("")
+%!error <ARGUMENTS must be strings> perl ("perlfile", 123)