CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
`STRING` - no output for non exiting command

 



aladin
New User

Jan 20, 2010, 1:16 AM

Post #1 of 14 (2927 views)
`STRING` - no output for non exiting command Can't Post

Hello,
I don't know how for example execute a command which puts some output (its what i need), but it doesn't exit, because it has its own console, to exit I need to write 'exit'.

So when I use backticks I don't see any output, is there a way to execute command and then put exit in its console?

Thanks.


7stud
Enthusiast

Jan 20, 2010, 2:30 AM

Post #2 of 14 (2923 views)
Re: [aladin] `STRING` - no output for non exiting command [In reply to] Can't Post

When you use backquotes, I believe perl waits for the process to end before proceeding. Try using pipes instead, for example:


Code
open my $PIPE_READER, "some_command |"; 

while (<$PIPE_READER>) {
print;
}



(This post was edited by 7stud on Jan 20, 2010, 2:45 AM)


aladin
New User

Jan 20, 2010, 2:35 AM

Post #3 of 14 (2921 views)
Re: [7stud] `STRING` - no output for non exiting command [In reply to] Can't Post

The problem is that it is locked on that line, so I can't do anything.
Unless you know how to start it and kill it in one command line.


7stud
Enthusiast

Jan 20, 2010, 2:40 AM

Post #4 of 14 (2919 views)
Re: [aladin] `STRING` - no output for non exiting command [In reply to] Can't Post

See my revised post above.


aladin
New User

Jan 20, 2010, 3:13 AM

Post #5 of 14 (2911 views)
Re: [7stud] `STRING` - no output for non exiting command [In reply to] Can't Post

Ok, it prints, but i forgot to tell what i really need.
I need to check in output for specific string, so i need to get that output in to var. Is it possible?

Also is it possible now to write 'exit' command to that pipe?

Thanks.


7stud
Enthusiast

Jan 20, 2010, 3:25 AM

Post #6 of 14 (2909 views)
Re: [aladin] `STRING` - no output for non exiting command [In reply to] Can't Post


Quote
I need to check in output for specific string, so i need to get that output in to var. Is it possible?


What do you think that while loop does? If you don't know, then you need to buy a beginning perl book and start reading it.


Quote
Also is it possible now to write 'exit' command to that pipe?

kill()


(This post was edited by 7stud on Jan 20, 2010, 3:26 AM)


aladin
New User

Jan 20, 2010, 5:04 AM

Post #7 of 14 (2903 views)
Re: [7stud] `STRING` - no output for non exiting command [In reply to] Can't Post

Thank you for replay, I am new to perl, but I think I will figure it out.


shawnhcorey
Enthusiast


Jan 20, 2010, 7:06 AM

Post #8 of 14 (2899 views)
Re: [aladin] `STRING` - no output for non exiting command [In reply to] Can't Post


In Reply To
Hello,
I don't know how for example execute a command which puts some output (its what i need), but it doesn't exit, because it has its own console, to exit I need to write 'exit'.

So when I use backticks I don't see any output, is there a way to execute command and then put exit in its console?

Thanks.


See `perldoc IPC::Open2`.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


7stud
Enthusiast

Jan 20, 2010, 3:56 PM

Post #9 of 14 (2887 views)
Re: [shawnhcorey] `STRING` - no output for non exiting command [In reply to] Can't Post

shawnhcorey,

1) Now that I think about it, to kill() a process you need the process id, which open() does not return. However, reading the IPC docs, they seem to suggest that there is a way to kill a process opened with a pipe:


Quote
You might notice that you could use backticks for much the same effect as opening a pipe for reading:

<snip code>

While this is true on the surface, it's much more efficient to process the file one line or record at a time because then you don't have to read the whole thing into memory at once. It also gives you finer control of the whole process, letting you to kill off the child process early if you'd like.


How would that work?

2) Is IPC::Open2 essentially a fork() and an exec() in the child?


(This post was edited by 7stud on Jan 20, 2010, 4:01 PM)


7stud
Enthusiast

Jan 20, 2010, 3:59 PM

Post #10 of 14 (2885 views)
Re: [aladin] `STRING` - no output for non exiting command [In reply to] Can't Post


In Reply To
Thank you for replay, I am new to perl, but I think I will figure it out.


Hint: look at the while loop and the print statement. There is nothing specified in the print statement, so how does anything get printed? Also, you can certainly write a while loop that explicitly does what perl does implicitly--it might even be considered better style.


(This post was edited by 7stud on Jan 20, 2010, 4:00 PM)


7stud
Enthusiast

Jan 21, 2010, 3:07 AM

Post #11 of 14 (2857 views)
Re: [7stud] `STRING` - no output for non exiting command [In reply to] Can't Post


Quote
1) Now that I think about it, to kill() a process you need the process id, which open() does not return.

Wrong. When you open() a pipe, open() returns the process id. Since you don't need a bi-directional pipe, IPC::Open2() sounds like overkill.


shawnhcorey
Enthusiast


Jan 21, 2010, 4:09 AM

Post #12 of 14 (2855 views)
Re: [7stud] `STRING` - no output for non exiting command [In reply to] Can't Post


In Reply To

Quote
1) Now that I think about it, to kill() a process you need the process id, which open() does not return.

Wrong. When you open() a pipe, open() returns the process id. Since you don't need a bi-directional pipe, IPC::Open2() sounds like overkill.


The OP stated that the process is killed by sending 'exit' to it. IPC::Open2 allows this.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


7stud
Enthusiast

Jan 21, 2010, 5:30 AM

Post #13 of 14 (2853 views)
Re: [shawnhcorey] `STRING` - no output for non exiting command [In reply to] Can't Post

Yeah, the op also said 'exit command', so it's not clear what they need.


shawnhcorey
Enthusiast


Jan 21, 2010, 5:46 AM

Post #14 of 14 (2850 views)
Re: [7stud] `STRING` - no output for non exiting command [In reply to] Can't Post


In Reply To
Yeah, the op also said 'exit command', so it's not clear what they need.


No, every time he said, 'exit' he wrote: write 'exit'

This implies that he wants to both read from and write to the process, which is what IPC::Open2 is designed for.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives