Stephen Woolerton's blog

Intro to Packages and NameSpaces

Tagged:  •  

In GNU Smalltalk package loading and namespaces are separate.

If like me you've come from Java, it takes an example to see what this means in practice.

Here is a very simple example using sockets (found in TCP package up to 3.0.x, and Sockets package in 3.1). The example below is for 3.1 so the Sockets package is used. Sockets package documentation is at...

What I tried to do first was this...

 #!/usr/local/bin/gst -f
       PackageLoader fileInPackage: 'Sockets'.

Mail scripting

I recommend you browse the NetClients package to see methods to show POP mail attributes, e.g. sender, destination, content.

POP mail snippet

   PackageLoader fileInPackage: 'NetClients'.
   popHost     := ''.
   popUsername := 'popUser'.
   popPassword := 'POPpassword'.
   client := NetClients.POP.POPClient connectToHost: popHost.
       username: popUsername
       password: popPassword.
       client login.
           getNewMailMessages: [:mimeEntity |
           | subject from |

Scripts - file and folder enumeration

And here's one to navigate through a home directory folder and change ownership of all folders to the home directory owner.

This script was required after the home directories got copied onto another server with a different operating system.

This works in Smalltalk 3.0x (I understand there are changes coming in File/Directory in 3.1)


#! /usr/local/bin/gst  -f

Object subclass: #DirNav
       instanceVariableNames: ''
       classVariableNames: ''
       poolDictionaries: ''
       category: 'SG-Scripting'!

DirNav comment:

Scripting example - regular expressions and Dictionaries

And here's another one with some regular expression comparisons.

The occasion being a requirement to link some OS X mailservers together. Unfortunately Apple didn't compile LDAP into Postfix so we're using output from the Apple Directory to generate text files for inter-branch mail routing.

  1. !/usr/local/bin/gst -f

"read a text file with LDIF records and generate a Postfix virtuals file

 from the records.
 Require uid and keyword attributes.
  - uid is home directory shortname and mail aliases e.g. jim, jim.bob

System calls

Here is an example of how to make a system call from a script and get console output.
The call is asynchronous - it returns after a time whether the process has completed or not.

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

Synchronous calls:
To make an operating system call and block until the command has completed (synchronous). The output is an integer. Here is an example:-

shellOut := Smalltalk system: 'ldapadd -x -f /project/LDAPImport.ldif -D binddnhere -S passwordinfilehere'

Syndicate content

User login