[Tutor] is it possible to embed data inside a script?

Alan Gauld alan.gauld@blueyonder.co.uk
Sat Jul 19 16:08:02 2003


> can a person include the data that a script processes 
> inside the script itself?  

Sure just create a variable assignment.

> i am in the process of converting a short perl script that 
> processes everything following __DATA__ instead of processing 
> the contents of a separate (very small) file.

You can use sinple text strings to hold the data, in which 
case it looks just like a file, but more sensible and sophisticated 
is to hold the data as a Python data structure.

A list of tuples might work, or a dictionary. This avoids all the 
text processing stuff and lets you get right to work while the 
data is still held in text form within the file for easy 
modification between runs.

I often do this for test scripts when using Python for volume tests.

Of course its usually better to separate the two and this can be 
done easily by defining an import and functon call after the data 
and putting all the processing code in a separate file:

#### trivial case ####
# file = foo.py

def process(data):
   for s in data:
      print s

#########################
# file = data.py

data = """
some data here
some more
and still more
"""

import foo
foo.process(data)
#########################

HTH,

Alan G.

Alan G.