4 MPSL (Minimum Profit Scripting Language) is a programming language
5 concieved as a scripting engine for the Minimum Profit Text Editor,
6 though it can also be used as a general-purpose programming tool.
11 An MPSL program consists of a sequence of statements which run from top to
12 bottom. Loops, subroutines and other control structures allow you to jump
13 around within the code. It is a free-form language, meaning that you can
14 format and indent it however you like, as whitespace serves to separate
15 tokens. MPSL borrows syntax and concepts from many languages: C, Perl, Java
16 or JavaScript. Statements end in a semi-colon, blocks are enclosed between {
17 and } (curly brackets).
22 Text enclosed between /* and */ is ignored, as in C. It can span multiple
28 Strings are enclosed around single or double quotes:
30 print("This a string");
31 print('This is also a string');
33 However, double quotes give special treatment to backslash-escaped
34 characters, also as in C:
36 print("Hello, world\n"); /* ends with a new line */
37 print('Hello, World\n'); /* prints Hello, World\n literally */
39 Strings can be broken between lines by using backslashes:
44 Non-ASCII characters (Unicode) can be included in strings using the
47 print("Hello, world \x{263A}\n"); /* a smiley follows the greeting */
52 Function and subroutine calls must enclose its arguments between
53 parentheses, and separate them by commas:
55 print("This is ", 'a list ', "of ", 5, " arguments\n");
57 Parentheses are needed even if a function has no arguments.
59 An alternative to this standard way of function calls, new in MSPL 2.x, is
60 what is named the 'inverse' calling syntax, in which the first argument of
61 a function is specified before the function call itself; for example,
63 print('Hello!', "\n");
65 can also be expressed as
69 Which is convenient when calling a long list of functions that process
70 the output of another, like in
72 func3(func2(func1(func0(first_value, arg0), arg1), arg2), arg3)
74 that can be better expressed as
76 first_value->func0(arg0)->func1(arg1)->func2(arg2)->func3(arg3)
81 The number 0, the strings '0' and '' and NULL are all false as boolean
82 values. Any other value is considered true.
84 Variables and data types
85 ------------------------
87 Data types in MPSL match the mpdm (Minimum Profit Data Manager) single,
88 array and hash values. They are very similar to Perl ones. However,
89 variable names don't have a prefix indicating its type as in Perl; they
92 Variables need not be declared (unless for local values, see below); they
93 are created on assignation.
98 A scalar represents a single value. They can be strings, integers or
99 floating point numbers, and they will be converted as required.
105 There are other types of scalar values, as file descriptors and executable
106 values; they will be treated later.
108 Scalar assignation is always by value; by doing
110 a = 100; b = a; b = 5;
112 The a variable still holds its original value of 100.
117 An array represents a list of values. Arrays are zero-indexed. Each
118 element of an array can be of any type (scalar, array, or hash). Array
119 size is dynamic and can grow or shrink at the programmer's wish.
121 workdays = [ 'monday', 'tuesday', 'wednesday', 'thursday', 'friday' ];
123 print(workdays[0], "\n"); /* prints monday */
125 heterogeneous_array = [ 1, "string", [ 1, 2, 3 ], NULL, function(), workdays ];
127 Array assignation is always by reference; by doing
129 a = [ 10, 20, 30 ]; b = a; b[0] = 100;
131 The value for the element a[0] is also 100; a and b both point to the same
132 array. A special-purpose function, clone(), can be used if you want them
133 to be different values.
135 Arrays grow when assigned to an out-of-bounds subscript:
137 ary = [ 0, 1, 2, 3 ];
138 print("size: ", size(ary), "\n"); /* prints size: 4 */
140 print("size: ", size(ary), "\n"); /* prints size: 101 */
142 The range operator can be used to populate an array with consecutive,
143 integer values, as in the example:
145 ary = [ 1 .. 100 ]; /* ary contains 100 elements, from 1 to 100 */
150 A hash, or associative array, represents a set of key/value pairs. Keys and
151 values can be of any type (scalar, array or hash). However, keys are
152 usually scalar values, in particular strings.
156 'tuesday' => 'martes',
157 'wednesday' => 'miercoles',
158 'thursday' => 'jueves',
159 'friday' => 'viernes'
162 print(en2es_workdays['monday'], "\n"); /* prints lunes */
165 'workdays' => workdays,
166 'weekend' => [ 'saturday', 'sunday' ]
169 Hash definitions *must* use the => to mark key/value pairs.
171 Hash assignation is always by reference, as in arrays (in fact, hashes are
172 just a special case of array).
174 Compact access to hash members
175 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
177 Hashes can, and ofter are, used as records. Given the following hash of
182 app['config']['save_on_exit'] = 1;
184 The save_on_exit component can also be accessed by using:
186 app.config.save_on_exit = 1;
188 Please note that variables are *not* automatically created unless the path
189 to them already exist; so 'app' and 'app.config' must exist and be hashes,
190 or the 'save_on_exit' component will not be created and fail silently.
191 This is a common pitfall.
196 A variable name must start with a letter or underscore and can be followed
197 by any number of letters, numbers and underscores. Case is significative,
198 so aaa and AAA and aAa are different symbols.
203 By default, non-existent assigned variables are created as global symbols.
204 Though they are useful, it's not a good programming practice to have all
205 variables as global, so local variables can be used. They are defined by
206 using the local directive:
208 local a; /* creates the local variable a, with NULL value */
209 local b = 100; /* creates the local variable b, with a value of 100 */
211 Local variables have its life limited to the subroutine where they are
212 defined or, if defined inside a block, limited to that block. Inner
213 variables obscure more global ones, as in the example:
215 var = 6; /* global variable var, with a value of 6 */
217 { /* start of block */
218 local var = 10; /* local variable var */
219 print("var: ", var, "\n"); /* prints var: 10 */
220 var = 20; /* local var now has 20 */
221 print("var: ", var, "\n"); /* prints var: 20 */
224 print("var: ", var, "\n"); /* prints var: 6 */
226 Subroutine arguments are treated as local variables.
228 Conditional and looping constructs
229 ----------------------------------
234 MPSL has the usual `if' conditional construct. The condition can be any
235 MPSL expression, and the conditional code must be written between braces
236 if it's more than one instruction, just like in C.
238 /* one instruction */
242 /* more than one instruction (braces needed) */
248 `If' conditionals can also include an else instruction or block:
251 print("You are sane\n");
258 `While' conditionals are exactly as you suppose they are:
260 while (player_is_alive) {
267 As in `if', the loop body needs braces if it has more than one instruction.
272 `Foreach' is a special statement to do something with all elements of an
273 array. The usual construction is as follows:
276 weekdays = [ 'monday', 'tuesday', 'wednesday', 'thurday', 'friday' ];
278 foreach (w, weekdays)
279 print("I hate ", w, "s\n");
281 Each value of the array will be assigned to the variable and the loop body
282 executed. The variable is local to the loop body.
287 Exit from `while' and `foreach' loops can be forced by using `break', just
293 A C-like `for' is also available from version 2.x. For example:
295 for (n = 0; n < 10; n++)
304 To assign a value to a variable, use the = operator. Remember that scalars
305 are always assigned by value, and multiple values (arrays and hashes) are
306 always assigned by reference (in case you need a copy of a multiple value,
307 use the clone() function).
312 The usual +, -, *, /, ** and % operators are used for addition,
313 substraction, multiply, divide, power and modulo, as always. Operands
314 for the modulo operator are treated as integers, and for the rest of them,
315 as real numbers. Operator precedence are like in C; you can always use
316 parentheses to explicitly force your desired precedence.
321 The usual == (equality), != (inequality), > (greater than), < (less than),
322 >= (greater or equal than) and <= (less or equal than) operands can be
328 The 'eq' operator is used for testing string equality, 'ne' for the
329 opposite and ~ is used for concatenating strings.
331 MPSL's scalar values are used for both string and numerical values, so (as
332 in Perl) the string and numeric comparison operators must be different:
335 if (a == b) { /* this is truth */ }
336 if (a eq b) { /* this is false */ }
341 As in C, && (logical and) and || (logical or) are used. They don't return
342 just a boolean value, but the value that matches the condition, so that
346 c = a || b; /* c ends with a value of 100 */
347 c = a && b; /* c ends with a value of 0 */
349 c = a || b; /* c ends with a value of 50 */
350 c = a && b; /* c ends with a value of 100 */
352 MPSL lacks a ternary conditional operator (as the ? : one in C), but it
353 can be easily simulated with && and || operators:
355 /* assigns to c the greatest of a and b */
358 There is also the ! operator, that negates a condition.
363 Immediate operators act directly over variables. The most usual examples
364 are the unary ++ and --, that are used as in C.
367 a++; /* a's value is now 101 */
368 a--; /* back to 100 */
370 Also as in C, this operators can be used as prefixes or suffixes, giving
371 the following result:
374 b = a++; /* b's value is 100, a's value is 101 */
375 c = ++a; /* c's and a's value are both 102 */
377 The well known binary +=, -=, *=, /= and %= also exist:
379 a += 25; /* adds 25 to a */
387 Subroutines (also called functions) are created with the 'sub' keyword:
391 while (!exit_requested) {
400 Subroutines can also accept arguments:
402 sub process_option(o)
407 print("Option is ", o, "\n");
410 Subroutine arguments can be used as local variables (in fact, that is exactly
413 As in assignations, when you pass a multiple value (arrays and hashes) as
414 an argument to a subroutine, it's always done by reference, so modifying
415 it will change the original value:
417 sub dequeue(queue) { local a = pop(queue); do_something_with(a); }
419 my_q = [ 10, 20, 30 ];
420 dequeue(my_q); /* on exit, my_q will contain only [ 10, 20 ] */
425 As every code fragment in MPSL, subroutines always return a value. By
426 default, a subroutine returns the result of the last operation:
428 sub avg(a, b) { a += b; a /= 2; }
430 The value to be returned can be explicitly expressed by using the 'return'
431 keyword, that can also be used to break execution flow:
436 print("Invalid value for b.\n");
443 When calling a subroutine, parentheses are always mandatory, even if it
444 has no arguments. If a subroutine is referenced without its arguments,
445 it's not called, but a pointer to it returned instead:
447 sub sum(a, b) { return a + b; }
449 a = sum(10, 20); /* a == 30 */
450 b = sum; /* b has a pointer to the sum subroutine */
452 c = b(20, 30); /* c == 50, same as calling sum(20, 30) */
454 Anonymous subroutines
455 ~~~~~~~~~~~~~~~~~~~~~
457 The `sub' directive can also be used as an rvalue to define anonymous
458 subroutines. So, the following can be done:
461 push(on_exit_subs, sub { print("Close files\n"); } );
462 push(on_exit_subs, sub { print("Clean up memory\n"); } );
463 push(on_exit_subs, sub { print("Other stuff\n"); } );
465 /* exiting; calling all functions stored in on_exit_subs */
466 foreach (f, on_exit_subs) f();
468 Anonymous functions can also accept arguments:
471 sub (a, b) { print(a, " + ", b, ": ", a + b, "\n"); },
472 sub (a, b) { print(a, " - ", b, ": ", a - b, "\n"); },
473 sub (a, b) { print(a, " * ", b, ": ", a * b, "\n"); },
474 sub (a, b) { print(a, " / ", b, ": ", a / b, "\n"); }
477 foreach (f, math_ops) f(10, 20);
479 This kind of anonymous subroutines are commonly used as callbacks. The
480 following examples use the two-argument version of sort() to sort arrays
483 /* reverse-sort the array (comparing each element backwards) */
484 sorted_ary = sort(unsorted_ary, sub (a, b) { cmp(b, a); } );
486 /* numeric sort (instead of plain ASCII) */
487 sorted_ary = sort(unsorted_ary, sub (a, b) { a - b; } );
492 Subroutines are defined in execution time and *not* in compilation time.
493 This means that you can redefine a subroutine whenever you want, but be
494 aware that you can't use it before the `sub' instruction has been
495 executed. This is a common pitfall:
497 /* call a function before it exists: error */
498 print("Average for 2 and 3 is ", avg(2, 3), "\n");
500 sub avg(a, b) { a += b; a /= 2; }
502 /* now you can use it */
503 print("Average for 4 and 5 is ", avg(4, 5), "\n");
505 It's easier to see subroutine definitions as an assignation of a code
506 block to a variable: you would never access a variable before assigning a
509 Variable number of arguments
510 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
512 If a subroutine is called with more arguments that those specified in the
513 definition, the list of values is stored inside the _ local list, making
514 possible to implement variable argument functions. For example:
519 /* loop all arguments and sum them */
526 This function allows being called with an arbitrary number of arguments, as
530 b = sum(10, 20, 30, 40, 50);
532 Both traditionally named arguments and these unnamed ones can be combined.
533 The _ local array will only be defined if there are 'lost' arguments.
541 The cmp() function can be used to compare two values. It returns a signed
542 number, negative if the first value is lesser than the second, positive
543 on the contrary or zero if they are the equal. It also can be used to
544 compare arrays and hashes:
546 cmp('a', 'b'); /* < 0 ('a' is lesser than 'b') */
547 cmp('aaa', 'aab'); /* < 0 ('aaa' is lesser than 'aab') */
549 [ 1, 2 ]); /* > 0 ([1,2,3] is greater than [1,2]) */
551 [ 1, "a", 3 ]); /* 0 (both arrays are equal) */
553 Arrays are first tested for number of elements (so bigger arrays are
554 greater than arrays with fewer elements), and if both are equal, then each
555 element is tested sequentially.
557 The split() and join() functions can be used to convert strings into
558 arrays and vice-versa. Examples:
560 /* sets a to [ 12, 'abcd', 356, '', 'xxx' ] */
561 local a = split('12:abcd:356::xxx', ':');
563 /* sets b to 12->abcd->356->->xxx */
564 local b = join(a, '->');
566 The split() function can have a special separator, NULL, that makes it
567 explode the string in characters:
569 /* sets c to [ 't', 'h', 'i', 's' ] */
570 local c = split("this");
572 The splice() function can be used to delete, insert or replace a subset of
573 a string with another one. It returns a two element array: one with the
574 newly created string, and another with the deleted part. Examples:
576 local a = "This is a string";
578 /* original string, string to be inserted, position, chars to delete */
579 splice(a, 'not', 8, 0); /* [ 'This is not a string', NULL ] */
580 splice(a, '', 5, 3); /* [ 'This a string', 'is ' ] */
581 splice(a, 'can be', 5, 3); /* [ 'This can be a string', 'is ' ] */
583 The chr() and ord() functions return the character represented by a Unicode
584 number and vice-versa:
586 ord('A'); /* returns 65 (ASCII or Unicode for A) */
587 chr(65); /* return 'A' */
589 The sprintf() function works like any other sprintf() implementation in
590 the world: accepts a format string (with special, %-prefixed placeholders)
591 and a list of arguments, and substitutes them.
593 sprintf("%s: %d", 'ERROR', 10); /* returns 'ERROR: 10' */
594 sprintf("%04X", 16384); /* return '4000' */
596 The sscanf() function can be seen as the opposite of sprintf(): given a
597 format string and a scanning string, extracts a set of strings and returns
600 sscanf("name 100", "%s %d"); /* returns [ 'name', 100 ] */
602 The standard %-commands can be used, as %d, %i, %u, %f and %x, for decimal,
603 unsigned, floating point or hexadecimal numbers, %n, to return a matching
604 offset, and %[, to match an explicit set of matching characters, than can
605 be negated by adding a ^ after the bracket. As a non-standard extension, a
606 special command %S is also implemented, that match a string upto the
607 following substring in the format string. For example:
609 sscanf("key: value", "%S: %S"); /* returns [ 'key', 'value' ] */
610 sscanf("text words 1", "%S%d"); /* returns [ 'text words ', 1 ] */
612 As in the standard, the format can include an optional number of maximum
613 characters and a * to mark the matched string as non-interesting (so it's
616 The sscanf() function also accepts an optional third argument with an
617 offset to start scanning.
622 Arrays can be easily manipulated by using push(), pop(), shift(), adel()
628 /* add three values: ary will contain [ 10, 20, 30, 40 ] */
629 push(ary, 10); push(ary, 20); push(ary, 30); push(ary, 40);
631 /* delete and return last value; ary contains now [ 10, 20, 30 ]
632 and top contains 40 */
635 /* shift() deletes from the beginning; ary contains now [ 20, 30 ] */
638 /* ins() inserts an element in an array; ary contains now [ 20, 5, 30 ] */
641 */ adel() does the opposite, delete values; ary is back [ 20, 30 ] */
642 deleted_item = adel(ary, 1);
644 The seek() function searches sequentially for an exact value in an array,
645 returning its offset if found, or -1 otherwise:
647 weekdays = [ 'monday', 'tuesday', 'wednesday', 'thurday', 'friday' ];
650 seek(weekdays, 'wednesday');
652 The sort() function returns a sorted copy of the array sent as argument:
654 sorted_weekdays = sort(weekdays);
656 The sorting is done with the cmp() function (see above); if another kind of
657 sort is wanted, the 2-argument version must be used, as mentioned
658 previously in the _Subroutines_ section.
660 Whenever a change must be done on every element of an array, the map()
661 function can be useful and much faster than iterating the array using
662 foreach() and storing the changes in another one.
664 The second argument of map() can be an executable value accepting one
665 argument, that will be set to each element of the array. The return
666 value will be used as the new value:
668 /* a function to multiply by 10 its unique argument */
669 sub multiply_by_10(val)
674 /* an array of values */
675 a = [ 1, 5, 3, 7, 30, 20 ];
677 /* a_by_10 will contain [10, 50, 30, 70, 300, 200 ] */
678 a_by_10 = map(a, multiply_by_10);
680 Of course, the executable value can be an anonymous subroutine:
682 /* Creates a zero-filled version of the numbers in a */
683 a_zero_filled = map(a, sub(e) { sprintf("%04d", e); });
685 The second argument of map() can also be a hash to directly
688 /* numerals as keys, names as values */
689 english_nums = { 1 => "one", 2 => "two", 3 => "three" };
691 /* translated_nums will have [ "one", "two", "three" ] */
692 translated_nums = map([ 1, 2, 3 ], english_nums);
694 Another powerful array function is grep(); it matches a regular
695 expression on an array and returns a new one containing all
696 elements that matched:
698 a = [ 1, 9, "mix190", 4, "foo", 9000 ];
700 /* b is [ 9, "mix190", 9000 ] */
703 The second argument of grep() can also be an executable value,
704 that will accept each element of the array on each call. The
705 output array will contain all elements for which the function
708 /* return all elements greater than 10 */
709 a = [ 1, 45, 20, 4, 0, 1000 ];
711 /* b is [ 45, 20, 1000 ] */
712 b = grep(a, sub(e) { e > 10; });
714 If no element were generated, NULL is returned (instead of
717 Other lower level array functions are expand() and collapse().
718 The expand() function opens space in an array:
720 ary = [ 1, 2, 3, 4 ];
722 /* ary will have two more elements at offset 3,
723 containing [ 1, 2, 3, NULL, NULL, 4 ] */
726 On the opposite, collapse() deletes space:
728 /* deletes 3 elements at offset 1: ary is now [ 1, NULL, 4 ] */
731 Take note that expand() and collapse() manipulate the array
732 directly (i.e. they don't return a new array).
737 The hsize() function returns the number of pairs in a hash:
740 num = hsize( { 1 => 'one', 2 => 'two', 3 => 'three' });
742 Take note that size() cannot be used for this purpose, as it returns the
743 number of buckets in the hash (which is probably of no use for you).
745 If the existence of a pair must be tested, the exists() function is handy:
747 h = { 'a' => 1000, 'b' => 0, 'c' => 3000 };
750 if (exists(h, 'b')) print("B exists\n");
752 /* this is not, as 0 does not evaluates as true */
753 if (h['b']) print("B is true\n");
755 The keys() function returns an array with the keys of a hash:
757 /* will contain [ 'a', 'b', 'c' ] */
760 The hdel() function deletes a pair given its key. The deleted value is
763 /* deletes 'c' and returns its value (3000) */
764 c_value = hdel(h, 'c');
766 Type-checking functions
767 ~~~~~~~~~~~~~~~~~~~~~~~
769 There are three functions to test a type of a value: is_exec(), is_hash()
770 and is_array(). They return true if the tested value is executable, a hash
771 or an array, respectively.
773 On this implementation, executable and hash values are also arrays, so if
774 you need to act differently on the type of a value, the correct order is
775 is_exec(), is_hash() and is_array(), as in the following example:
777 if (is_exec(v)) { print("v is executable\n"); }
779 if (is_hash(v)) { print("v is a hash with ", hsize(v), " pairs\n"); }
781 if (is_array(v) { print("v is an array with ", size(v), " elements\n"); }
783 print("v is a scalar\n");
785 Regular expression matching
786 ---------------------------
788 Regular expression matching is done with the regex() function. In the simpler
789 of its forms, it can be used to test if a string matches:
791 /* hours, minutes, seconds */
794 if (regex(v, "/^[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$/"))
795 print("The v variable contains a valid time\n");
797 The regular expression can contain flags; those are `i', for case
798 insensitiveness, `l', to select the last matching instead of the first,
799 `m', to treat the string as a multiline one (i.e., one containing \n
800 characters), where the special ^ and $ markup match beginning and end of
801 lines and not the beginning or end of the string, or `g', to do a global
802 matching (return _all_ matching strings). Flags are written after the
805 Regex() returns the matched string, or an array of matched strings if
806 the `g' flag is used. If no match is found, NULL is returned.
808 v = "The value of PI is 3.1416, more or less";
810 pi = regex(v, "/[0-9]+\.[0-9]+/");
812 If regex() is called without arguments, it returns the last match as an array
813 of two elements; the first contain the offset of the matched strings, and the
814 second, the size in characters of it. If last match was unsuccessful, NULL is
817 /* get the match of the previous example */
821 print(coords[0], ":", coords[1], "\n");
823 Regex() can include an optional third argument, the offset where the regular
824 expression matching must start (instead of 0, the beginning of the string).
825 This can be used to extract many values from the same string:
827 /* lets extract all numbers in the following string: */
828 s = "There is 10 and 35 and 42 in the number 103542!";
831 while (n = regex(s, "/[0-9]+/", offset)) {
837 /* get the coordinates */
840 /* start from there (previous match offset + size) */
841 offset += c[0] + c[1];
844 Though, for this particular example, the `g' flag could be more suitable:
846 /* return all numbers in one step */
847 foreach (i, regex(s, '/[0-9]+/g'))
850 For more complex situations, the second argument of regex() can be an array
851 of regular expressions (instead of a string). Those expressions will tried
852 one after the other on the string, each one using the offset returned by
853 the previous one. If all of them are matched, an array with the matched strings
856 /* match pairs of key=value commands, with many variants */
858 v2 = " KEY2 = value2";
861 /* array regex: a regex for each part */
862 r = regex(v1, [ "/\s*/", /* possible leading blanks (0) */
863 "/[a-z0-9]+/i", /* the key (1) */
864 "/\s*=\s*/", /* optional blanks, the = , more blanks (2) */
865 "/[a-z0-9]+/i" /* the value (3) */
869 /* the key is the element #1 and the value the #3 */
874 Regular expression substitutions
875 --------------------------------
877 The sregex() function can be used to substitute the matching part of a
878 string with another string. The simplest form:
880 /* substitute all characters in password with asterisks */
881 sregex(password, '/./g', '*');
883 The substitution part can also be a hash:
885 /* substitute \n, \r, etc with their equivalents */
886 sregex(var, "/\\[rnt]/g", {
894 /* zero-pad all numbers in var */
895 sregex(var, '/[0-9]+/g', sub (m) {
899 The regular expression can contain flags; those are `i', for
900 case-insensitive matching, and `g', for global replacements (all ocurrences
901 in the string will be replaced, instead of just the first found).
903 Input/Output operations
904 -----------------------
906 File operations are done by opening files with the open() call. It accepts
907 two arguments, the file and the mode:
909 /* open in read mode */
910 local i = open('existingdata.dat', 'r');
912 /* open in write mode (create) */
913 local o = open('newdata.dat', 'w');
915 The opening mode is the same as the one in the fopen() C library call, so
916 other combinations exist.
918 If the file cannot be open or created, NULL is returned.
920 The read() function returns a line read from a file descriptor, or NULL if
921 no more lines can be read (i.e. on EOF). A line is considered from the
922 current file position until an end of line mark (\n or \r\n). All files
923 under MPSL are considered as text ones; before the read() call returns, the
924 string is converted to wide chars (the internal representation of strings
925 in MPSL) by using the current locale definitions, unless a explicit
926 encoding is set by calling encoding() (see below).
928 The opposite of read() is write(); it accepts a file descriptor and a
929 string to be written. As read(), the string is converted from wide chars by
930 using the current locale definitions unless a different encoding() was set.
931 The write() function does not write an end of line, though.
933 Similar functions getchar() and putchar() exist to read and write
934 characters instead of lines. This functions don't do any locale nor
935 encoding conversions.
937 The unlink() function can be used to delete files, chmod() to set the mode
938 and chown() to set the owner.
940 The stat() function returns an array containing information about a file,
941 similar to the C library function of the same name. The returned values
942 are: device number, inode, mode, number of links, uid, gid, rdev, size,
943 atime, mtime and ctime. Take note that not all values has meaning depending
944 on the filesystem or the architecture. The `mode' argument can be used in
945 chmod() and `uid' and `gid' in chown(). If the file does not exist, stat()
946 returns NULL (which is a good way to test for file existence).
948 The glob() function accepts a file spec as the only argument and returns
949 an array of filenames matching it. Directories have a / as the last
950 character. The file spec and the returned files are system-dependent, but
951 always usable by open().
953 The popen() and pclose() function calls are used for piping from / to
954 programs. Pipes can be open for reading ('r' mode), for writing ('w' mode),
955 or bidirectional (by appending '+' to the mode, which can be used to
956 communicate two-way with programs like ispell, smtp servers and such). Once
957 open, the read() and write() functions can be used the same as in plain
960 As mentioned above, the encoding() function can be used to force the
961 reading or writing conversions to be done when doing IO. By default, the
962 current locale is used, and it's the same as calling encoding() without
963 arguments. If an encoding is forced, reading and writing is done by using
964 the selected converter. Take note that support for the `iconv' library must
965 be compiled in; if it's not, a special, ad-hoc converter called "utf-8"
968 Each file can have a different encoding converter and is selected at open()
971 A snippet to convert a file from current locale to iso8859-1 can be:
973 sub convert_to_iso8859_1(fromfile, tofile)
978 /* open the file first */
979 if ((i = open(fromfile, 'r')) == NULL)
982 /* set encoding to iso8859-1 (can fail) */
983 if (encoding("iso8859-1") < 0) {
988 /* create the new file now; it will have iso8859-1
989 encoding as it's the last encoding set. The input
990 file keeps its original encoding */
991 if ((o = open(tofile, 'w')) != NULL) {
1000 /* close input and revert to default encoding */
1007 The load() function can be used to load, compile and execute an MPSL
1008 source file. The file will be searched for using the paths in the INC
1017 Stores the command line arguments sent to the main() function.
1022 Holds an array of paths on disk where source files will be searched
1023 for by the load() command.
1028 Stores the last severe error message (compilation errors, etc.).
1033 Stores the last error message from the system (file not found, etc.).
1035 STDIN, STDOUT, STDERR
1036 ~~~~~~~~~~~~~~~~~~~~~
1038 The famous standard file descriptors. Can be used in read(), write()
1039 and related functions.
1044 The home directory for the current user. It's supposed to be a writable
1045 directory where users can store their configuration and documents.
1050 The application directory.
1055 The last successful charset encoding set by the encoding() function.
1060 Similar to ENCODING, but used only when opening a file for writing and
1061 if ENCODING is not set. After opening, its content is destroyed.
1066 Filled after each file opening in read mode, if a specific charset
1067 encoding could be found. Can be copied onto ENCODING or TEMP_ENCODING.
1072 Stores the MPSL version as a string.
1077 This is an accessor / mutator function that allows to retrieve information
1078 and tune some values for the MPDM (Minimum Profit Data Manager). If called
1079 without arguments, the current values are returned. A hash can be sent as
1080 argument to change some of them. These are the keys:
1082 * count: Total number of values being managed (read only).
1083 * hash_buckets: The default number of buckets a hash will use. It must
1084 be a prime number (read / write).
1086 The default values are the best for normal usage. Of course, you only must
1087 change any of them if you know what you are doing.
1091 /* set a bigger value for hash buckets */
1092 MPDM( { 'hash_buckets' => 127 });
1094 /* dump current setup */
1098 Angel Ortega <angel@triptico.com>