An Example: The Ubiquitous Hello World

A long-standing tradition in the programming world is the Hello World program. When a programmer encounters a new language or a new system, the first program they are supposed to write simply prints “Hello, World!” to the screen. Far be it for me to go against tradition, so the first Perl script we'll examine in this book will do just that. In this section, you'll run a version of Hello World as a one-liner, and then in the next section you'll run Hello World as a standalone script.

Creating the One-Liner

Hello World as a one-liner is about the simplest Perl script you can do, and it's a quick way to find out if you've got Perl installed correctly on your system.

On Unix, from a command prompt, type the following (the bold parts are the part you actually type; the percent sign is the prompt). Watch out for the quotes—there are single quotes on the outside and double quotes on the inside):

%  perl
							-w
							-e 'print "Hello, World!
";'
						

On Windows, open a DOS or command-prompt window (look under the Start menu), and then type this command (as in Unix, watch out for the quotes, they're all double quotes, but there are four of them):

C:> perl
							-w
							-e "print "Hello, World!
";"
						

What to Do if It Doesn't Work

What happens if it doesn't print Hello, World! onto your screen? If you get errors such as Unmatched ', Syntax Error or Can't find string terminator, make sure you typed exactly the same thing as I have given you here. Quotes are most likely to be your problem—make sure you are not confusing single quotes (') and double quotes (“). If you are working on Windows, don't mix up your backslashes () with your forward slashes (/).

If you get a perl: Command Not Found error on Unix, a name not recognized as a command or a bad command or filename error on Windows, then one of three things has occurred: Perl has not been installed on your system, you do not have access to it, or it is not in your execution path. Go back to the Appendix B for Unix or Appendix C for Windows to make sure you have Perl installed correctly.

If nothing happens—if the computer just sits there—then it is likely that you accidentally included a space between the dash and the letter of one of the options (-w or -e), or used different letters from -w or -e. You can press Control+C to get out of the script and start again.

How It Works

The Hello World one-liner has four basic parts:

  • The perl command

  • The -w option

  • The -e option

  • The script, surrounded by quotes

With the perl command you're calling Perl to execute your script. The -w option turns on warnings. Perl warnings are helpful for debugging. You can turn them on here or in the body of your script. We'll get into more detail about warnings in the next example.

The -e option tells Perl that there is a one-liner script coming up. Any time you use a one-liner, you'll need the -e option.

The part just after the -e is the Perl code itself, inside single quotes ('), or double quotes (“) for Windows. In this case, the code inside single quotes is print "Hello, World. ";. The double quotes around the Hello World part are part of the Perl code—in Windows, the outer double quotes are regular quotes, but the inner double quotes have to be backslashed (")

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset