Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

help in opening file with pipe

by uva (Sexton)
on Feb 09, 2006 at 10:37 UTC ( [id://529049]=perlquestion: print w/replies, xml ) Need Help??

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

hello monks, what the difference between opening the file with pipe sysmbol and without pipe.where we actually use i mean which one is more efficient in which place and where it is inefficeient. eg:
open(handle,"d:/help.txt |"); and open(handle,"d:/help.txt");

Replies are listed 'Best First'.
Re: help in opening file with pipe
by zentara (Cardinal) on Feb 09, 2006 at 11:17 UTC
    When you open the file without the pipe, you are opening it for editing or reading it. When you open it with a pipe, you are doing IPC (see perldoc perlipc ). It means that you are "executing the script". You can put the pipe at the front or the end of the open string. If the pipe is at the front, it means that you can write to the filehandle, and it will be sent to the programs STDIN. If the pipe is at the end of the open string, it means you can read the filehandle, which will contain the STDOUT output of the executed program. It you want to setup a program to execute, where you can manipulate BOTH the STDIN and STDERR, use IPC::Open2 (or IPC::Open3 to include STDERR).

    I'm not really a human, but I play one on earth. flash japh
Re: help in opening file with pipe
by svenXY (Deacon) on Feb 09, 2006 at 10:42 UTC
    Hi,
    you would only use the pipe if there was some command that produces some output that you want to catch (e.g. open(CMD, 'ls -l |')). If you only want to open a file for reading, you shouldn't use the pipe operator. See open for details.
    Regards,
    svenXY
      it means we get the output from the command and we use that output as a filename.right

        (Incidentally: s/$/?/)

        No, it's not right. It means that a process is forked for that command and that the output of that command, i.e. what that it will inject into STDOUT is returned to you when you read from the opened filehandle, as if it were the content of an actual file.

Re: help in opening file with pipe
by zentara (Cardinal) on Feb 09, 2006 at 11:29 UTC
    #!/usr/bin/perl my $pid = $$; open (T,"top -d 1 -p $pid b |" ) or die $!; $|=1; while(<T>){ $output = $_; $output =~ tr/\n//; #print $output; my @words=split(/\s+/,$output); print join "\n",@words,"**************\n"; # if($words[0] =~ /$pid/){print "$words[4]\n"} }

    I'm not really a human, but I play one on earth. flash japh
Re: help in opening file with pipe
by zentara (Cardinal) on Feb 09, 2006 at 11:57 UTC
    For windows users check out ActiveStates perlipc

    Here is a super-simple piped input, using a perl script.

    #the Main script to run #!/usr/bin/perl -w $|++; open( H, " | other_script") or die "$!\n"; print H "hi\n"; sleep 1; #let other script print before dieing __END__ ##################################### #the other_script #!/usr/bin/perl $|++; my $in = <>; print "$in\n" __END__

    I'm not really a human, but I play one on earth. flash japh
Re: help in opening file with pipe
by mickeyn (Priest) on Feb 09, 2006 at 15:05 UTC
    You're missing 'cat' before the file name (or 'type' if you're running on Windows).

    open(handle,"type d:/help.txt |");
    The pipe as you wrote it doesn't get anything in its STDIN.

    Enjoy,
    Mickey

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2025-01-13 14:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (32 votes). Check out past polls.