Some hints to help you get going.
A Perl script is just a text file. Use any text (programmer's) editor.
By convention, Perl script files end with the extension .pl.
The Emacs text editor has a Perl mode that will auto-format your Perl scripts and highlight keywords. Perl mode will be activated automatically if you end the script name with .pl. Otherwise, you can force Emacs to enter Perl mode by placing this line somewhere near the top of the file:
# -*- mode: perl -*-
The next time you open the file, Emacs will enter Perl mode.
(~) 50% perl time.pl
The time is now Thu Sep 16 18:09:28 1999
#!/usr/bin/perl # file: time.pl $time = localtime; print "The time is now $time\n"; |
Make the script executable with chmod +x time.pl:
(~) 51% chmod +x time.pl
Run the script as if it were a command:
(~) 52% time.pl
The time is now Thu Sep 16 18:12:13 1999
Every script goes through a few iterations before you get it right. Here are some common errors:
Code:
#!/usr/bin/perl # file: time.pl time = localtime; print "The time is now $time\n"; |
Output:
(~) 53% time.pl Can't modify time in scalar assignment at time.pl line 3, near "localtime;" Execution of time.pl aborted due to compilation errors.
Code:
#!/usr/bin/perl # file: math.pl $six_of_one = 6; $half_dozen = 12/2; $result = $six_of_one/($half_dozen - $six_of_one); print "The result is $result\n"; |
Output:
(~) 54% math.pl Illegal division by zero at math.pl line 6.
(~) 55% test.pl test.pl: Permission denied.
Code:
#!/usr/local/bin/pearl # file: time.pl $time = localtime; print "The time is now $time\n"; |
(~) 55% time.pl time.pl: Command not found.
You can call Perl with a few command-line options to help catch errors:
Usually you will invoke these from the command-line, as in perl -cw time.pl (syntax check time.pl with verbose warnings). You can also put them in the top line: #!/usr/bin/perl -w.
|
| Contents |
Next |