PreviousUpNext

5.3.17  Mythryl Backticks Operators

Bash, Perl and a number of other modern scripting-influenced languages supply a backticks operator returning the output from an executed shell expression:

    #!/usr/bin/perl -w
    use strict;
    my $text = `ls -l`;

Mythryl implements a similar backquote operator:

    linux$ my

    eval:  text = `ls -l`;

    eval:  print text;
    total 5028
    drwxr-xr-x 2 cynbe cynbe    4096 2009-03-12 03:53 bin
    -rwxr-xr-x 1 cynbe cynbe     328 2007-11-26 00:34 Configure
    drwxr-xr-x 4 cynbe cynbe    4096 2009-03-02 23:34 doc
    drwxr-xr-x 2 cynbe cynbe    4096 2008-01-10 14:10 etc
    -rw-r--r-- 1 cynbe cynbe   30127 2009-03-12 03:51 LIBRARY_CONTENTS
    -rw-r--r-- 1 cynbe cynbe    1178 2009-03-12 03:56 main.log~
    -rw-r--r-- 1 cynbe cynbe 3558521 2009-03-12 03:53 MAKELIB_FILE_HIERARCHY.INFO~
    -rw-r--r-- 1 cynbe cynbe   25328 2009-03-08 22:57 Makefile
    -rwxr-xr-x 1 cynbe cynbe     386 2009-03-12 03:22 my-script
    -rw------- 1 cynbe cynbe  601330 2009-03-12 03:56 mythryl.compile.log
    -rw-r--r-- 1 cynbe cynbe  219341 2009-03-12 03:51 mythryld-9748.load.log
    -rw-r--r-- 1 cynbe cynbe  129121 2009-03-12 03:51 COMPILED_FILES_TO_LOAD
    -rw-r--r-- 1 cynbe cynbe  492671 2009-03-12 03:56 read-eval-print-loop.log~
    -rw-r--r-- 1 cynbe cynbe    3338 2009-03-04 17:37 README
    drwxr-xr-x 3 cynbe cynbe    4096 2009-03-09 05:10 sh
    drwxr-xr-x 6 cynbe cynbe    4096 2009-03-08 20:36 src
    -rw-r--r-- 1 cynbe cynbe     353 2007-09-06 21:52 TODO
    drwxr-xr-x 3 cynbe cynbe    4096 2009-02-21 14:05 try
    -rw-r--r-- 1 cynbe cynbe      27 2008-02-07 03:06 w
    -rwxr-xr-x 1 cynbe cynbe     124 2008-03-09 22:05 y
    -rwxr-xr-x 1 cynbe cynbe     501 2008-01-06 13:15 z

A major difference is that early in compilation the Mythryl compiler expands this operator into a call to the back__ticks function.

This means that just by redefining the back__ticks function, the application programmer can redefine the meaning of the backticks construct.

This can be useful, for example, in a file defining many TCP/IP dotted-quad addresses, allowing syntax like

    open_socket( `192.168.0.1` );

to be substituted for perhaps

    open_socket( IP_ADDRESS (192, 168, 0, 1) );

If the construct is being used only once or twice, this is not a significant win, but if a long file configuring (say) a mail transport agent contains hundreds of such constructs, the difference in readability may be substantial.

A similar dot__backticks operator is also implemented by the Mythryl front end, expanding from syntax like

    open_socket( .`192.168.0.1` );

By defining dot__backticks, you can make the construct do whatever you want with the quoted string:

    linux$ my

    eval:  dot__backticks = toupper;

    eval:  .`this is weird`;
    "THIS IS WEIRD"

To implement the above IP address facility we might write something like:

    #!/usr/bin/mythryl

    Ip_Address = IP_ADDRESS (Int, Int, Int, Int);

    fun dot__backticks  ip_address_string
        =
        case (regex::find_first_match_to_regex_and_return_all_groups
                  ./^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
                  ip_address_string)

        THE [ a, b, c, d ]   => IP_ADDRESS( atoi a, atoi b, atoi c, atoi d );
        _                    => raise exception DIE "Invalid IP address syntax";
        esac;

With these definitions in place we can do:

    eval:  .`123.194.12.14`;
    IP_ADDRESS (123, 194, 12, 14)

    eval:  .`123.43.23`;
    unCaUght exception DIE [DIE: Invalid IP address syntax]

The Mythryl standard library assigns no default definition to the dot__backticks function.

In a similar vein, ."a b c d" expands early in the Mythryl front end into dot__qquotes "a b c d". The scripting_globals package sets dot__qquotes to words which in turn is defined as

        words = string::tokens char::is_space;

Consequently, by default this construct provides a convenient way to specify lists of short words. It is somewhat like the Perl qw/.../ construct:

    linux> my

    eval:  ."a b c d e f";

    ["a", "b", "c", "d", "e", "f"]

This can substantially improve readability in certain sorts of programming.

Once again, by redefining dot__qquotes the application programmer may repurpose this facility for other needs:

    linux$ my

    eval:  dotqquotes__op = implode o shuffle o explode;

    eval:  ."abcdefgh"
    "hcdefgba"

In similar fashion .’foo’ expands into a call to dot__quotes, .<foo> expands into a call to dot__brokets, .|foo| expands into a call to dot__barets, .#foo# expands into a call to dot__hashets and ./foo/ expands into a call to dot__slashets. All of these functions default to the identity function. Also, the only escape sequence recognized within any of these quotation constructs is doubling of the terminator to include it in the string; for example .#foo##bar# is equivalent to "foo#bar. This makes them useful for avoiding the need to double all backslashes in regular expressions.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext