17.2 Including external scripts

17.2.1 include

Synopsis

include(file_name);

include(file_name, parameters);

Description

Include and interpret file_name as if it were typed directly within source file; file_name can be any valid expression:

Remember that file names must be relative to the directory storing the file where the include () function occurs.

Warning: If you use Cows-mkgen to create makefiles, file_name can only be provided as a string constant (e.g. "foo.cws") or via the inputfile () and outputfile () functions; otherwise, Cows-mkgen will miss the dependency and raise a warning. Of course, Cows will correctly include the file, since it can handle complex expressions, but if the included file is changed, make won't update output file. For further informations see Section 24.1.

Optional parameters can be one or more expressions separated by commas. Within the included file parameters are available in array _argv [ ]; the first elements of this array is file name, so the first parameter is stored in _argv [1], the second in _argv [2] etc. Length of array _argv [ ] is available as _argc.

Warning: If you don't plan to run Cows directly on the included file, you don't need a pair of <cows>...</cows> tags within it. In fact, content of included file will be inserted in a script so there are always Cows-tags around it.

However, if you user Cows-mkgen in fast mode, you need them to get right Makefiles (Section 6.10)

Example

Assuming file quote.cws contains the following text:

print ("At present, scientists do not look for alternatives");
print ("simply because they do not care enough about the animals");
print ("they are using.");
print ("-- Grace Slick");

You can include its content within your script with the following line:

include ("quote.cws");

Cows' output will be:

At present, scientists do not look for alternatives
simply because they do not care enough about the animals
they are using.
-- Grace Slick

Example 2 - include () and variables

Assuming file quote.cws contains the following text:

quote_and_author = 
    "Atrocities are not less atrocities when they occur in "
  + "laboratories and are called medical research."
  + author;

You can define a variable in another script, include quote.cws and print the result:

author = "-- G.B. Shaw";
include ("quote.cws");
print (quote_and_author);

Cows' output will be:

Atrocities are not less atrocities when they occur in
laboratories and are called medical research.
-- G.B. Shaw

As you can see, included files are executed as if they were directly typed within including script so:

Example 3 - parameters

Assuming file foo.cws contains the following text:

<cows
  print ("File Included with " + _argc + " parameters.");
  print ("File Name: " + _argv [0]);
  print ("First Parameter: " + _argv [1]);
  print ("Second Parameter: " + _argv [2]);
/>

If you include this script with the following code:

include ("foo.cws", 10, "Hallo World !");

You'll get the following output:

File Included with 3 parameters.
File Name: foo.cws
First Parameter: 10
Second Parameter: Hallo World !

This manual can be downloaded from http://www.g-cows.org/.