interpolation problem in emacspipe.pl script from emacswiki
Dear List,
i found on the emacswiki the following script written in Perl which is
supposed to make emacsclient read from a pipe:
#! /usr/bin/perl
# This script uses emacsclient, be sure to have a running server session.
# A server-session can be started by "M-x server-start".
my $status = system("emacsclient -n --eval '(progn (pop-to-buffer (get-buffer-create \"*piped*\")))'");
if($status!=0){ exit 1; }
while(<STDIN>){
system("emacsclient -n --eval '(with-current-buffer \"*piped*\" (insert \"" . $_ . "\"))'");
}
Now when this script reads a list of emailaddresses for example from
mutt:
From: John Test <john [at] test.org>
From: "William Testtwo" <william testtwo [at] test.test.org>
it gives the following output:
#<buffer *piped*>
*ERROR*: Symbol's value as variable is void: William
everything works normaly if before feeding the output to emacspipe.pl,
i run it through "sed s/\"//g", whereby deleting the double quotes.
There must be something about the interpolation. Maybe somebody has an
idea? I am not an expert at this.
Gabriel
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: interpolation problem in emacspipe.pl script from emacswiki
>>>>> "GS" == Gabriel Striewe <linux [at] gabriel-striewe.de> writes:
GS> my $status = system("emacsclient -n --eval '(progn (pop-to-buffer (get-buffer-create \"*piped*\")))'");
GS> if($status!=0){ exit 1; }
GS> while(<STDIN>){
GS> system("emacsclient -n --eval '(with-current-buffer \"*piped*\" (insert \"" . $_ . "\"))'");
GS> }
a first pass is for you to learn the qq{} alternate quote
operator. escaping " like you do is noisy and tricky. learn about these
in perlop. qq{} is like "" but you can put " inside without
escaping. you can use any delimiter you like but paired {} is among the
best (you can even have nested {} inside without escaping!). so those
lines would look like (untested):
my $status = system( qq{emacsclient -n --eval
'(progn (pop-to-buffer (get-buffer-create "*piped*")))'
});
system( qq{ emacsclient -n --eval
'(with-current-buffer "*piped*" (insert "$_"))'
});
notice how much easier they are to read and you can see the "" are for
emacs lisp and not for perl.
actually i just noticed the first one doesn't to any perl interpolation
so that could use q{} which is like ''. the second one needs double
quotish behavior since it interpolates $_.
another improvement would be to use a named variable in the while which
makes the code easier to read. i stay away from $_ unless it is required
or it has a major win. neither case applies here, so this is
better. also you don't chomp the input line and that may also screw
things up:
while( my $email = <STDIN> ) {
chomp $email ;
system( qq{ emacsclient -n --eval
'(with-current-buffer "*piped*" (insert "$email"))'
});
}
better formatting helps too. that code is now ready for you and others
to understand easily and maintain.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/