Perl’s roots run deep in the Unix ecosystem, but macOS brings those roots to your desktop. The operating system ships with a fully‑functional Perl interpreter in /usr/bin/perl, so you don’t need to download a separate installer for most users. If you prefer the classic Mac interface, the free
#!/usr/bin/perl
<h1>Our first application</h1>
print "What is your name? ";</p>
<p>my $name = <STDIN>;</p>
<p>chomp($name);</p>
<p>print "Hi $name, nice to meet you!";
Notice the very first line: #!/usr/bin/perl. This is called a shebang. It tells the shell that the script should be interpreted by the Perl interpreter located at that path. Because macOS ships Perl to /usr/bin/perl, the shebang works out of the box. The next line is a comment (anything after a # is ignored by the interpreter); comments are a good place to explain what a script does or why a particular line is present.
After pasting, save and exit. In nano press Ctrl + O to write the file, hit Enter to confirm the name, and then Ctrl + X to quit the editor.
Now the file is ready to run, but we still need to give it execute permission. macOS treats files as data by default; they need a special flag to be treated as code. In the terminal run:
Prompt
chmod 755 test.pl
The chmod command changes the file’s mode bits. A value of grants read, write, and execute rights to the owner, and read and execute rights to group and others. If you need more detail on Unix permissions, the
The script will prompt “What is your name?”; type a name and press Enter. The program will greet you with “Hi …, nice to meet you!”. That’s all it takes to create a working Perl script on macOS.
For users on older Classic Mac OS, the workflow is similar but the interpreter lives in a different location. Open the MacPerl application, paste the same code into the editor, then press Command + Shift + R to run. The result is identical, and the same shebang line points to the interpreter bundled with MacPerl.
Now that you’ve run a script, it’s time to explore the building blocks of Perl code. The next section dives into scalars, operators, input, output, and how to keep your code readable and maintainable.
Core Concepts: Scalars, Operators, Input, Output, and Commenting
Perl’s syntax is flexible, but its core primitives stay consistent across versions. Understanding scalars, operators, and the standard input/output model is essential for writing clean, effective scripts.
### Scalars: The Single‑Value Data Type
Scalars are variables that hold a single value: a number, a string, or a reference to any other data structure. In Perl a scalar variable begins with a dollar sign $. For example, $name or $age store data that can be retrieved or modified later in the script.
When naming a scalar, you can use letters, digits, and underscores, but the first character must be a letter. The names are case‑sensitive, so $Age and $age are distinct variables. Here’s a quick illustration:
Scalar operators perform arithmetic or concatenation. The plus + adds numbers or concatenates strings depending on context. For arithmetic, both operands must be numeric. When used with strings, Perl automatically performs string concatenation. The minus , times *, and divide / operators work similarly.
Perl also supports increment ++ and decrement operators. They are handy for loops or simple counters:
Web counters often use this technique. A script reads a number from a file, adds one, prints the result, and writes the new number back to the file for the next visit.
### Input: Reading from STDIN
Perl programs commonly receive input from the standard input stream (STDIN). The expression <STDIN> reads a single line, including the trailing newline character. To remove that newline, the chomp function is called. Chomp is safe to use even if the string doesn’t end with a newline; it simply does nothing in that case.
Prompt
$user_input = <STDIN>;</p>
<p>chomp($user_input);
When you ask a user a question with print, you typically follow up with the STDIN read. The pattern looks like this:
Prompt
print "Enter your age: ";</p>
<p>my $age = <STDIN>;</p>
<p>chomp($age);
Notice the my keyword, which creates a lexical variable that’s local to the block. Using my is considered good practice because it limits variable scope and reduces accidental clashes.
### Output: Using the Print Function
The print function writes data to the standard output stream, which in the terminal appears on the screen. Anything passed to print is converted to a string automatically. If you pass a scalar, its value is printed. If you pass a string literal, it must be surrounded by quotes. Double quotes allow interpolation of variables; single quotes treat the text literally.
Prompt
print "Your name is $name
";</p>
<p>print 'This is a literal string with $variables not interpolated';
Perl automatically appends a newline character to each print call if you don’t supply one, but it’s common to include it explicitly for clarity.
### Comments: Writing Readable Code
Anything following a # is ignored by the interpreter. Comments are your tool for documenting intent, noting why a line is present, or marking TODOs. Although there’s no block comment syntax like //, you can place a comment at the end of each line or on its own line. Keep comments concise but meaningful.
Here’s a fully commented example that ties together the concepts discussed so far:
Prompt
#!/usr/bin/perl
<h1>Ask the user for their age, then estimate their age in days.</h1>
<p>print "How old are you? ";
# Prompt</p>
<p>my $age = <STDIN>;
# Read input</p>
<p>chomp($age);
# Remove trailing newline</p>
<p>my $age_days = $age * 365;
# Simple calculation (ignoring leap years)</p>
<p>print "You are approximately $age_days days old.
";
This script uses a shebang, a comment header, variable declarations, input handling, arithmetic, and output - all in a single, readable block. By following this structure, you’ll write code that’s easy for others (and future you) to understand.
### File Permissions and Running Scripts in X (classic Mac)
On macOS, the chmod command is used to make scripts executable. The example above used chmod 755 test.pl, which is sufficient for most scripts. If you’re on a classic Mac, the same principle applies; just remember that the path to the interpreter in the shebang may differ. The file permission system remains the same because macOS inherits Unix file permission semantics.
To test your script without typing the full path, you can place it in a directory that’s on your $PATH environment variable. Adding ~/bin to your $PATH is a common convention. After moving the script there and ensuring it’s executable, you can run it from anywhere with simply test.pl
By mastering scalars, operators, STDIN, print, and commenting, you’ll have the foundation to tackle more advanced Perl topics - arrays, hashes, regular expressions, and modules. Those subjects build directly on these basics, so feel free to experiment by extending the sample script: ask for a second input, perform a calculation, or output a formatted table.
Happy coding! The Perl community offers countless tutorials, CPAN modules, and documentation; dive in, ask questions, and keep building.
No comments yet. Be the first to comment!