(scripting) How to pass arguments and return values...

Features wanted...
Post Reply
drewkeller
Posts: 33
Joined: 01 Nov 2008 05:29

(scripting) How to pass arguments and return values...

Post by drewkeller »

I could not think of a good way to search the forum for this question and I didn't see anything in the help about it.

I think it would be very useful if the scripting language implemented a means to pass arguments and return values to/from user subroutines/functions (aka scripts, I guess) without having to use global variables. (Maybe it does and I'm missing something major.)

For example, here is a simple script that gets the age of a file in seconds (perhaps this is not the best way to determine that, but I guess it works as an example):

Code: Select all

"_GetFileAge"
	global $inFile;        // function inputs
	global $fileAge;       // function outputs
	global $tempDir;
	$timeFile = $tempDir . "time.txt";
	writefile($timeFile, "Check modified timestamp to get UTC offset", "o");
	$tempModified = Property( "Write", $timeFile);
	$inModified = Property( "Write", $inFile);
	$fileAge = datediff($inModified, $tempModified, "s");

// how to call that function.... you have to use global variables for the inputs and the output
	global $inFile, $fileAge; 
	$inFile = "myfile.foo"; 
	sub _GetFileAge;
If we had a means to pass arguments and return values, it could look something like...

Code: Select all

"_GetFileAge($inFile)"
	global $tempDir;
	$timeFile = $tempDir . "time.txt";
	writefile($timeFile, "Check modified timestamp to get UTC offset", "o");
	$tempModified = Property( "Write", $timeFile);
	$inModified = Property( "Write", $inFile);
	return datediff($inModified, $tempModified, "s");

// how to call that function....
	$fileAge = _GetFileAge("myfile.foo");

Maybe it's just me, but this second way is much more readable and less spaghetti-ish. For short functions like this, a significant amount of code is written just to pass data. In a recent script I wrote, I would have used a number of one or two line subroutines if I could have, but instead, I copied the same lines to multiple places in the code (bleh).

Code: Select all

// the function
"_JoinLines($text)"
	return regexreplace($text, "(\r\n|\r|\n)", " ");

// calling it
	$foo = _JoinLines($bar);

admin
Site Admin
Posts: 60602
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: (scripting) How to pass arguments and return values...

Post by admin »

Yes, is planned since long.

Post Reply