Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: I may be remotely depressed.

by brassmon_k (Sexton)
on Jan 02, 2002 at 23:30 UTC ( #135770=note: print w/replies, xml ) Need Help??

in reply to FTP question

Perl can execute UNIX commands doing something that took me forever to figure out and is just really weird. I couldn't pray to tell you where to find it anymore but here is the answer. I could let you look for it forever HAHAHA! but I'm nice.
#!/usr/bin/perl -w open I, "| ps -ef" or die; close I;
Now remember where it says "open I"....."I" can be anything. It could be "Open PROCESS" it doesn't matter. The "close I" has to be there though or "close PROCESS". You can make that handle anything you want it to be. Then after that you can use any UNIX system command. ls, cd, ls -al, ftp, it will open anything so far as I've used it.

The Brassmon_k

Replies are listed 'Best First'.
Re(2): I may be remotely depressed.
by dmmiller2k (Chaplain) on Jan 03, 2002 at 00:18 UTC

    brassmon_k, you are referring to opening a pipe to read process output. Unfortunately the syntax you suggest is the one for *supplying input* to the subprocess, which is not a common usage of pipes (your pipe character is the *first* character of the command).

    Far more common is to run a command producing output which the perl code must process, using the pipe character ('|') as the *last* character of the command (e.g., open PH, "shell command|".).

    Sample code follows:

    open PH, "ps -ef|" or die "$!: opening pipe"; while (<PH>) { # retrieves output from "ps" command one line at a ti +me # .. process output--each line is $_ .. } close PH or die "$!: error closing pipe";


    You can give a man a fish and feed him for a day ...
    Or, you can
    teach him to fish and feed him for a lifetime
      What do you mean?

      I tested the little Perl snippet on my SPARC and it produces the output on STDOUT for the "ps -ef" command. I don't get what you mean for "standard input". I know the handle is used for INPUT processing via a file Handle but what I suggested still works. It's probably not syntaticallly correct but it works perfect. Perl doesn't seem to care if the pipe (|) is in front. It does send the "ps -ef" command to STDOUT though I guarantee it...try it. I also found it convienient to give it a handle because then this guy can manipulate the data any way he wants to later on. I might be misunderstanding you though.

      The Brassmon_k
        I just tested yours...
        #!/usr/bin/perl -w open PH, "ps -ef|" or die; close PH;
        The result was not to the screen or STDOUT. The command was run like if you used the "system" function. It executed the command but didn't return it's output. I'm not nearly even close as being as good at Perl as you but maybe you got the pipe placement screwed up. (I could be dumb and I'm not understanding what this guy really wants) but my version of it does return the "ps -ef" commands output to the screen on STDOUT. I know my way isn't common but it's a really nice hack and works really good for what I use it for.

        Well I'm just offering what I thought the guy wanted. Perl is a lot of fun now that I'm understanding it better.
        WOO HOO!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://135770]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2018-06-21 22:55 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (120 votes). Check out past polls.