Welcome to the wonderful world of programming! For the purposes of this book, I’m going to assume that you have some general computer experience, but have little or no programming experience. I trust that you will enjoy the journey and pick up some valuable new skills along the way.
Even though this book is written with the new programmer in mind, have no fear if you are an accomplished developer. The language we’ll be looking at, Euphoria, has plenty of features to make you look like a miracle worker. Once you’ve tasted what Euphoria has to offer, you’ll never go back to pointers again!
What
exactly is programming?
Well, back in the old days of computers, programmers needed to literally feed (with punch cards) instructions to the machine telling it every little action it needed to perform. These instructions included such things as where to find the data it needed to work on, when to add or subtract numbers, comparing numbers, and printing results.
If you think this sounds familiar, you’re right. Even though computers have gotten smaller and faster, the same basic tasks need to be accomplished. Of course now we have a lot more options in how to tell the computer what it needs to do. The set of instructions are what we call programs. The tool we use to create programs is a language.
You’ve probably heard of such languages as C, BASIC, Java, Perl, and Python. Each has its advantages and disadvantages, and no language is going to be the perfect tool for every job – not even Euphoria. With so many languages to choose from, how do I choose the best tool for the job? A lot of it comes from personal preference, either my own or my supervisor’s. If I want to write a program telling my computer how to talk to a new device, I’d probably use C. For a program that requires a lot of communication between different networks, I’d pick Perl or Python. But for most everyday projects that need to be written quickly and perform well, Euphoria definitely outperforms the other tools available. It is easy to learn, easy to use, creates programs that use little disk space, and generally never crashes (but your programs might until all the bugs are worked out).
Before we get started, I’d like to mention a few basics about programming. If you’re looking to get rich quick by writing the next super-cool high-speed game, you’re going to be disappointed. Almost all the popular games are written by teams of programmers with a great deal of skill and experience in math, physics, and art. The creation of a single game will take a team of 10-20 people several years to develop. Does this mean you can’t write games? Not at all. Most programmers start out by writing games. And many of them are very well-done and very popular. But except in very rare cases, they don’t make any money. Most of the money in programming comes from writing specialized solutions for businesses, which requires a great deal of research and understanding about the industry you are writing for. With that said, as both a hobby and a profession, programming can be extremely rewarding.
I’d like to mention a few rules about good programming that are sure to drive the folks who like to just push all the buttons crazy.
Rule #1: Think forward. You need to plan ahead what you are trying to accomplish and how you plan to get there. This may mean creating an outline or a flowchart; or it may mean drawing a series of screen shots like a storyboard. The method you use to plan is not important, but the planning itself is.
Rule #2: Work backward. Most people are used to thinking in the most general terms possible. Think about how you would teach someone to make a cup of coffee. How many steps did it take you? If you came up with more than 10 steps, you’re doing quite well. Now see if you can break any of those steps down into something more specific. If you think really hard about it; including such things as finding where the spoon is, where to get water, how to measure the water, how to tell when the coffee is finished, what to do with the coffee when it is finished, etc.; you have just discovered what programmers must do all the time. Programming frequently involves going back over the steps and filling in more and more details. That’s one of the reasons we do so much planning ahead of time.
Rule #3: Start with zero. Okay, this rule doesn’t apply as much in Euphoria as it does in other languages, but it’s still good to know. Computers generally start counting from zero, where people usually start counting at one. Euphoria makes life a little easier for the programmer by allowing us to start counting from one like we are naturally inclined to. Just remember that what may come naturally for you will generally not come naturally for the computer and vice versa.
Rule #4: Always use a pencil. The thing about programming is you will constantly be changing your program. No program is every really finished. There will always be mistakes to fix (bugs), features to add, and places where you just can’t figure out how to make it work. There may be times when you spend more time erasing code than creating it. Don’t worry. It will sometimes be better to delete large sections of a program and start over from scratch than to try to find one or two hidden bugs that prevent the program from working the way it’s supposed to. Often you find a better way to do something the second or third time around that makes your program more in line with the Four C’s.
The
4 Cs.
A well-written program will be clear, correct, concise, and compatible.
Clear: A clear program is well-documented, well laid out stylistically, using meaningful variable and routine names (if you don’t know what I’m talking about just yet, keep this in the back of your hat; we’ll be pulling it out soon enough) and verbosely commented. One of the most difficult tasks in programming is to pick up someone else’s program, or even your own a few months after you last worked on it, and try to figure out what it’s doing. Following this principle will make your job, and anyone else’s looking at your code, much much easier.
Correct: This one should be obvious. The program shouldn’t crash on unexpected data, it should provide an accurate result every time; and above all, it should perform in the way the user expects it to. We’ll get into debugging syntax and logical errors later. However, it’s important to remember this: a perfect program in every other respect that has a difficult or unintuitive user interface will be considered a failure. Knowing your users and what they expect is as much a part of good programming as accurate number-crunching.
Concise: This principle may seem to be at odds with the other three, but it really isn’t. If a task can be accomplished in 10 lines instead of 100, make it shorter. Use the other 90 lines to document what you did in the 10. Comments aren’t executed, take up no memory when your program is run, and make it clearer what is going on. Reuse code whenever possible. Smaller code runs faster and cleaner (usually), so always be on the lookout for ways to optimize your algorithms.
Compatible: Forgive my abuse of the English language here, but I needed another C-word. As time goes on, more and more people are using more and more different types of machines. Currently, Euphoria runs on DOS, Windows, Linux, and FreeBSD. There are people working on versions for Mac, Palm, HP-UX, and several other platforms. While the majority of programs you write will be written for either DOS or Windows (based on ownership statistics), it is important to keep your code organized in such a way to allow easy conversion to a different platform. Another application of this principle is writing your code in such a way as to allow it to be reusable. You should be able to copy a section of code from one program, and paste it into a completely different program, and have it work with as few changes as possible.
Euphoria makes it very easy to follow these principles. Without a goto, it is easy to keep your code clear. There is no jumping back and forth between six different pages trying to figure out where your code is going to execute next. Euphoria’s “define-it-first” principle means that all variables and routines are created prior to their use in the program. There are some cases where this is inconvenient, so Euphoria has a work-around that lets you have your cake and eat it too.
Euphoria is both safe and powerful, making it easy to write correct programs that won’t crash. But in those cases where you need to do dangerous programming, the language provides the means to get down and dirty with the lowest risk of crashing your system of any language.
Euphoria’s small footprint makes it easy to write Windows programs that fit on a floppy disk, as opposed to programs that require three CD-ROMs. Bigger is not always better!
Euphoria’s flexibility in statement use and variable use mean the power of programming is in your hands. It’s up to you to make the well-written programs, and Euphoria will take care of the rest. I hope you enjoy learning this great tool as we embark on the journey of great programming.
First things first. If you haven’t done so already, you’ll have to get a copy of the public domain version of Euphoria. Point your internet browser to http://www.rapideuphoria.com and follow the instructions on the page to download the language. While this book is going to be focusing on DOS and Windows programming, most of the features of the language are the same for all platforms. I will try to point out platform differences as they come up, so if you want to work off Linux or FreeBSD, simply download the version for your platform.
The first major difference between the two platforms is the extraction procedure. Assuming you run at least Windows 95, there is a convenient Windows installer based on the Inno program that will take care of Windows and DOS. The Linux / FreeBSD version requires you run tar. Much of the information in this section is taken directly from RDS’s website. As newer versions and platforms become available, check there for the latest installation instructions.
After downloading e23setup.exe, double-click the file to begin the installation. The standard intro screen will come up, so just click “Next”. The next screen is some important information about what to do later, so you should read through this and write down anything you might want to remember. Clicking “Next” brings you to the install directory page. The default directory is C:\Euphoria, but you can change this to any place you like. If the directory does not exist, it will be created. If it does exist, all files in that directory will be placed in a “Backup” subdirectory. Click “Next” to move to the Icons folder page. Unlike most Windows installations, you actually have an option for adding no icons to your Start menu or desktop. Just check the “No Icons” box. Clicking “Next” from here begins the file extraction.
A few files required by the setup program will be placed in your Windows\Temp directory. Other than that, everything is placed in the Euphoria directory specified earlier. No DLLs will pollute your Windows or System directory, and no strange keys will be added to your registry. Everything you need is self-contained in one managable folder. Your autoexec.bat file will be modified in three ways: the path to ex.exe will be added to your PATH= line, an environment variable EUDIR will be set to the root of your Euphoria directory, and an environment variable EUINC will be set to your include directory. The EUINC variable is used to specify a search path for include files not located in the Euphoria directory. If you don’t understand about include files, don’t worry – we’ll get to them a bit later.
Once the files have been extracted, there is a page of notes regarding what to do next and how to uninstall Euphoria should you ever decide to do so. Just click “Next”, then “Finish”. Before using the DOS version of Euphoria (including the editor), you need to manually reboot your computer so the autoexec changes can be applied.
If you are running Windows 95 or 98, don’t worry about the environment variables because autoexec.bat is the only place they exist. For Windows NT, 2000, or XP users, you may need to set your environment variables manually. Right click on “My Computer” and select “Properties” from the pop-up menu. Click on the “Advanced” tab and click the “Environment variables” button. If they are not already defined at they system level, create EUDIR and set it to the root of your Euphoria directory. Then create EUINC and set it to your personal include directory. If you don’t have one yet, don’t worry about it. After you’ve written a few programs and installed a few libraries, you will want one. Just come back to this section and add it or change it later when you need to.
If you chose to have the icons placed in your Start menu, you will see five of them: Euphoria DOS interpreter (ex.exe), Euphoria Windows interpreter (exw.exe), Euphoria DOS Editor (ed.ex – run by ex.exe), Euphoria reference manual (refman.htm & library.htm), and a link to www.RapidEuphoria.com.
If you have a Windows editor that you like to use (including some written in Euphoria available on the Recent Users Contributions page of the Euphoria web site), open up “My Computer”. Select the “View” menu, “Folder Options”, and the “File Types” tab. Look for Euphoria DOS application and select “Edit”. Click “New Action”, type “edit” in the action box, and the filename of your favorite editor. Do the same for Euphoria Windows applications. To invoke the editor, right-click on the .ex or .exe file and choose “edit”. This will open the file in your editor.
If you have just finished installing Euphoria for DOS/Windows, go ahead and skip down to “Running your first program.” For those of you using Linux or FreeBSD, follow these instructions (if you don’t know what Linux or FreeBSD are, you’re not using them; so skip ahead to the next section).
After downloading euphor23.tar to the directory you wish to install Euphoria in, run the following command from the command line:
tar –xvz –f euphor23.tar
This will extract the Euphoria program and utilities to and new directory underneath the current one named ./euphoria. You must set the environment variables in your .profile script manually. A full PATH must be set to the euphoria/bin directory, the EUDIR variable must be set to the euphoria directory, and the EUINC variable must be set to the directory where you will store your own include files. If you have root privileges and installed euphoria in the /usr directory, you may want to set the PATH, EUDIR, and EUINC variables in the /etc/.profile script so all users can enjoy the benefits of Euphoria. Also, if it wasn’t done automatically on your system, don’t forget to set execute privileges for ./euphoria/bin/exu.
FreeBSD users cannot use the exu program included in the .tar file. You must download www.rapideuphoria.com/eubsd23.zip and unzip the new exu file. Simply copy the new exu file over top the one in ./euphoria/bin and restore the execute permissions.
For the examples in the first part of this book, I will be using the DOS version of the interpreter, ex.exe. This means typing something like the following at the command line:
ex myprog.ex “param1” “param2”
If you are running from Windows and have your associations set up as explained above, you can double-click myprog.ex to have it run. Windows programs can also be run from the command line by replacing ex with exw.
In Linux or FreeBSD, if the first line of your program is something like this,
#!/usr/euphoria/bin/exu
and the permissions of your program are set to execute, you can simply type this at the command line:
myprog.eu “param1” “param2”
You can also run programs the same as in DOS by replacing ex with exu.
Euphoria programs do not require any special extensions to run, but some conventions are good to continue (caveat: for Windows associations to work, the correct extensions must be used).
.ex - DOS program
.exw - Windows program
.exu - Linux / FreeBSD program
.e - DOS include
.ew - Windows include
.eu - Linux / FreeBSD include
What are we going to edit? Well, the first thing we can look at is one of the demo programs. Change directories to \euphoria\demo. Type the following at the command line:
ex ed.ex sanity.ex
Because the editor is so commonly used, a batch file has been created to make it easier to launch. Just type the following from the demo directory:
ed sanity.ex
The ed.bat file just replaces the ex ed.ex that is normally run from the command line.
When the editor comes up, you will see the program in multiple colors appear on a gray background. You can navigate around the program using the arrow keys, page up, page down, home, and end keys. Because this is a simple demonstration of Euphoria’s abilities, some features you may be used to are not available: mouse support, drop-down menus, and configuration windows. Now before you give up and say, “Is this all it can do?” I want to encourage you that this is the most basic of editors. There are many others available in the “Recent User Contributions” page and “Archives” pages of the Euphoria website. Two that have been repeatedly recommended (although there are many other very good ones available) are M Editor submitted by Pete Lomax and Enhanced IDE submitted by Judith Evans. If you are used to fuller-featured development environments, I suggest you take a look at these. You can also search the Euphoria website for all the editors available (56 entries as of this writing).
Back to the included editor, let’s highlight a few basic commands. All file commands can be performed by pressing the <ESC> key. The most common ones you will use are <ESC> then <q> for quit, <ESC> and <e> to execute the current program, <ESC> then <n> to open a new file, <ESC> and <c> to split the screen into two or more buffers, and <ESC> then <w> to save the current program to disk. <ESC> and <f> will let you look for a specific word or phrase. One other question that many people ask is about cutting and pasting. Deleting a character can be done with either the <Backspace> or <Delete> key. Cutting a whole line is done with <CTRL><D>. You cannot copy a line to the clipboard, you can only cut the line. Pressing <CTRL><D> multiple times cuts all the lines deleted to the clipboard. To paste the whole set of lines, simply press <Insert>. You can <Insert> as many times and in as many places as you like. You can also <Insert> in separate sessions by pressing the corresponding <Fn> key before pressing <Insert>.
The next chapter will get you started programming in Euphoria. A lot of people recommend downloading as many programs from the website as possible and trying to figure out how they work. Another method of learning the language is to play with the demos. As you get more comfortable programming in Euphoria, you may want to add some features to ed.ex. This is not only allowed, but also encouraged. The only way to learn how to do it is by practicing. I would like to suggest, however, that before playing with any of the demos, you make a backup copy of the files. This will let you start over if you make too many mistakes.
Speaking of mistakes, you will make some. You will make a lot. That’s all part of programming. If you get too far stuck, don’t be afraid to ask questions. There is a very active community of programmers at topica.com. Look for EuForum@topica.com. To subscribe to the mailing list, send a blank message to EuForum-subscribe@topica.com. There are about 70 messages a day, but there is also a digest mode. If receiving this much email concerns you, you may also be interested in the web interface. The Euphoria community welcomes you, and looks forward to helping you on your journey.
Next chapter….Hello, hello,
hello.
Euphoria is a product of Rapid Deployment Software. Windows is a registered
trademark of Microsoft Corp. Many
thanks to Jonas (jktemple@…..) for the tip on setting Windows NT/2K/XP
environment variables.