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.
62 The number 0, the strings '0' and '' and NULL are all false as boolean
63 values. Any other value is considered true.
65 Variables and data types
66 ------------------------
68 Data types in MPSL match the mpdm (Minimum Profit Data Manager) single,
69 array and hash values. They are very similar to Perl ones. However,
70 variable names don't have a prefix indicating its type as in Perl; they
73 Variables need not be declared (unless for local values, see below); they
74 are created on assignation.
79 A scalar represents a single value. They can be strings, integers or
80 floating point numbers, and they will be converted as required.
86 There are other types of scalar values, as file descriptors and executable
87 values; they will be treated later.
89 Scalar assignation is always by value; by doing
91 a = 100; b = a; b = 5;
93 The a variable still holds its original value of 100.
98 An array represents a list of values. Arrays are zero-indexed. Each
99 element of an array can be of any type (scalar, array, or hash). Array
100 size is dynamic and can grow or shrink at the programmer's wish.
102 workdays = [ 'monday', 'tuesday', 'wednesday', 'thursday', 'friday' ];
104 print(workdays[0], "\n"); /* prints monday */
106 heterogeneous_array = [ 1, "string", [ 1, 2, 3 ], NULL, function(), workdays ];
108 Array assignation is always by reference; by doing
110 a = [ 10, 20, 30 ]; b = a; b[0] = 100;
112 The value for the element a[0] is also 100; a and b both point to the same
113 array. A special-purpose function, clone(), can be used if you want them
114 to be different values.
116 Arrays grow when assigned to an out-of-bounds subscript:
118 ary = [ 0, 1, 2, 3 ];
119 print("size: ", size(ary), "\n"); /* prints size: 4 */
121 print("size: ", size(ary), "\n"); /* prints size: 101 */
123 The range operator can be used to populate an array with consecutive,
124 integer values, as in the example:
126 ary = [ 1 .. 100 ]; /* ary contains 100 elements, from 1 to 100 */
131 A hash, or associative array, represents a set of key/value pairs. Keys and
132 values can be of any type (scalar, array or hash). However, keys are
133 usually scalar values, in particular strings.
137 'tuesday' => 'martes',
138 'wednesday' => 'miercoles',
139 'thursday' => 'jueves',
140 'friday' => 'viernes'
143 print(en2es_workdays['monday'], "\n"); /* prints lunes */
146 'workdays' => workdays,
147 'weekend' => [ 'saturday', 'sunday' ]
150 Hash definitions *must* use the => to mark key/value pairs.
152 Hash assignation is always by reference, as in arrays (in fact, hashes are
153 just a special case of array).
155 Compact access to hash members
156 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
158 Hashes can, and ofter are, used as records. Given the following hash of
163 app['config']['save_on_exit'] = 1;
165 The save_on_exit component can also be accessed by using:
167 app.config.save_on_exit = 1;
169 Please note that variables are *not* automatically created unless the path
170 to them already exist; so 'app' and 'app.config' must exist and be hashes,
171 or the 'save_on_exit' component will not be created and fail silently.
172 This is a common pitfall.
177 A variable name must start with a letter or underscore and can be followed
178 by any number of letters, numbers and underscores. Case is significative,
179 so aaa and AAA and aAa are different symbols.
184 By default, non-existent assigned variables are created as global symbols.
185 Though they are useful, it's not a good programming practice to have all
186 variables as global, so local variables can be used. They are defined by
187 using the local directive:
189 local a; /* creates the local variable a, with NULL value */
190 local b = 100; /* creates the local variable b, with a value of 100 */
192 Local variables have its life limited to the subroutine where they are
193 defined or, if defined inside a block, limited to that block. Inner
194 variables obscure more global ones, as in the example:
196 var = 6; /* global variable var, with a value of 6 */
198 { /* start of block */
199 local var = 10; /* local variable var */
200 print("var: ", var, "\n"); /* prints var: 10 */
201 var = 20; /* local var now has 20 */
202 print("var: ", var, "\n"); /* prints var: 20 */
205 print("var: ", var, "\n"); /* prints var: 6 */
207 Subroutine arguments are treated as local variables.
209 Conditional and looping constructs
210 ----------------------------------
215 MPSL has the usual `if' conditional construct. The condition can be any
216 MPSL expression, and the conditional code must be written between braces
217 if it's more than one instruction, just like in C.
219 /* one instruction */
223 /* more than one instruction (braces needed) */
229 `If' conditionals can also include an else instruction or block:
232 print("You are sane\n");
239 `While' conditionals are exactly as you suppose they are:
241 while (player_is_alive) {
248 As in `if', the loop body needs braces if it has more than one instruction.
253 `Foreach' is a special statement to do something with all elements of an
254 array. The usual construction is as follows:
257 weekdays = [ 'monday', 'tuesday', 'wednesday', 'thurday', 'friday' ];
259 foreach (w, weekdays)
260 print("I hate ", w, "s\n");
262 Each value of the array will be assigned to the variable and the loop body
263 executed. The variable is local to the loop body.
268 Exit from `while' and `foreach' loops can be forced by using `break', just
277 To assign a value to a variable, use the = operator. Remember that scalars
278 are always assigned by value, and multiple values (arrays and hashes) are
279 always assigned by reference (in case you need a copy of a multiple value,
280 use the clone() function).
285 The usual +, -, *, /, ** and % operators are used for addition,
286 substraction, multiply, divide, power and modulo, as always. Operands
287 for the modulo operator are treated as integers, and for the rest of them,
288 as real numbers. Operator precedence are like in C; you can always use
289 parentheses to explicitly force your desired precedence.
294 The usual == (equality), != (inequality), > (greater than), < (less than),
295 >= (greater or equal than) and <= (less or equal than) operands can be
301 The 'eq' operator is used for testing string equality, 'ne' for the
302 opposite and ~ is used for concatenating strings.
304 MPSL's scalar values are used for both string and numerical values, so (as
305 in Perl) the string and numeric comparison operators must be different:
308 if (a == b) { /* this is truth */ }
309 if (a eq b) { /* this is false */ }
314 As in C, && (logical and) and || (logical or) are used. They don't return
315 just a boolean value, but the value that matches the condition, so that
319 c = a || b; /* c ends with a value of 100 */
320 c = a && b; /* c ends with a value of 0 */
322 c = a || b; /* c ends with a value of 50 */
323 c = a && b; /* c ends with a value of 100 */
325 MPSL lacks a ternary conditional operator (as the ? : one in C), but it
326 can be easily simulated with && and || operators:
328 /* assigns to c the greatest of a and b */
331 There is also the ! operator, that negates a condition.
336 Immediate operators act directly over variables. The most usual examples
337 are the unary ++ and --, that are used as in C.
340 a++; /* a's value is now 101 */
341 a--; /* back to 100 */
343 Also as in C, this operators can be used as prefixes or suffixes, giving
344 the following result:
347 b = a++; /* b's value is 100, a's value is 101 */
348 c = ++a; /* c's and a's value are both 102 */
350 The well known binary +=, -=, *=, /= and %= also exist:
352 a += 25; /* adds 25 to a */
357 Subroutines (also called functions) are created with the 'sub' keyword:
361 while (!exit_requested) {
370 Subroutines can also accept arguments:
372 sub process_option(o)
377 print("Option is ", o, "\n");
380 Subroutine arguments can be used as local variables (in fact, that is exactly
383 As in assignations, when you pass a multiple value (arrays and hashes) as
384 an argument to a subroutine, it's always done by reference, so modifying
385 it will change the original value:
387 sub dequeue(queue) { local a = pop(queue); do_something_with(a); }
389 my_q = [ 10, 20, 30 ];
390 dequeue(my_q); /* on exit, my_q will contain only [ 10, 20 ] */
392 As every code fragment in MPSL, subroutines always return a value. By
393 default, a subroutine returns the result of the last operation:
395 sub avg(a, b) { a += b; a /= 2; }
397 The value to be returned can be explicitly expressed by using the 'return'
398 keyword, that can also be used to break execution flow:
403 print("Invalid value for b.\n");
410 When calling a subroutine, parentheses are always mandatory, even if it
411 has no arguments. If a subroutine is referenced without its arguments,
412 it's not called, but a pointer to it returned instead:
414 sub sum(a, b) { return a + b; }
416 a = sum(10, 20); /* a == 30 */
417 b = sum; /* b has a pointer to the sum subroutine */
419 c = b(20, 30); /* c == 50, same as calling sum(20, 30) */
421 The `sub' directive can also be used as an rvalue to define anonymous
422 subroutines. So, the following can be done:
425 push(on_exit_subs, sub { print("Close files\n"); } );
426 push(on_exit_subs, sub { print("Clean up memory\n"); } );
427 push(on_exit_subs, sub { print("Other stuff\n"); } );
429 /* exiting; calling all functions stored in on_exit_subs */
430 foreach (f, on_exit_subs) f();
432 Anonymous functions can also accept arguments:
435 sub (a, b) { print(a, " + ", b, ": ", a + b, "\n"); },
436 sub (a, b) { print(a, " - ", b, ": ", a - b, "\n"); },
437 sub (a, b) { print(a, " * ", b, ": ", a * b, "\n"); },
438 sub (a, b) { print(a, " / ", b, ": ", a / b, "\n"); }
441 foreach (f, math_ops) f(10, 20);
443 This kind of anonymous subroutines are commonly used as callbacks. The
444 following examples use the two-argument version of sort() to sort arrays
447 /* reverse-sort the array (comparing each element backwards) */
448 sorted_ary = sort(unsorted_ary, sub (a, b) { cmp(b, a); } );
450 /* numeric sort (instead of plain ASCII) */
451 sorted_ary = sort(unsorted_ary, sub (a, b) { a - b; } );
453 Subroutines are defined in execution time and *not* in compilation time.
454 This means that you can redefine a subroutine whenever you want, but be
455 aware that you can't use it before the `sub' instruction has been
456 executed. This is a common pitfall:
458 /* call a function before it exists: error */
459 print("Average for 2 and 3 is ", avg(2, 3), "\n");
461 sub avg(a, b) { a += b; a /= 2; }
463 /* now you can use it */
464 print("Average for 4 and 5 is ", avg(4, 5), "\n");
466 It's easier to see subroutine definitions as an assignation of a code
467 block to a variable: you would never access a variable before assigning a
476 The cmp() function can be used to compare two values. It returns a signed
477 number, negative if the first value is lesser than the second, positive
478 on the contrary or zero if they are the equal. It also can be used to
479 compare arrays and hashes:
481 cmp('a', 'b'); /* < 0 ('a' is lesser than 'b') */
482 cmp('aaa', 'aab'); /* < 0 ('aaa' is lesser than 'aab') */
484 [ 1, 2 ]); /* > 0 ([1,2,3] is greater than [1,2]) */
486 [ 1, "a", 3 ]); /* 0 (both arrays are equal) */
488 Arrays are first tested for number of elements (so bigger arrays are
489 greater than arrays with fewer elements), and if both are equal, then each
490 element is tested sequentially.
492 The split() and join() functions can be used to convert strings into
493 arrays and vice-versa. Examples:
495 /* sets a to [ 12, 'abcd', 356, '', 'xxx' ] */
496 local a = split('12:abcd:356::xxx', ':');
498 /* sets b to 12->abcd->356->->xxx */
499 local b = join(a, '->');
501 The split() function can have a special separator, NULL, that makes it
502 explode the string in characters:
504 /* sets c to [ 't', 'h', 'i', 's' ] */
505 local c = split("this");
507 The splice() function can be used to delete, insert or replace a subset of
508 a string with another one. It returns a two element array: one with the
509 newly created string, and another with the deleted part. Examples:
511 local a = "This is a string";
513 /* original string, string to be inserted, position, chars to delete */
514 splice(a, 'not', 8, 0); /* [ 'This is not a string', NULL ] */
515 splice(a, '', 5, 3); /* [ 'This a string', 'is ' ] */
516 splice(a, 'can be', 5, 3); /* [ 'This can be a string', 'is ' ] */
518 The chr() and ord() functions return the character represented by a Unicode
519 number and vice-versa:
521 ord('A'); /* returns 65 (ASCII or Unicode for A) */
522 chr(65); /* return 'A' */
524 The sprintf() function works like any other sprintf() implementation in
525 the world: accepts a format string (with special, %-prefixed placeholders)
526 and a list of arguments, and substitutes them.
528 sprintf("%s: %d", 'ERROR', 10); /* returns 'ERROR: 10' */
529 sprintf("%04X", 16384); /* return '4000' */
531 The sscanf() function can be seen as the opposite of sprintf(): given a
532 format string and a scanning string, extracts a set of strings and returns
535 sscanf("name 100", "%s %d"); /* returns [ 'name', 100 ] */
537 The standard %-commands can be used, as %d, %i, %u, %f and %x, for decimal,
538 unsigned, floating point or hexadecimal numbers, %n, to return a matching
539 offset, and %[, to match an explicit set of matching characters, than can
540 be negated by adding a ^ after the bracket. As a non-standard extension, a
541 special command %S is also implemented, that match a string upto the
542 following substring in the format string. For example:
544 sscanf("key: value", "%S: %S"); /* returns [ 'key', 'value' ] */
545 sscanf("text words 1", "%S%d"); /* returns [ 'text words ', 1 ] */
547 As in the standard, the format can include an optional number of maximum
548 characters and a * to mark the matched string as non-interesting (so it's
551 The sscanf() function also accepts an optional third argument with an
552 offset to start scanning.
557 Arrays can be easily manipulated by using push(), pop(), shift(), adel()
563 /* add three values: ary will contain [ 10, 20, 30, 40 ] */
564 push(ary, 10); push(ary, 20); push(ary, 30); push(ary, 40);
566 /* delete and return last value; ary contains now [ 10, 20, 30 ]
567 and top contains 40 */
570 /* shift() deletes from the beginning; ary contains now [ 20, 30 ] */
573 /* ins() inserts an element in an array; ary contains now [ 20, 5, 30 ] */
576 */ adel() does the opposite, delete values; ary is back [ 20, 30 ] */
577 deleted_item = adel(ary, 1);
579 The seek() function searches sequentially for an exact value in an array,
580 returning its offset if found, or -1 otherwise:
582 weekdays = [ 'monday', 'tuesday', 'wednesday', 'thurday', 'friday' ];
585 seek(weekdays, 'wednesday');
587 The sort() function returns a sorted copy of the array sent as argument:
589 sorted_weekdays = sort(weekdays);
591 The sorting is done with the cmp() function (see above); if another kind of
592 sort is wanted, the 2-argument version must be used, as mentioned
593 previously in the _Subroutines_ section.
595 Whenever a change must be done on every element of an array, the map()
596 function can be useful and much faster than iterating the array using
597 foreach() and storing the changes in another one.
599 The first argument of map() can be an executable value accepting one
600 argument, that will be set to each element of the array. The return
601 value will be used as the new value:
603 /* a function to multiply by 10 its unique argument */
604 sub multiply_by_10(val)
609 /* an array of values */
610 a = [ 1, 5, 3, 7, 30, 20 ];
612 /* a_by_10 will contain [10, 50, 30, 70, 300, 200 ] */
613 a_by_10 = map(multiply_by_10, a);
615 Of course, the executable value can be an anonymous subroutine:
617 /* Creates a zero-filled version of the numbers in a */
618 a_zero_filled = map( sub(e) { sprintf("%04d", e); }, a);
620 The first argument of map() can also be a hash to directly
623 /* numerals as keys, names as values */
624 english_nums = { 1 => "one", 2 => "two", 3 => "three" };
626 /* translated_nums will have [ "one", "two", "three" ] */
627 translated_nums = map(english_nums, [ 1, 2, 3 ]);
629 Another powerful array function is grep(); it matches a regular
630 expression on an array and returns a new one containing all
631 elements that matched:
633 a = [ 1, 9, "mix190", 4, "foo", 9000 ];
635 /* b is [ 9, "mix190", 9000 ] */
638 The first argument of grep() can also be an executable value,
639 that will accept each element of the array on each call. The
640 output array will contain all elements for which the function
643 /* return all elements greater than 10 */
644 a = [ 1, 45, 20, 4, 0, 1000 ];
646 /* b is [ 45, 20, 1000 ] */
647 b = grep( sub(e) { e > 10; }, a);
649 If no element were generated, NULL is returned (instead of
652 Other lower level array functions are expand() and collapse().
653 The expand() function opens space in an array:
655 ary = [ 1, 2, 3, 4 ];
657 /* ary will have two more elements at offset 3,
658 containing [ 1, 2, 3, NULL, NULL, 4 ] */
661 On the opposite, collapse() deletes space:
663 /* deletes 3 elements at offset 1: ary is now [ 1, NULL, 4 ] */
666 Take note that expand() and collapse() manipulate the array
667 directly (i.e. they don't return a new array).
672 The hsize() function returns the number of pairs in a hash:
675 num = hsize( { 1 => 'one', 2 => 'two', 3 => 'three' });
677 Take note that size() cannot be used for this purpose, as it returns the
678 number of buckets in the hash (which is probably of no use for you).
680 If the existence of a pair must be tested, the exists() function is handy:
682 h = { 'a' => 1000, 'b' => 0, 'c' => 3000 };
685 if (exists(h, 'b')) print("B exists\n");
687 /* this is not, as 0 does not evaluates as true */
688 if (h['b']) print("B is true\n");
690 The keys() function returns an array with the keys of a hash:
692 /* will contain [ 'a', 'b', 'c' ] */
695 The hdel() function deletes a pair given its key. The deleted value is
698 /* deletes 'c' and returns its value (3000) */
699 c_value = hdel(h, 'c');
701 Type-checking functions
702 ~~~~~~~~~~~~~~~~~~~~~~~
704 There are three functions to test a type of a value: is_exec(), is_hash()
705 and is_array(). They return true if the tested value is executable, a hash
706 or an array, respectively.
708 On this implementation, executable and hash values are also arrays, so if
709 you need to act differently on the type of a value, the correct order is
710 is_exec(), is_hash() and is_array(), as in the following example:
712 if (is_exec(v)) { print("v is executable\n"); }
714 if (is_hash(v)) { print("v is a hash with ", hsize(v), " pairs\n"); }
716 if (is_array(v) { print("v is an array with ", size(v), " elements\n"); }
718 print("v is a scalar\n");
720 Regular expression matching
721 ---------------------------
723 Regular expression matching is done with the regex() function. In the simpler
724 of its forms, it can be used to test if a string matches:
726 /* hours, minutes, seconds */
729 if (regex("/^[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$/", v))
730 print("The v variable contains a valid time\n");
732 The regular expression can contain flags; those are `i', for case
733 insensitiveness, `l', to select the last matching instead of the first,
734 `m', to treat the string as a multiline one (i.e., one containing \n
735 characters), where the special ^ and $ markup match beginning and end of
736 lines and not the beginning or end of the string, or `g', to do a global
737 matching (return _all_ matching strings). Flags are written after the
740 Regex() returns the matched string, or an array of matched strings if
741 the `g' flag is used. If no match is found, NULL is returned.
743 v = "The value of PI is 3.1416, more or less";
745 pi = regex("/[0-9]+\.[0-9]+/", v);
747 If regex() is called without arguments, it returns the last match as an array
748 of two elements; the first contain the offset of the matched strings, and the
749 second, the size in characters of it. If last match was unsuccessful, NULL is
752 /* get the match of the previous example */
756 print(coords[0], ":", coords[1], "\n");
758 Regex() can include an optional third argument, the offset where the regular
759 expression matching must start (instead of 0, the beginning of the string).
760 This can be used to extract many values from the same string:
762 /* lets extract all numbers in the following string: */
763 s = "There is 10 and 35 and 42 in the number 103542!";
766 while (n = regex("/[0-9]+/", s, offset)) {
772 /* get the coordinates */
775 /* start from there (previous match offset + size) */
776 offset += c[0] + c[1];
779 Though, for this particular example, the `g' flag could be more suitable:
781 /* return all numbers in one step */
782 foreach (s, regex('/[0-9]+/g', s))
785 For more complex situations, the first argument of regex() can be an array
786 of regular expressions (instead of a string). Those expressions will tried
787 one after the other on the string, each one using the offset returned by
788 the previous one. If all of them are matched, an array with the matched strings
791 /* match pairs of key=value commands, with many variants */
793 v2 = " KEY2 = value2";
796 /* array regex: a regex for each part */
797 r = regex([ "/\s*/", /* possible leading blanks (0) */
798 "/[a-z0-9]+/i", /* the key (1) */
799 "/\s*=\s*/", /* optional blanks, the = , more blanks (2) */
800 "/[a-z0-9]+/i" /* the value (3) */
804 /* the key is the element #1 and the value the #3 */
809 Regular expression substitutions
810 --------------------------------
812 The sregex() function can be used to substitute the matching part of a
813 string with another string. The simplest form:
815 /* substitute all characters in password with asterisks */
816 sregex('/./g', password, '*');
818 The substitution part can also be a hash:
820 /* substitute \n, \r, etc with their equivalents */
821 sregex("/\\[rnt]/g", var, {
829 /* zero-pad all numbers in var */
830 sregex('/[0-9]+/g', var, sub (m) {
834 The regular expression can contain flags; those are `i', for
835 case-insensitive matching, and `g', for global replacements (all ocurrences
836 in the string will be replaced, instead of just the first found).
838 Input/Output operations
839 -----------------------
841 File operations are done by opening files with the open() call. It accepts
842 two arguments, the file and the mode:
844 /* open in read mode */
845 local i = open('existingdata.dat', 'r');
847 /* open in write mode (create) */
848 local o = open('newdata.dat', 'w');
850 The opening mode is the same as the one in the fopen() C library call, so
851 other combinations exist.
853 If the file cannot be open or created, NULL is returned.
855 The read() function returns a line read from a file descriptor, or NULL if
856 no more lines can be read (i.e. on EOF). A line is considered from the
857 current file position until an end of line mark (\n or \r\n). All files
858 under MPSL are considered as text ones; before the read() call returns, the
859 string is converted to wide chars (the internal representation of strings
860 in MPSL) by using the current locale definitions, unless a explicit
861 encoding is set by calling encoding() (see below).
863 The opposite of read() is write(); it accepts a file descriptor and a
864 string to be written. As read(), the string is converted from wide chars by
865 using the current locale definitions unless a different encoding() was set.
866 The write() function does not write an end of line, though.
868 Similar functions getchar() and putchar() exist to read and write
869 characters instead of lines. This functions don't do any locale nor
870 encoding conversions.
872 The unlink() function can be used to delete files, chmod() to set the mode
873 and chown() to set the owner.
875 The stat() function returns an array containing information about a file,
876 similar to the C library function of the same name. The returned values
877 are: device number, inode, mode, number of links, uid, gid, rdev, size,
878 atime, mtime and ctime. Take note that not all values has meaning depending
879 on the filesystem or the architecture. The `mode' argument can be used in
880 chmod() and `uid' and `gid' in chown(). If the file does not exist, stat()
881 returns NULL (which is a good way to test for file existence).
883 The glob() function accepts a file spec as the only argument and returns
884 an array of filenames matching it. Directories have a / as the last
885 character. The file spec and the returned files are system-dependent, but
886 always usable by open().
888 The popen() and pclose() function calls are used for piping from / to
889 programs. Pipes can be open for reading ('r' mode), for writing ('w' mode),
890 or bidirectional (by appending '+' to the mode, which can be used to
891 communicate two-way with programs like ispell, smtp servers and such). Once
892 open, the read() and write() functions can be used the same as in plain
895 As mentioned above, the encoding() function can be used to force the
896 reading or writing conversions to be done when doing IO. By default, the
897 current locale is used, and it's the same as calling encoding() without
898 arguments. If an encoding is forced, reading and writing is done by using
899 the selected converter. Take note that support for the `iconv' library must
900 be compiled in; if it's not, a special, ad-hoc converter called "utf-8"
903 Each file can have a different encoding converter and is selected at open()
906 A snippet to convert a file from current locale to iso8859-1 can be:
908 sub convert_to_iso8859_1(fromfile, tofile)
913 /* open the file first */
914 if ((i = open(fromfile, 'r')) == NULL)
917 /* set encoding to iso8859-1 (can fail) */
918 if (encoding("iso8859-1") < 0) {
923 /* create the new file now; it will have iso8859-1
924 encoding as it's the last encoding set. The input
925 file keeps its original encoding */
926 if ((o = open(tofile, 'w')) != NULL) {
935 /* close input and revert to default encoding */
942 The load() function can be used to load, compile and execute an MPSL
943 source file. The file will be searched for using the paths in the INC
952 Stores the command line arguments sent to the main() function.
957 Holds an array of paths on disk where source files will be searched
958 for by the load() command.
963 Stores the last severe error message (compilation errors, etc.).
968 Stores the last error message from the system (file not found, etc.).
970 STDIN, STDOUT, STDERR
971 ~~~~~~~~~~~~~~~~~~~~~
973 The famous standard file descriptors. Can be used in read(), write()
974 and related functions.
979 The home directory for the current user. It's supposed to be a writable
980 directory where users can store their configuration and documents.
985 The application directory.
990 The last successful charset encoding set by the encoding() function.
995 Similar to ENCODING, but used only when opening a file for writing and
996 if ENCODING is not set. After opening, its content is destroyed.
1001 Filled after each file opening in read mode, if a specific charset
1002 encoding could be found. Can be copied onto ENCODING or TEMP_ENCODING.
1007 Stores the MPSL version as a string.
1012 This is an accessor / mutator function that allows to retrieve information
1013 and tune some values for the MPDM (Minimum Profit Data Manager). If called
1014 without arguments, the current values are returned. A hash can be sent as
1015 argument to change some of them. These are the keys:
1017 * count: Total number of values being managed (read only).
1018 * hash_buckets: The default number of buckets a hash will use. It must
1019 be a prime number (read / write).
1021 The default values are the best for normal usage. Of course, you only must
1022 change any of them if you know what you are doing.
1026 /* set a bigger value for hash buckets */
1027 MPDM( { 'hash_buckets' => 127 });
1029 /* dump current setup */
1033 Angel Ortega <angel@triptico.com>