Pascal in 30 lessons
The lessons use Free Pascal. fpc compiles your file into one program that starts by itself and needs no other file.
The 30 lessons
Chapter 1
First steps
Print text, keep values in variables, calculate and read what the user types.
- 1Compile a program
fpccompiles the source file into a program. - 2Write in columns
Writeprints and stays on the line;WriteLnends it. - 3Declare variables
varlists each name with its type:string,Integer,RealorBoolean. - 4Count with numbers
divandmodwork on whole numbers,/gives a fraction. - 5Work with text
Length,Copy,Posand the tools of the unitSysUtilswork on text. - 6Ask the user
ReadLnreads a line of text, or several numbers on one line.
Chapter 2
Decisions and loops
Use conditions to choose what to do and loops to repeat it.
- 7Decide with if
if,thenandelsechoose a block;beginandendgroup several statements. - 8Join conditions
and,orandxorjoin Boolean values,notturns one round. - 9Choose with case
casecompares one value with letters, lists and ranges. - 10Repeat with loops
forcounts up or down,whiletests first,repeattests last. - 11Check what the user typed
TryStrToIntreads a whole number from a text and says whether that worked. - 12A loop in a loopThe inner loop prints one row, the outer loop starts the next one.
Chapter 3
Data, functions and the command line
Keep many values together, write functions and read the command line.
- 13Keep many values
LowandHighgive the first and the last index of an array,Lengththe number of its elements. - 14Let an array grow
SetLengthchanges the size while the program runs,for ... invisits every item. - 15Name a block of linesA
constparameter can only be read. - 16Give a value backA function sets
Result. - 17Join fields into a recordA record keeps the slot, model and temperature of one disk together.
- 18Use your own valuesAn enumeration names the days, a set holds some of them,
inasks whether a day is there.
Chapter 4
Files and errors
Read and write files and handle errors such as a missing file.
- 19Write and read a file
AssignFilelinks a variable with a file name. - 20Handle an error
try ... exceptcatches an error,try ... finallyalways runs the last part. - 21Read the command line
ParamCountgives the number of arguments andParamStr(1)the first one. - 22Split a program into unitsA unit shows its functions in the
interfaceand keeps the code in theimplementation. - 23Build a calculatorPut
Split,TryStrToFloatandcaseinto one program. - 24Keep lines in a list
TStringListsorts, searches and reads name=value pairs.
Chapter 5
Tools for the NAS
Programs that read the state of the NAS and report it.
- 25Write a classA class joins data with the methods that may change it; a constructor makes an object and
Freeends it. - 26Copy a file with a date
FormatDateTimemakes the name andTFileStreamcopies the file. - 27Walk through folders
FindFirstandFindNextread a folder, and the procedure calls itself for each subfolder. - 28Ask the system for free space
fpStatFSasks the system about the volume of a folder: how many blocks it has and how many are free. - 29Build a notepadA
TStringListloads and saves the notes;casechoosesadd,listordelete. - 30Finish a tool
TIniFilereads the limit,RunCommandcallsgrepand the program saves the list asreport.txt.
Projects
Longer programs that draw a menu, play a game, greet you at sign-in or show the state of the NAS. Each page shows the screen as the program drew it on a test NAS.
- A menu bar with drop-down boxes in Free PascalA Free Pascal program draws a desktop with a menu bar, drop-down boxes and dialog boxes with shadows, and fills the dialogs with the NAS's memory, load, processor, volumes and RAID groups. You learn to draw boxes with escape codes, read keys with crt and run commands with POpen.
- A greeting screen with large letters in Free PascalA Free Pascal program prints the word NAS in large block letters, greets you by the hour and lists the model, kernel, uptime, load and memory in a framed table. You learn records and dynamic arrays, reading the date with dateutils and drawing a table with box characters.
- A NAS summary in two columns in Free PascalA Free Pascal program reads the CPU, temperatures, load, memory, data volumes and network ports into records and prints them as framed panels in two columns. You learn records, dynamic arrays of records, var parameters and a layout built from lists of lines.
- Cable run: a moving game in Free PascalA Free Pascal game where you steer a network cable round a frame with the arrow keys; it grows with every data packet it picks up, and the wall or its own body ends the run. You learn a game loop with KeyPressed and Delay, records for coordinates and drawing only the cells that change.
- A temperature chart of the NAS in Free PascalA Free Pascal program asks QNAP's getsysinfo for the system, CPU and disk temperatures and draws them as a vertical bar chart with a scale, coloured green, amber or red by level. You learn to build a chart line by line from the top and to treat an empty disk bay as a value of its own.
- A log viewer with colours and search in Free PascalA Free Pascal program shows a log file a screen at a time, colours warnings amber and errors red, scrolls with the arrow and page keys and jumps to the next error with /. You learn to load a file into a TStringList, keep a window of lines on the screen and search with UpperCase and Pos.
Write it
Sign in to the NAS over SSH and type these commands. micro opens an empty file; Ctrl+S saves it and Ctrl+Q closes it.
mkdir -p /share/Public/lessons
cd /share/Public/lessons
micro 29-notepad.pasRun it
fpc makes the program 29-notepad next to the source. The words after its name go to the program.
fpc 29-notepad.pas
./29-notepad add "Buy two disks"
./29-notepad list