Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Windows & IPC -- request for comments

by rkg (Hermit)
on Aug 06, 2003 at 22:21 UTC ( [id://281624]=perlquestion: print w/replies, xml ) Need Help??

rkg has asked for the wisdom of the Perl Monks concerning the following question:

Hi --

I've never used Perl for anything pipe-like before. The following is my attempt, running on Activestate 5.8.0 on WinXP. The code below is a framework, not my full-blown module.

I found the perl docs hard to understand for IPC::Open3. The following rough hackish code works but I'd like any comments on how I could be doing it better.

Should I be checking that the pipe opened? If so how? Should I have error checking elsewhere? Should I be closing the pipe gracefully, rather than killing it? And am I killing it correctly?

Thanks for helping me learn--

rkg
package Word::Spell; use strict; use warnings; use lib qw(c:\ blah blah blah blah); use base qw(Exporter); our @EXPORT_OK = qw(spell); use IPC::Open3; my ($pid, $wph, $rph, $eph); BEGIN { my $cmd = 'c:/apps/aspell/bin/aspell'; my $args = '-a'; #-pipe --ignore-case --sug-mode=normal'; $pid = open3($wph, $rph, $eph,$cmd, $args); # eat and confirm the id line my $idline = <$rph>; die "bad idline: $idline" unless $idline =~ /Aspell 0.50.3/; } sub spell { my ($word) = @_; die "word '$word' has spaces" if $word =~ /\s/; die "word '$word' has numbers" if $word =~ /\d/; die "word '$word' has odd chars" if $word =~ /[^A-Z-]/i; print $wph "^$word\n"; my $result = <$rph>; return $result; } END { kill 9, $pid; } 1;

Replies are listed 'Best First'.
Re: Windows & IPC -- request for comments
by BrowserUk (Patriarch) on Aug 06, 2003 at 23:31 UTC

    perlipc for AS 5.8 says:

    CAVEATS AND LIMITATIONS

    BEGIN blocks

    The fork() emulation will not work entirely correctly when called from within a BEGIN block. The forked copy will run the contents of the BEGIN block, but will not continue parsing the source stream after the BEGIN block.

    Which may or may not be relevant to IPC::open3 (which must do an emulated fork under the covers), but as I just re-read this a few minutes ago, I thought it worth mentioning. You might be better off using an INIT block rather than a BEGIN block.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: Windows & IPC -- request for comments
by rkg (Hermit) on Aug 06, 2003 at 22:32 UTC
    PS Yes, I am aware of Bill Moseley's Text-Aspell-0.03 on CPAN. However, I am
    • looking for a pure perl layer over the aspell binary, and
    • looking to learn about pipes.
    Hence reinventing the wheel.

    PPS In the Text-Aspell docs, it says that implementation "should provide good performance compared to forking the aspell program for every word".

    In the implementation I posted above, I keep open one aspell process, yes, so I would not suffer the poor performance Bill mentions? I don't really care about speed here, I am just curious. rkg
Re: Windows & IPC -- request for comments
by rkg (Hermit) on Aug 06, 2003 at 22:56 UTC
    Me again. One thing I don't like about this code is that if the program fails or errs out and doesn't end gracefully, the END block isn't called and I get a zombie hanging around. How to fix?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://281624]
Approved by Thelonius
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 13:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found