Specifying both Perl code and input, looping over input lines, redirecting output
Combining Evaluate and Process modes
The usual way to process some text with an ad hoc Perl snippet is to copy the text into the pasteboard, type the snippet into something like TextEdit and Evaluate it. This way, you can undo and tweak the snippet, if the result is not quite what you wanted. Alternatively, if you have the text in an editable space, you can Process it, typing your snippet into PerlPad's dialog. This way, undo will give you back the input text. A third way is combining both Perl snippet and input text into the selection you Evaluate. You can do this by appending a
__DATA__ section to your Perl code:
The contents of the
__DATA__ section will be used as input instead of the pasteboard. Be sure to start the line with
__DATA__ (nothing before it!).
Looping over input lines
The Perl command line application has a number of nice switches to help creating the famous Perl One-Liners. PerlPad emulates some of them:
As you can see, you can specify these switches (in this case
-n to loop over lines without printing them) by starting the snippet with
#!perlpad. The option parsing is not very sophisticated, you have to specify all switches grouped together after a single hyphen.
In case you were wondering,
$. is a magic Perl variable that holds the current input line number.
-n loops over input lines, providing them as
-p same as above, but also prints
-l end of line processing, automatically adds newlines when printing, removes newlines from input lines when combined as
-pl for looping
-a automatically split
$_ into array
#!perlpad syntax is also used to redirect PerlPad's output to a variable, the pasteboard or a file. As with normal shell redirects, the standard output target receives nothing if you redirect. This means that when Evaluating the Perl snippet that you selected does not get replaced.