Beginning Programming All-in-One For Dummies. Wallace WangЧитать онлайн книгу.
created commercial programs using LISP.
Scripting Languages
Languages, such as C and C++, are often dubbed system programming languages because they can create programs that access and manipulate the hardware of a computer, such as an operating system (for example, Linux or Windows) or a utility program (for example, an antivirus or anti-spyware program). However, using system programming languages, like C++, for everything can get clumsy. Instead of writing an entirely new program from scratch using a system programming language, more people are likely to use an existing program and customize it in some way. Programming languages that customize existing programs are typically called scripting languages.
Scripting languages work with one or more existing programs and act as “glue” that connects different parts of an existing program together. For example, Microsoft Office consists of several programs including a word processor (Microsoft Word), a spreadsheet (Microsoft Excel), and a database (Microsoft Access). By using the scripting language that comes with Microsoft Office, you can write a program that can automatically yank information from an Access database, create a chart from that information in an Excel spreadsheet, and then copy both the data and its accompanying chart into a Word document for printing.
Trying to yank information from a database, create a chart with it, and print the data and chart using a system programming language, like C++ or Java, would mean creating everything from scratch including a database, a spreadsheet, and a word processor. By using a scripting language, you use existing components and simply “glue” them together. The existing components do all the work, while the scripting language just passes the data from one component to another.
Because scripting languages work with existing programs, they differ from traditional programming languages (like C++ or Java) in two important ways:
Because scripting languages work with one or more existing programs, scripting languages are usually interpreted rather than compiled. Therefore, if someone else wants to run your program, written in a scripting language, they need the source code to your program along with all the programs your scripting program needs, such as Microsoft Word and Microsoft Access. As a result, scripting languages are used less to create commercial applications and more to create custom solutions.
To make scripting languages easy to understand and use, even for nonprogrammers, most scripting languages are typeless languages. System programming languages, like C++ and Swift, are strongly typed or type-safe languages. Strongly-typed languages force you to define the type of data your program can use at any given time. So, if your program asks the user to type a name, a strongly typed language makes sure that the user doesn’t type in a number by mistake. This protects a program from accidentally trying to manipulate the wrong type of data, which could crash the program as a result.In comparison, typeless languages don’t care what type of data the program stores at any given time. This makes writing programs much easier because your program assumes if it’s going to yank data from a particular program, such as Microsoft Excel, the data is probably going to be the right “type” anyway, so type-checking would just be restrictive and tedious.
Scripting languages are typically used in four different ways:
To automate repetitive tasks
To customize the behavior of one or more programs
To transfer data between two or more programs
To create stand-alone programs
Automating a program
At the simplest level, scripting languages (also called macro languages) can automate repetitive tasks that essentially record your keystrokes so you can play them back at a later time. For example, if you regularly type the term Campylobacteriosis (a disease caused by the Campylobacter bacteria), you have two choices:
Type the term manually, and hope that you spell it correctly each time.
Type the term just once (the easier solution), record your keystrokes, and use those captured keystrokes to create a scripting language program that you can save and run in the future.
Figure 3-6 shows a scripting language, Visual Basic for Applications (VBA), that has captured keystrokes and saved them in a VBA scripting language program.
FIGURE 3-6: Recording keystrokes automatically creates the equivalent VBA code in Microsoft Word.
Customizing a program
Besides letting you automate a program, scripting languages also let you customize a program, which can make the program easier to use. For example, you may have a spreadsheet that calculates your company’s invoices. However, to use this spreadsheet, you need to know the specific place in the spreadsheet to type new invoice information. Type this information in the wrong place, and the spreadsheet doesn’t work right.
To avoid this problem, you can write a program in a scripting language that can display a window with boxes to type in new invoice information. Then the scripting language program automatically plugs that new information in the correct place in the spreadsheet every time.
For even more power, a scripting language can combine automation with customization to make programs perform tasks on their own. By using VBA in Microsoft Office, you could write a VBA program that tells your computer to copy data from an Excel spreadsheet, paste it into a Word document at a specific time each day, and then save your document.
Transferring data among multiple programs
Built-in scripting languages can help you automate or customize a program, but what if you use a program that doesn’t include a scripting language? Or what if you need to transfer data between two or more programs, but neither program uses the same scripting language? In these cases, you’ll need to use a scripting language that isn’t tied to any particular program, such as JavaScript, Perl, Python, or Ruby.
When scripting languages link two or more programs together, the scripting language programs are often referred to as glue. So, if you have a web page that lets users type in their names, addresses, and credit card numbers, and a database program that stores customer information, you could use a scripting program to glue the web page to the database. The user would type information into the web page, and the scripting language would then yank this data off the web page and shove it into the database.
By gluing programs together, scripting languages let you combine existing programs to create custom applications. Because scripting languages are interpreted rather than compiled, they can run on any computer with the proper language interpreter. So, whether you use Linux, macOS, or Windows, you can still use the same scripting language (and programs) on different computers.
Creating stand-alone programs
If you wanted to create your own program, you could write everything from scratch. So, if you wanted to include features of a spreadsheet and a database, you would have to create your own spreadsheet and database.
Obviously this would be difficult to do, so by using scripting languages within existing programs, you can create custom programs that rely on the features of an existing program like Microsoft Excel.
Now instead of writing a spreadsheet from scratch, you can use the features of the Excel spreadsheet and create a custom program based on Excel. This lets you reuse proven features of an existing program while letting you focus solely on writing the features you need.
Stand-alone