Opening a STREAM in a Persistent Procedure Functio

2019-08-31 09:15发布

问题:

I have a persistent procedure, in which I am trying to open, write and close a stream.

I have in the main area of the procedure

DEFINE STREAM sOutFile.
OPEN STREAM sOutFile TO VALUE( outFileName ).
MESSAGE SEEK( sOutFile ).

and subsequently a function within the persistent procedure

FUNCTION example RETURN LOGICAL:
  MESSAGE SEEK( sOutFile ).
  PUT STREAM sOutFile UNFORMATTED someData.
END.

When the persistent procedure is instantiated, the message displays "0" so the stream has been opened. However, when example is called, the message displays "?" and I get a message about attempting to write to a closed stream.

I've tried declaring the stream NEW SHARED but that didn't make any difference.

Am I doing something wrong, or is it impossible to define streams within persistent procedures?

回答1:

It is early and my coffee hasn't kicked in yet but I think that you have to open the stream outside the body of the PP.

This works:

/* ppstream.p
 *
 */

define stream logStream.

session:add-super-procedure( this-procedure ).

/* end of PP init */

function initLog returns logical ( input lgFile as character ):
  output stream logStream to value( lgFile ) unbuffered.
  return true.
end.

function logStuff returns logical ( input msg as character ):
  put stream logStream unformatted msg skip.
  return true.
end.

and then call it like so:

function initLog  returns logical ( input lgFile as character ) in super.
function logStuff returns logical ( input msg as character ) in super.

run ./ppstream.p persistent.

initLog( "log.txt" ).
logStuff( "test" ).

(I used a session super-procedure to avoid having to define handles -- you would not necessarily need to do that.)