PerlPad [ Homepage | Project Summary page | Download | Source code ]
[ Back | Forward | Index ]

Basic Perl expressions, variables and subroutines

Hello World

As you know by now, PerlPad allows you to run Perl statements from any Mac OS X application that supports Services. Basically the selected text gets evaluated (see the Manual for details how exactly) and replaced by the result of that evaluation.

not so impressive.

slightly better, you might consider retiring your calculator now.

Get your math right

Suppose you want to add up some numbers that you found on a website, but you dont want to retype them into a summation formula. Let me show you how to handle this with PerlPad: Select the following numbers and copy them to the pasteboard (Cmd-C).

9 10 11 12

And now select and Evaluate with PerlPad this code:

If all is well, it just be replaced with the sum of the numbers in the pasteboard.

What goes in ...

So how exactly did that work? Let's analyze the summation snippet.

my $sum = 0; $sum += $_ foreach split; $sum;

If we take away some of the Perl magic and be more verbose it becomes

my $sum = 0; 
foreach my $number (split ' ', $_) {
    $sum += $number;

Yes, that is the same program. Trust me.

As you can see, your input text (the numbers in the pasteboard) has been made available as $_.

Sometimes you don't want to have the whole input fed to your script as a big chunk in $_. You can also read it line by line using the special filehandle $IN. So you could count the number of lines in the pasteboard like this:

or like this:

... must come out

What gets returned to the application requesting the service? Usually, this is the value of the last statement in the Perl snippet you provided. This is why we had the pretty lame $sum; as our final statement in the summation code.

When the return value is undefined, PerlPad does not replace the selected text.

Another way to produce the output is to print something. If you do that, the return value from the snippet is disregarded and you get the print output instead.


One thing that is really setting the PerlPad services apart from similar services is that they offer you to share data between individual invocations of services. To do this, you have to declare global Perl variables.


In what might come to a shock to a lot of Perl hackers out there, PerlPad requires you to predeclare your variables.

No !

Yes !

Basically, there are two types of variables, local variables that can be used only within a single evaluation context (service invocation) and global variables that persist, making them available for later use.

You declare a local variable using the keyword my.

You declare a global variable using the keyword our.

You can then use that variable later on.


You can also declare subroutines.

To use them later, just call them by name (and they will answer):

If you have some subroutined you want to use often, you can also make them more easily accessible via clickable buttons and the second PerlPad Service (Process selection with PerlPad). I will teach you about that on the next page.

PerlPad [ Homepage | Project Summary page | Download | Source code ]
[ Back | Forward | Index ]
Built With WebMake Logo