Page 1 of 1

Embedding a Perl filter

Posted: 2008-08-28 00:56:07
by js
To sort paragraphs and kill duplicate lines I normally use a Perl macro like this

Code: Select all

use strict;
my %seen;
while (<>) {
    $seen{$_}++;
}
print sort keys %seen;
I tried to integrate this into a Nisus macro like this:

Code: Select all

Begin Perl
my perl macro
End
But this does not work. What should I have done?
Is there maybe a Nisus macro which does the same thing?

Re: Embedding a Perl filter

Posted: 2008-08-28 03:51:51
by Kino
If you don't need exchanging data between a Perl block and other parts of the same Nisus macro and if you just want to replace a selection with the output of your perl script, then I think you'd better write it as a Perl macro (with pl as file extension), not as a Nisus macro.

Code: Select all

#Nisus Macro Block
#source front selection
#destination clipboard
#after execution
#Paste
#End Nisus Macro Block

my %seen;
while (<STDIN>) {
	$seen{$_}++;
}
print sort keys %seen;
js wrote:Is there maybe a Nisus macro which does the same thing?
The simplest Nisus Writer Pro way would be...

Code: Select all

Menu ':Edit:Sort Paragraphs:Ascending (A-Z)'
Replace All '(^.*\n)\1+', '\1', 'E-is'

Re: Embedding a Perl filter

Posted: 2008-08-28 12:29:26
by js
Thank you, Both work fine now.

I found BTW that in the case of the Perl macro, even without using it with additional Nisus code, I had to add a Start Perl / End line to make it work.

Re: Embedding a Perl filter

Posted: 2008-08-29 04:24:45
by Kino
js wrote:I found BTW that in the case of the Perl macro, even without using it with additional Nisus code, I had to add a Start Perl / End line to make it work.
That means that you saved it as a Nisus Macro (filename.nwm). If it were a Nisus Perl Macro (filename.pl), "Begin Perl ... End" should have yielded an error.