Global Variables. Global Dictionary. Pool Dictionaries. Chapter



Similar documents
Instance Creation. Chapter

Copyright 1997 by Alec Sharp PDF-Conversion by Lukas Renggli Download more free Smalltalk-Books at: The University of Berne:

filename := Filename named: 'c:\basedir\file.ext'. filename := 'c:\basedir\file.ext' asfilename.

Simple Document Management Using VFP, Part 1 Russell Campbell russcampbell@interthink.com

The Smalltalk Programming Language. Beatrice Åkerblom

My Secure Backup: How to reduce your backup size

Capturing Network Traffic With Wireshark

Using Term to Pierce an Internet Firewall mini HOWTO

A Guide To Evaluating a Bug Tracking System

5. At the Windows Component panel, select the Internet Information Services (IIS) checkbox, and then hit Next.

Using Google Docs in the classroom: Simple as ABC

Text of Templates

Tunnel Client FAQ. Table of Contents. Version 0v5, November 2014 Revised: Kate Lance Author: Karl Auer

A Java Crib Sheet. First: Find the Command Line

SyncTool for InterSystems Caché and Ensemble.

Subversion Server for Windows

Get Your Project Back In Shape! How A Few Techniques Can Change Your (Project s) Life

SOLoist Automation of Class IDs Assignment

LDAP and Active Directory Guide

Make a folder named Lab3. We will be using Unix redirection commands to create several output files in that folder.

Deploying Microsoft Operations Manager with the BIG-IP system and icontrol

ESA FAQ. Self Administration Frequently Asked Questions

eg Enterprise v5.2 Clariion SAN storage system eg Enterprise v5.6

Setting Up the Site Licenses

QaTraq Pro Scripts Manual - Professional Test Scripts Module for QaTraq. QaTraq Pro Scripts. Professional Test Scripts Module for QaTraq

Part II. Managing Issues

EPiServer Operator's Guide

Installation & User Guide

SUnit Explained. 1. Testing and Tests. Stéphane Ducasse

How To Restore Your Data On A Backup By Mozy (Windows) On A Pc Or Macbook Or Macintosh (Windows 2) On Your Computer Or Mac) On An Pc Or Ipad (Windows 3) On Pc Or Pc Or Micro

About Kobo Desktop...5. About Kobo Desktop...5. Downloading and installing Kobo Desktop...7. Buying ebooks with Kobo Desktop Buying a book...

Version control. with git and GitHub. Karl Broman. Biostatistics & Medical Informatics, UW Madison

How To Link Tomcat 5 with IIS 6 on Windows 2003 Server using the JK2 ajp13 connector

After you install Spark, you'll get going by logging in and adding contacts. Try out a chat with one of your contacts!

Backing up Data. You have lots of different options for backing up data, different methods offer different protection.

Beginning BGP. Peter J. Welcher. Introduction. When Do We Need BGP?

Look for the 'BAERCOM Instruction Manual' link in Blue very near the bottom.

Document Services Online Customer Guide

NRPE Documentation CONTENTS. 1. Introduction... a) Purpose... b) Design Overview Example Uses... a) Direct Checks... b) Indirect Checks...

USC Marshall School of Business ShareFile_With_Outlook_Client_v2.docx 6/12/13 1 of 9

Talk2M ewon Internet Connection How To

Deploying the BIG-IP LTM system and Microsoft Windows Server 2003 Terminal Services

Object Oriented Programming and the Objective-C Programming Language 1.0. (Retired Document)

Why you shouldn't use set (and what you should use instead) Matt Austern

Load Balancing BEA WebLogic Servers with F5 Networks BIG-IP

CA Nimsoft Service Desk

The Settings tab: Check Uncheck Uncheck

Setting Up Your FTP Server

Installation & User Guide

Chronoforums. Written by ClubAero.nl, 8 th December 2013

Chapter 6 Using Network Monitoring Tools

Using the ISCR FTP Program

SlimDrivers User Manual

Frequently Asked Questions

Oracle Application Server for JDeveloper Beginners

Version 1.0 January Xerox Phaser 3635MFP Extensible Interface Platform

mylittleadmin for MS SQL Server 2005 from a Webhosting Perspective Anthony Wilko President, Infuseweb LLC

Datalogic Desktop Utility

Publishing Geoprocessing Services Tutorial

$ftp = Net::FTP->new("some.host.name", Debug => 0) or die "Cannot connect to some.host.name: $@";

Populating Your Domino Directory (Or ANY Domino Database) With Tivoli Directory Integrator. Marie Scott Thomas Duffbert Duff

for Android Windows Desktop and Conduit Quick Start Guide

ibaan ERP 5.2a Configuration Guide for ibaan ERP Windows Client

PHP Debugging. Draft: March 19, Christopher Vickery

Figure 1: Restore Tab

Selling On the Moon. the ecrater experience.

Created With ScreenSteps Trial

IBM SPSS Statistics Version 22. Windows Installation Instructions (Concurrent License)

Role Based Administration for LDMS 9.0 SP2

Regain Your Privacy on the Internet

Cincom Smalltalk. Application Developer's Guide P SIMPLIFICATION THROUGH INNOVATION

Chapter 6 Using Network Monitoring Tools

Microsoft Access Database

Creating, Sharing, and Selling Buzztouch Plugins

Developing In Eclipse, with ADT

Cincom Smalltalk. Installation Guide P SIMPLIFICATION THROUGH INNOVATION

BEGINNER S ADMINISTRATION TUTORIAL for MEDAL OF HONOR: ALLIED ASSAULT by Crow King crowking@autokick.com

T320 E-business technologies: foundations and practice

Laserfiche Web Access 8 and Kerberos Configuration in a Windows Server 2008 and IIS 7 Environment. White Paper

Outlook 2007: Managing your mailbox

Getting Started With Halo for Windows For CloudPassage Halo

Guidelines for Schools

How To Check If Your Router Is Working Properly

Redeploying Microsoft CRM 3.0

User Terminal Service Constituent module IN-Lab report 93/8

SCF/FEF Evaluation of Nagios and Zabbix Monitoring Systems. Ed Simmonds and Jason Harrington 7/20/2009

Jive Case Escalation for Salesforce

Mesa DMS. Once you access the Mesa Document Management link, you will see the following Mesa DMS - Microsoft Internet Explorer" window:

Contents Using Jive Anywhere

Using the Caché SQL Gateway

Configuring Microsoft Internet Information Service (IIS6 & IIS7)

Module 2 Cloud Computing

Load Balancing IBM WebSphere Servers with F5 Networks BIG-IP System

Introduction to Securing Data in Transit

Answers Implementation Guide

REDUCING YOUR MICROSOFT OUTLOOK MAILBOX SIZE

Deploying the BIG-IP System with Oracle E-Business Suite 11i

SDK Code Examples Version 2.4.2

NNMi120 Network Node Manager i Software 9.x Essentials

Kaseya 2. User Guide. Version 6.1

Transcription:

Chapter 7 Global Variables I'll start with the standard caveat that global variables should be used sparingly, if at all, and that most information should stored in instance variables or be passed as method arguments. Having said that, there is a definite use for global information and objects. You may have constants that are used through your system, or you may have global objects such as configuration objects or trace log objects. Let's look at some techniques that can be used for these purposes. We'll use two examples: a configuration object and a timeout value. Global Dictionary The most basic technique is to put global objects in the system dictionary named Smalltalk since objects in Smalltalk can be referenced from anywhere. For example, you might have code that does: Smalltalk at: #Timeout put: 20. Smalltalk at: #Config put: MyConfiguration new. I recommend against using Smalltalk for application globals because it's used heavily by the Smalltalk system (for example, it contains all the classes in the system, and global variables such as Transcript, Processor, and ScheduledControllers). I prefer not to clutter Smalltalk up with application objects or risk name collisions. Pool Dictionaries The next technique is to use a PoolDictionary. A PoolDictionary contains objects to which you want global access for your application. The nice thing about a PoolDictionary is that classes have to register an interest in it before they can access its objects, so it allows you a certain amount of scoping. The way a class registers an interest is to name the PoolDictionary in the appropriate line in the class definition template. (In VisualWorks 2.0, subclasses inherit the PoolDictionary, but in VisualWorks 2.5, you have to explicitly name the PoolDictionary in each subclass that wants to reference PoolDictionary variables.) In the example below, I've called it MyPoolDictionary. NameOfSuperclass subclass: #NameOfClass instancevariablenames: 'instvarname1 instvarname2' Copyright 1997 by Alec Sharp Download more free Smalltalk-Books at: - The University of Berne: http://www.iam.unibe.ch/~ducasse/webpages/freebooks.html - European Smalltalk Users Group: http://www.esug.org

Global Variables 2 classvariablenames: 'ClassVarName1 ClassVarName2' pooldictionaries: 'MyPoolDictionary' category: 'MyStuff' Before you write code you must have done two things. First, before referencing the PoolDictionary in your class definition you have to make the name of the PoolDictionary universally known by adding it to the Smalltalk dictionary. To do this, execute the following. Note that we are using a Dictionary and not an IdentityDictionary. Because of the way an IdentityDictionary is implemented, it will not work as a PoolDictionary. Smalltalk at: #MyPoolDictionary put: Dictionary new. Before you write code referencing the objects in the PoolDictionary, you have to add these objects to the PoolDictionary so that the compiler can associate a variable name in your method with an object in the PoolDictionary. However, the compiler just needs to be able to reference the name in the PoolDictionary and doesn't care about the type of the object. For the purpose of accepting your method, the easiest thing to say initially is: MyPoolDictionary at: #Config put: nil. We don't want to do these steps manually every time we file in our code (see Chapter 33, Managing Source Code, for more information on managing projects and filing in code). So, at the beginning of the file that files in our code, we add the following. Smalltalk at: #MyPoolDictionary put: Dictionary new. MyPoolDictionary at: #Config put: nil. If you have code that refers to PoolDictionary variables and you create a new PoolDictionary, the code will no longer be able to find the old variables. If you get an error in a method because a PoolDictionary variable is not found, and yet a PoolDictionary exists with the appropriate variable, recompile the method by making a small change and accepting the method. If this solves the problem, somewhere you created a new PoolDictionary while code still references the old one. The question that always comes up when using a PoolDictionary is how do you find all the references to an object in your PoolDictionary, since you can't browse References To or Implementors Of? The answer is to evaluate the following code: Browser browseallcallson: (MyPoolDictionary associationat: #Config). It might be worth putting something like this in your Workspace and saving your image. VisualWorks 1.0 had a menu option to open the System Workspace which, among other useful code samples, had code to find PoolDictionary variable references. The System Workspace is not obviously available in VisualWorks 2.0 or 2.5, but you can get it by executing: ComposedTextView opensystemworkspace. Advantages of using a PoolDictionary are: you refer to the objects in a PoolDictionary by a name starting with an uppercase letter, which makes it very obvious that the code references a global object; and, only classes with an interest in the PoolDictionary have access to it. Disadvantages of PoolDictionaries are: you have to take

Global Variables 3 specific actions to allow PoolDictionary objects to be referenced, and it is harder to find all the references to the PoolDictionary object. Class side variables The next technique is to use a class to store global data. For example, we might have two classes: MyGlobals and MyConstants. Global and constant values are referenced by sending messages to the appropriate class. For example, we might reference and define a timeout value as follows. MyClass>>myMethod timeout := MyConstants timeout.... MyConstants class>>timeout ^20 We might reference and initialize a global value (in this case a Log file) as fillows. MyClass>>myMethod... MyGlobals logfile log: 'salary=', salary printstring. MyGlobals class>>logfile ^LogFile MyGlobals class>>initialize LogFile := MyLogFile on: self logfilename. To find all references to an individual global, we can browse references to the message that returns the global. To see all the places that any global is used, we can browse class references for MyGlobals. Similarly, for constant values. Unlike with variables in a PoolDictionary, we don't have to do anything special on filein, and we don't have to make sure that the reference contains an object when we write methods that refer to constants and globals. The only disadvantages of this approach over a PoolDictionary are that you can't restrict scope in the same way, and you have a class that does not act as a factory for instances. Default instances There's one other thing to talk about since we are on the topic of class side behavior. Sometimes you have a single instance of a class that you want to access from many places. Our Configuration object may be such an example. There's a definite temptation to say that there will never be more than one instance of this class, so we might as well just dispense with the instance and put all the information and behavior on the class side. That way you can access the behavior easily since class names are globally known. Despite the temptation, it should be resisted. There's another way that allows similar ease of access without preventing you from creating additional instances. Suppose the class is MyClass. Define a class variable called

Global Variables 4 Default. On the class side, create a class initialization protocol with an initialize method 1. In the class side initialize method do the following: MyClass class>>initialize "self initialize" Default := self new. You'll probably need to initalize the instance so you'll either need to write new, which invokes initialize, or in the class initalize method do Default := self new initalize. Then on the class side write a default method: MyClass class>>default ^Default In your application code, you can now write code such as: length := MyClass default length. If you want to get really fancy, you can assume that instance side messages being sent to the class are really intended for the default instance. You'd write the last example as: length := MyClass length. Given that the class doesn't understand the length message, how do we make this work? We can implement length on the class side as ^self default length, or we can implement the method doesnotunderstand: on the class side as follows. MyClass class>>doesnotunderstand: amessage ^self default perform: amessage selector witharguments: amessage arguments I don't particularly recommend this way of coding because it's not obvious how it's working, but hey, it's ideas like this that make Smalltalk so much fun. If you decide to use this technique, make sure your class comments contain explicit information about what you are doing. Environment Variables / Command Line Arguments Environment variables and command line arguments are not global variables, but since the information is globally available, and varies depending on the environment and how the image was invoked, we'll mention them here. The following returns an array containing the command line arguments. The first element is the program name and the second is the image name. Any additional array elements will be other command line arguments you added. CEnvironment commandline. 1 Class initialization is only done on filein so it's a good idea to always put self initialize in comments at the start of class side initalize methods. That way you can reinitialize the class if you make changes to initialize. Of course, you can select the whole method and doit, but having self initialize makes it more obvious that it may need to be done.

Global Variables 5 You can also retrieve environment variables using getenv:. For example, CEnvironment getenv: 'LOGNAME' Image Version Number The version number for your image can be discovered in one of two ways. You can send the version message to Smalltalk, or the versionid message to ObjectMemory. For example, on a VW 2.0 system, I get the following. Smalltalk version 'VisualWorks(R) Release 2.0 of 4 August 1994' ObjectMemory versionid #[42 26 35 0 20 0 0 4 42 26 35 4]. In particular, element 5 of the array returned by versionid is the image major version number. VW 2.0 gives 20, while VW 2.5 gives 25. So, to discover if you are using VW 2.0 you could do either of the following. (Smalltalk version findstring: '2.0' startingat: 1) > 0 (ObjectMemory versionid at: 5) == 20