Contents / Previous / Next


Hello World and print


Running Perl Programs

To run a Perl program from the Unix command line:
> perl script.pl

Alternatively, put the location of you Perl interpreter (this may vary from system to system use: which perl) in first line of your script. It tells the machine what to do when the file is executed (run the file through Perl):

#!/usr/bin/perl -w
The -w option enables the output of warnings.
To run the script it has to be executable (do "chmod 755 script.pl" under Unix).


Hello World

Here is the "hello world" Perl program:
#!/usr/bin/perl -w
#
# Program "Hello world"
#
print 'Hello world'; # Print a message
Comments can be inserted into a program with the # symbol (this is the only way),
Everything else is a Perl statement which must end with a semicolon. Whitespace is irrelevant.


The print Function

The print function takes a filehandle and a comma separated list of arguments of any type:
 print FILEHANDLE LIST
If FILEHANDLE is omitted, prints by default to standard output (or to the last selected output channel--see "select"). If LIST is also omitted, prints $_ to the currently selected output channel.

You can use parentheses for function arguments or omit them (they are required occasionally to clarify precedence).

print("Hello, world\n");   
print "Hello, world\n";    
print "Hello", "world\n";    
print "Hello
world";       # linebreak in the middle

Double quotes or single quotes may be used around literal strings: However, only double quotes "interpolate" variables and special characters such as new­ lines ("\n"):
$name = "fred";
print "Hello, $name\n";     # works fine
print 'Hello, $name\n';     # prints $name\n literally

Numbers do not need quotes:
print 42;

print works the same for fileIO, you have to specify a filehandle as first argument:
print { $file } "stuff\n";
print { $OK ? STDOUT : STDERR } "stuff\n";

print accepts scalars, strings, lists, arrays and hashes as arguments.
To print formated you can use the C-like printf function.