From time to time I fiddle around with the oo part of ooRexx, but to
get
anything useful done I always find myself falling back to function-based
programming, which just works.
For example, I'm trying to figure out how to add methods to read/write
data
between an external file and a directory collection, and I'm really
struggling. I modified the example from the V4.2 LangRef, pp. 288, 289 to
store user-defined objects instead of strings in a directory collection. I
finally got that to work (after puzzling over the arcane requirements of the
setMethod method), but getting the data from an external file into the
directory and putting it back just won't work, and I can't see why.
I created a subclass of .directory to which I added class methods load
and
unload. ooRexx insists on treating them like instance method calls and
triggers the UNKNOWN method instead of invoking them as class methods. There
are no detailed examples of such usage in the doc as far as I can see; what I
do find there sheds no light on what I'm doing wrong.
Here's my main program:
| #!/usr/bin/rexx
|
--.--------------------------------------------------------------------------.--
| --|A Phone Book Directory
program |--
| --|This program demonstrates use of the directory
class. |--
|
--'--------------------------------------------------------------------------'--
| trace rnormal
| -- Define an UNKNOWN method that adds an abbreviation lookup feature.
| -- (Directories do not normally have to have an UNKNOWN method.)
| example = .PhoneBook ~ new ~~ setMethod('UNKNOWN', .methods['UNKNOWN'])
| say example ~ type
| example ~ load('Phone.book')
|
| do e over example -- Iterate over the collection
| say example[e]
| end e
|
| say -- Index lookup is case sensitive...
| say example ~ entry('Mike') -- The ENTRY method uppercases before
lookup
| say example['ANN'] -- Exact match
| say example ~ ann -- Message converts to uppercase
before lookup
| say example['ann'] -- Exact match with lowercase index
| say -- Use the UNKNOWN method too look
these up:
| say example['M']
| say example['J']
| say example['Z']
|
| example ~ unload('Phone.book')
| exit
|
| -- Handle indexes not found. (Cannot be a method of a class, and apparently
must
| -- be in the same file from which it is instantiated.)
| ::method 'UNKNOWN'
| trace normal
| use arg matchName
| value = ''
|
| do instance over self
| if name ~ abbrev(matchName) then
| do
| if instance ~ fullName ~ length > 0 then
| value = value', '
|
| value = value || self ~ fullName[instance]':' self ~
phoneNumber[instance]
| end
| end instance
|
| if value ~ length = 0 then
| value = 'No listing was found for' matchName'.'
| return value
|
| ::requires 'PhoneBook3.orxCls'
and this is PhoneBook3.orxCls:
|
-------------------------------------------------------------------------------
| -- Implementation of a phone book
entry: --
| ::class contact public
|
| ::attribute name
| ::attribute fullName
| ::attribute phoneNumber
|
| ::method init public
| expose name fullName phoneNumber
| trace ?results
| use arg name,fullName,phoneNumber
| return
|
| ::method type public
| trace ?results
| return 'a Phone Book contact'
|
| ::method string public
| expose name fullName phoneNumber
| trace ?results
| say name ~ left(12) fullName ~ left(25,'.') phoneNumber
| return
|
| ::method format public
| expose name fullName phoneNumber
| trace ?results
| EoT = '04'x
| record = name ~ strip || EoT,
| || fullName ~ strip || EoT,
| || phoneNumber ~ strip
| return record
|
-------------------------------------------------------------------------------
| -- Implementation of a phone book
directory: --
| ::class PhoneBook public subclass directory
|
| ::method init public
| trace normal
| self ~ init:super
| return
|
| ::method type public
| trace normal
| return 'a Phone Number directory'
|
| ::method load public class -- Read contacts from an external
file.
| expose name fullName phoneNumber
| trace ?results
| EoT = '04'x
| use strict arg fileName,mode = 'read shared'
| fileStream = .stream ~ new(fileName)
| fileStream ~ open(mode)
|
| do while fileStream ~ lines > 0
| parse value fileStream ~ lineIn with name (EoT) fullName (EoT)
phoneNumber
| self ~ put(.contact ~ new(fullName,phoneNumber),name)
| end
|
| fileStream ~ close
| return
|
| ::method unload public class -- Write contacts into an external
file.
| expose name fullName phoneNumber
| trace ?results
| EoT = '04'x
| use strict arg fileName,mode = 'write replace'
| fileStream = .stream ~ new(fileName)
| fileStream ~ open(mode)
|
| do this over self
| fileStream ~ lineOut(contact ~ record)
| end
|
| fileStream ~ close
| return
|
-------------------------------------------------------------------------------
This is the output from PhoneBook3:
| 9 *-* example = .PhoneBook ~ new ~~
setMethod('UNKNOWN', .methods['UNKNOWN'])
| >>> "a PHONEBOOK"
| 10 *-* say example ~ type
| >>> "a Phone Number directory"
| a Phone Number directory
| 11 *-* example ~ load('Phone.book')
| >>> "No listing was found for LOAD."
| 13 *-* do e over example -- Iterate over the
collection
| >>> "a PHONEBOOK"
| 17 *-* say -- Index lookup is case
sensitive...
| >>> ""
|
| 18 *-* say example ~ entry('Mike') -- The ENTRY method
uppercases before lookup
| >>> "No listing was found for MIKE."
| No listing was found for MIKE.
| 19 *-* say example['ANN'] -- Exact match
| >>> "No listing was found for ANN."
| No listing was found for ANN.
| 20 *-* say example ~ ann -- Message converts to
uppercase before lookup
| >>> "No listing was found for ANN."
| No listing was found for ANN.
| 21 *-* say example['ann'] -- Exact match with lowercase
index
| >>> "No listing was found for ann."
| No listing was found for ann.
| 22 *-* say -- Use the UNKNOWN method too
look these up:
| >>> ""
|
| 23 *-* say example['M']
| >>> "No listing was found for M."
| No listing was found for M.
| 24 *-* say example['J']
| >>> "No listing was found for J."
| No listing was found for J.
| 25 *-* say example['Z']
| >>> "No listing was found for Z."
| No listing was found for Z.
| 27 *-* example ~ unload('Phone.book')
| >>> "No listing was found for UNLOAD."
| 28 *-* exit
So, what am I missing? I suspect it's something that's so fundamental
that
it's not clearly stated in the doc.
Leslie
--
"Disobedience is the true foundation of liberty. The obedient must be
slaves." --Henry David Thoreau
------------------------------------------------------------------------------
Time is money. Stop wasting it! Get your web API in 5 minutes.
www.restlet.com/download
http://p.sf.net/sfu/restlet
_______________________________________________
Oorexx-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/oorexx-users