smalltalkgnu-smalltalk

Not understanding basic symbol '|'


I am just starting to use gnu-smalltalk. I have taken following code from here and trying to run it with gst command.

display_etc
   | pipe |
   pipe := FileStream popen: 'ls -l /etc' dir: FileStream read.        
   Transcript showCr: pipe contents. !   

But it is giving error that it is not understanding basic symbol |:

$ gst dir_etc.st 
Object: nil error: did not understand #|
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
UndefinedObject(Object)>>doesNotUnderstand: #| (SysExcept.st:1448)
UndefinedObject>>executeStatements (dir_etc.st:2)
dir_etc.st:3: expected expression

Where is the problem and how can it be solved. Thanks for your help.


Solution

  • The confusion here is a distinction between the definition of a complete method (function), which includes (what in C would be) the method/function header and the method/function body. The header is the name of the method with any formal parameters, while the body of the method is the code that is executed when the method is called.

    In your example, you are copying a full method and executing it as if it were just a block of code. Thus, something that is legal at the beginning of a block of code is not at the beginning. The compiler thinks you are "sending the message #|" which is illegal.

    If you leave off the method name (function header), then the code block begins with the vertical bar (pipe), which is legal syntax for declaring variables.