Contents
6. Storing and Running Perl Programs
Homework: Hello
To 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
~/bin/hello
. (That's in your home directory, subdirectory bin, file
hello.) You can then run the program by entering a command like:
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 #!/usr/local/bin/perl
or
#!/usr/bin/perl
, as illustrated in the preceding example program.
This line can also give command options, like -w (warn of possible
inconsistencies).
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/hello
To make it executable and readable by all
enter a Unix command like the following:
chmod a+rx ~/bin/hello
You 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 ~/.cshrc
or ~/.login
to make
directory ~/bin
part of the path Unix searches for executables,
with a line like this:
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