
| Linuxtopia Contents |
Perl Tutorial - An Introduction to Perl - Running Perl |
| [an error occurred while processing this directive] |
Contents6. Storing and Running Perl ProgramsHomework: HelloTo test your ability to store and run a Perl program, enter and execute something like this classic code: #!/usr/local/bin/perl -w
if ($#ARGV >= 0) { $who = join(' ', @ARGV); }
else { $who = 'World'; }
print "Hello, $who!\n";
Here's How:Let us assume that the above lines are stored in a Unix file
perl ~/bin/hello
perl ~/bin/hello Citizens of Earth
perl hello (If you're in the ~/bin directory.)
If you expect to use this program a lot and want to execute it as a command, then you need to do five things. 1. The first line of the program should after a "#!" specify the location of
the perl command, typically as 2. Set the execute permissions of the program file. To make the file executable (and readable and writable) by only yourself, use a Unix command like: chmod 700 ~/bin/helloTo make it executable and readable by all enter a Unix command like the following: chmod a+rx ~/bin/helloYou may also need to use chmod a+x on the directories ~ and ~/bin. See "man chmod" for details and the security implications. (The MU program "chweb" can make a file accessible to the Web server and yourself, but not to other users. See "man chweb" for details.) 3. Edit your file set path = ($path ~/bin) 4. This takes effect the next time you start a default tcsh or csh shell (.cshrc file) or login (.login file). If you want it to take effect immediately, enter the above set path command at the Unix prompt or enter execute your .cshrc or .login file with the "source" command. If you are using sh, bash, ksh or some other shell, alter ~/.profile or some other file to set the path at login. If a program you want to execute has just been newly created, then issue the csh/tcsh command "rehash" to rescan the path. If you perform (1)-(5), then you can execute your program via a command like this: hello |