A Whirlwind Tour of Perl
So, you want to see a real Perl program with some meat? (If you don’t, just play along for now.) Here you are:
#!/usr/bin/perl
@lines = `perldoc -u -f atan2`;
foreach (@lines) {
s/\w<([^>]+)>/\U$1/g;
print;
}Now, the first time you see Perl code like this, it can seem strange. (In fact, every time you see Perl code like this, it can seem strange.) But let’s take it line by line, and see what this example does. (These explanations are brief; this is a whirlwind tour, after all. We’ll see all of this program’s features in more detail during the rest of this book. You’re not really supposed to understand the whole thing until later.)
The first line is the #! line, as you saw before. You might need to change that line for your system, as we discussed earlier.
The second line runs an external command, named within backquotes (”` `“). (The backquote key is often found next to the number 1 on full-sized American keyboards. Be sure not to confuse the backquote with the single quote, "'“.) The command we used is perldoc -u -f atan2; type that at your command line to see what its output looks like. The perldoc command is used on most systems to read and display the documentation for Perl and its associated extensions and utilities, so it should normally be available.[31] This command tells you something about the trigonometric function atan2; we’re using it here as an example of an external command whose output we wish to process.
The output of that command in the backticks ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access