[REBOL] [REBOL]Problem with function argument Re:(4)
Thanks Elan: Your discussion is very helpful. As to your question: >I haven't been following this thread, so I don't know why you are using >ports. Generally speaking, it is often more convenient to use higher As to the above, I want re-usable code that will allow me to easily write to a file OR to standard output. My approach is to set up a value that could point EITHER to a file or to standard output. EX: in C: FILE *f; if(write_to_file) f = fopen("test.txt","w") else f = stdout; fprintf(f,"line one"); That in part is my choice of using ports. Part of that is circumstance, as well, perhaps the upper level functions would be better, given this need. I would welcome your advice. As well, I come from the c/c++/assembler background, I try to think in terms of minimizing machine overhead. I do primarily CGI programming, and like to use the same code to do either web content or send that web content to file. Either for testing purposes or in case a client wants me to enable him/her to build or update a page on the machine end. Keep up the good work Elan, you are always helpful. tim At 01:19 PM 4/2/00 -0700, you wrote: >Hi t, > >A file! type is a filename with a leading % character. Example: %myfile.r >%/c/mydir/. A file! may be any type of file, including a directory. > >A port! datatype is a low-level i/o abstraction that REBOL uses to >serialize different types of i/o channels, such as files (here we are not >talking about the filename, but access to the file's contents), and socket >i/o. > >The access to ports is typically controlled by protocols that define how >information is read and written to a port, and also commonly implement some >convenient navigation functions for the port's datastream, such as first, >next, insert, pick, etc. These functions work in a sensible way on a port! >datatype. They enable you to treat ports as though they were series values >(i.e. blocks, paths, hashes ...) to a degree, hence the term serialize is >used to describe this type of access to i/o channels. > >There are some limitations, since not all ports lend themselves to being >treated as a series effectively with respect to all series! functions. > level >functions when dealing with files, then accessing them as ports. read, >write, read/lines, write/append write/lines, write/append/lines, save, >load, come to mind. All of these functions take a filename (type file!), >and do not require that you manage the file on the port level, for instance >with respect to positioning, etc. > > >Hope this helps. > > > >;- Elan >> [: - )] > >
[REBOL] [REBOL]Problem with function argument Re:(3)
Hi t, A file! type is a filename with a leading % character. Example: %myfile.r %/c/mydir/. A file! may be any type of file, including a directory. A port! datatype is a low-level i/o abstraction that REBOL uses to serialize different types of i/o channels, such as files (here we are not talking about the filename, but access to the file's contents), and socket i/o. The access to ports is typically controlled by protocols that define how information is read and written to a port, and also commonly implement some convenient navigation functions for the port's datastream, such as first, next, insert, pick, etc. These functions work in a sensible way on a port! datatype. They enable you to treat ports as though they were series values (i.e. blocks, paths, hashes ...) to a degree, hence the term serialize is used to describe this type of access to i/o channels. There are some limitations, since not all ports lend themselves to being treated as a series effectively with respect to all series! functions. I haven't been following this thread, so I don't know why you are using ports. Generally speaking, it is often more convenient to use higher level functions when dealing with files, then accessing them as ports. read, write, read/lines, write/append write/lines, write/append/lines, save, load, come to mind. All of these functions take a filename (type file!), and do not require that you manage the file on the port level, for instance with respect to positioning, etc. Hope this helps. ;- Elan >> [: - )]
[REBOL] [REBOL]Problem with function argument Re:(2)
Hi Ladislav: Thanks, you got it! All I was doing was using the wrong data type in the function arg. Obviously, I am not entirely clear about the distinction between "port" and "file" data types. Can anyone enlighten me, or point me to the documentation that will clarify for me? regards :>) tim At 11:13 AM 4/2/00 +0200, you wrote: >Hi, > >- Puvodní zpráva - >Od: <[EMAIL PROTECTED]> >Komu: <[EMAIL PROTECTED]> >Odesláno: 2. dubna 2000 7:33 >Predmet: [REBOL] [REBOL]Problem with function argument > > >> Hello All: >> >> I am getting an error message when attempting >> to pass a value as a argument to a user-defined >> function: >> >> the error message is: >> fprint expected fp argument of type: file. >> ** Where: fprint fp "line one" >> >> code follows: thanks in advance to all!! >> tim >> >> ; first the function >> fprint: func [fp[file!] value[string!]] >> [ >> either not-equal? fp none >> [ >> write/append fp value >> write/append fp newline >> ] >> [ print value ] >> ] >> fp: none >> ; check for server name in cgi environment variables >> either equal? system/options/cgi/server-name none >> > >>; value is empty, presume the script is run from commad line >>print "no server name, application run from console" >>fp: open/new/write %hello.htm >> ] >> ; script is invoked via server, yes, I know we need >> ; a content-header string, etc here. >> > >> prin "Server name: " >> print system/options/cgi/server-name >> ] >> ; call function >> fprint fp "line one" ; rebol doesn't like this >> ; if the file has been opened, close it >> if not-equal? fp none [close fp] >> > >The header of your function should probably be as follows: > >fprint: func [fp[port! none!] value[string!]] > >to allow open ports and none as the valid arguments. > >Moreover, the following: > >> either not-equal? fp none >> [ >> write/append fp value >> write/append fp newline >> ] >> [ print value ] >> ] > > >will not work for ports. You can change it to: > >either fp [ >append fp value >append fp newline >] [print value] > >Regards, >Ladislav > > >
[REBOL] [REBOL]Problem with function argument Re:
Hi [EMAIL PROTECTED]: when you do an 'open, you don't get a file!, but a port. so change your func this way: fprint: func [fp[port!] value[string!]] [ either not-equal? fp none [ ; thou canst write on a port, just append ... append fp value append fp newline ] [ print value ] ] regards, Ingo -- _ ._ ingo@)|_ /| _| _ ingo@| |(_|o(_)| (_| http://www.2b1.de/Rebol/ ._| ._|
[REBOL] [REBOL]Problem with function argument Re:
Hi, - Puvodní zpráva - Od: <[EMAIL PROTECTED]> Komu: <[EMAIL PROTECTED]> Odesláno: 2. dubna 2000 7:33 Predmet: [REBOL] [REBOL]Problem with function argument > Hello All: > > I am getting an error message when attempting > to pass a value as a argument to a user-defined > function: > > the error message is: > fprint expected fp argument of type: file. > ** Where: fprint fp "line one" > > code follows: thanks in advance to all!! > tim > > ; first the function > fprint: func [fp[file!] value[string!]] > [ > either not-equal? fp none > [ > write/append fp value > write/append fp newline > ] > [ print value ] > ] > fp: none > ; check for server name in cgi environment variables > either equal? system/options/cgi/server-name none > >; value is empty, presume the script is run from commad line >print "no server name, application run from console" >fp: open/new/write %hello.htm > ] > ; script is invoked via server, yes, I know we need > ; a content-header string, etc here. > > prin "Server name: " > print system/options/cgi/server-name > ] > ; call function > fprint fp "line one" ; rebol doesn't like this > ; if the file has been opened, close it > if not-equal? fp none [close fp] > The header of your function should probably be as follows: fprint: func [fp[port! none!] value[string!]] to allow open ports and none as the valid arguments. Moreover, the following: > either not-equal? fp none > [ > write/append fp value > write/append fp newline > ] > [ print value ] > ] will not work for ports. You can change it to: either fp [ append fp value append fp newline ] [print value] Regards, Ladislav
[REBOL] [REBOL]Problem with function argument Re:
>> do %test.r no server name, application run from console ** Script Error: fprint expected fp argument of type: file. ** Where: fprint fp "line one" if not-equal? >> type? fp == port! > fprint: func [fp[file!] value[string!]] >> port! = file! == false I hope that helps. Andrew Martin Awakening the dragon... (in another universe) :-) ICQ: 26227169 http://members.xoom.com/AndrewMartin/ -><- - Original Message - From: <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Sunday, 2 April 2000 5:33 PM Subject: [REBOL] [REBOL]Problem with function argument > Hello All: > > I am getting an error message when attempting > to pass a value as a argument to a user-defined > function: > > the error message is: > fprint expected fp argument of type: file. > ** Where: fprint fp "line one" > > code follows: thanks in advance to all!! > tim > > ; first the function > fprint: func [fp[file!] value[string!]] > [ > either not-equal? fp none > [ > write/append fp value > write/append fp newline > ] > [ print value ] > ] > fp: none > ; check for server name in cgi environment variables > either equal? system/options/cgi/server-name none > [ >; value is empty, presume the script is run from commad line >print "no server name, application run from console" >fp: open/new/write %hello.htm > ] > ; script is invoked via server, yes, I know we need > ; a content-header string, etc here. > [ > prin "Server name: " > print system/options/cgi/server-name > ] > ; call function > fprint fp "line one" ; rebol doesn't like this > ; if the file has been opened, close it > if not-equal? fp none [close fp] > >
[REBOL] [REBOL]Problem with function argument
Hello All: I am getting an error message when attempting to pass a value as a argument to a user-defined function: the error message is: fprint expected fp argument of type: file. ** Where: fprint fp "line one" code follows: thanks in advance to all!! tim ; first the function fprint: func [fp[file!] value[string!]] [ either not-equal? fp none [ write/append fp value write/append fp newline ] [ print value ] ] fp: none ; check for server name in cgi environment variables either equal? system/options/cgi/server-name none [ ; value is empty, presume the script is run from commad line print "no server name, application run from console" fp: open/new/write %hello.htm ] ; script is invoked via server, yes, I know we need ; a content-header string, etc here. [ prin "Server name: " print system/options/cgi/server-name ] ; call function fprint fp "line one" ; rebol doesn't like this ; if the file has been opened, close it if not-equal? fp none [close fp]