[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8. File and Directory Objects

Since files and strings are both sequences of characters, it is reasonable to use the same operations for both. But we need to distinguish files from file names, just as we distinguish identifiers from the values they evaluate to. Q uses the prefix `./' to indicate a filename, which evaluates to a file "object":
 
./Makefile # The file named Makefile.
File names can be specified at runtime:
 
Q1> :fname="Makefile"
Q2> ./$fname # Same as ./Makefile

Q follows the emacs convention that two consecutive `/' means to go to root:
 
.//etc/passwd  # The file name /etc/passwd
Just `.' by itself is short for `./.'.

A filename object is a sequence (string) variable, so all generic sequence operations are applicable:
 
Q1> [2 3 4] 1 # Indexing
3
Q2> "xyz" 1
y
Q3> ./Makefile
Q.tar.Z:
        tar -v -cf - -X non-tared mi doc unix/Makefile Qlink test \
        Makefile non-tared|compress -c>Q.tar.Z
Q4> ./Makefile 1
.
Q5> size [3 4 5]
3
Q6> size "xyz"
3
Q7> size ./Makefile
110
Q8> # Reversing Makefile:
Q9> sprintf "%s\n" (./Makefile (109 downto 0))

Z.rat.Q>c- sserpmoc|derat-non elifekaM
\ tset knilQ elifekaM/xinu cod im derat-non X- - fc- v- rat
:Z.rat.Q     

Filenames can be used for assignment:
 
Q10> ./foo := ./Makefile
In this case the filename ./foo can name a non-existing file.

A filename that names a directory is viewed as an associative mapping from names to other filenames.
 
Q11> sprintf "%#s\n" ./unix
./unix
Q12> sprintf "%#s\n" (./unix "Makefile")
./unix/Makefile
Q13> # Can also use a filename as an "index":
Q14> sprintf "%#s\n" (./unix ./Makefile)
./unix/Makefile
Q15> sprintf "%#s\n" (. "/etc/passwd")
.//etc/passwd
Q16> size (. "/etc/passwd")
759
Q17> wc /etc/passwd
      14      30     759 /etc/passwd

Compare the Unix process environment, which is also represented as an associative mapping, except in this case each name is mapped to a string:
 
Q18> env "TERM"
xterm
Q19> size env
18
Q20> env "FOO" := "/etc/foo"
Q21> size env
19
Q22> env "FOO"
/etc/foo

As a convenience, Q will assume that an identifier is a filename, even without a `./' prefix, if there exists a file with that name, and no other binding for that name is in scope. Thus Makefile will normally work as well as ./Makefile. Also, a word that begins with `/' is treated as an absolute filename. (Just `/' by itself is the division operator.) A word that begins with an existing directory name followed by `/' is also a filename.

These conventions work well, but may be modified or removed if it turns out they are too complicated or error prone. There are some complications because / is also used for division. Thus a/b is a divided by b if a is defined. Otherwise, a/b is a filename if a is a directory (that exists at compile time). Otherwise, a/b is an error.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Per Bothner on December, 4 2001 using texi2html