Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

help in opening file with pipe

by uva (Sexton)
on Feb 09, 2006 at 10:37 UTC ( #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");

Comment on help in opening file with pipe
Download Code
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 (Archbishop) 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 zentara (Archbishop) 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 (Archbishop) 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
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? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2014-12-26 00:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (163 votes), past polls