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

Hi All, I tried to search through the site for a similar problem but couldn't get any hits so I am presuming it has not been answered before. If similar thing is already dicussed could someone please point me to it?

I am hoping someone could shed light on the problem that I have.

I am trying to a simple program that reads in some file and converts it to HTML and displays it the Links browsers (linux machine).

Once I am done creating the HTML file, I call "links" with the filename as argument using the system function. This works very well if I don't pipe in values. But when I use "head" to send values to the program, browser does not display anything, it just shows me a blank screen and none of the browser commands works. I had to ctrl+Z, kill %1 (even Ctrl+C does not work, that is my interrupt).

It sort of makes me feel that the pipe is not terminating properly and the forking another process from there is causing the problem (I could be horribly wrong :)). So I tried to close(STDIN) (bad idea?) and that makes the file to come up on the browser but the browser commands don't work anymore! Ctrl+C works now.

Here is my program - this does not do anything useful but the idea is there -

perl Version: 5.8.0 #!/usr/local/bin/perl -w # program ht use strict; my $file = "temp.html"; my $ret = 0; open(HTML,">$file"); print HTML ("<html><body>\n"); print HTML ("<B> $_ </B>") while(<>); print HTML ("\n</body></html>\n"); close(HTML); # close(STDIN); # links breaks if I close it explicitly $ret = system("links",$file); die "System command failed\n" if ($ret & 0xffff); Usage: ht myfile (works fine, displays HTML file in browser and terminates well) ht (RETURN) Enter values on STDIN ^D (works fine) head myfile | ht (without closing stdin) (no display on the browser)

Thanks a lot!

cheers

SK

Replies are listed 'Best First'.
Re: system function problem
by lidden (Curate) on Sep 01, 2004 at 21:51 UTC
    I don't have "links" on my system so I can't try it, but you should check if your opens succede. This is how I would have written your program.
    #!/usr/bin/perl -w use strict; my $file = 'temp.html'; open HTML, ">$file" or die "Damn: $file $!"; print HTML "<html><body>\n"; print HTML "<B> $_ </B>" while <>; print HTML "\n</body></html>\n"; close HTML or die $!; # close(STDIN); # links breaks if I close it explicitly die "System command failed $_" if system('links', $file) == -1;

      Thanks Lidden!

      I did add the checks for open and close as per your suggestion. It does not change the end result. It is dumb on my part not to check for error on open (I shouldn't try to type code on the message box directly :)) I like your style on the system call! I have never used a die on close and that is also something that i should do going forward. Thanks again.

      Please let me know if you ever get a chance to test it on a machine with links.

      cheers

      SK

        Hi All,

        Something told me that I should check out links with pipes and I tried this.

        head myfile | links

        and i get the same problem that i had mentioned with my script. for some reason links does not like a pipe being open when it is called. In my script, The pipe should be done (i.e. no more data to feed) before it calls links but still it does not like that. I feel if we could close the pipe explicitly then it might help. is there a way to close that pipe explicitly? I am not sure how to do this because the user could send in a file name too, if they don't then it defaults to STDIN/Pipes i.e. i am using the <> handle.

        Thanks for your help!

        cheers

        SK