0

i have a perl script(myscript.pl)

use strict;
use IO::Handle;

open OUTPUT, '>', "output.txt" or die $!;

STDOUT->fdopen( \*OUTPUT, 'w' ) or die $!;

while (<STDIN>){
print $_;
}

i can paste into output.txt what i write on commandline

also

I have a bash script:

cat >in.$$ 

cat in.$$> /tmp/msg 

i can paste what i have in in.$$ into /tmp/msg

i need to run perl script with stdin that come from bash script.

is it possible and how?

i tried , even though i know it is too silly

cat in.$$> ./myscript.pl
in.$$> ./myscript.pl
in.$$> perl myscript.pl
1
  • 1
    I probably have misunderstood it, but, how about ./myscript.pl in.$$? Commented Dec 5, 2013 at 16:41

2 Answers 2

5

To specify the contents of a file as standard input to a bash command, use <

./myscript.pl < in.$$

To use the output of one command as the input to another command, use '|' (the 'pipe' character, as if you were sending bits from one process to another through a pipe)

cat in.$$ | ./myscript.pl
echo foo | ./myscript.pl
Sign up to request clarification or add additional context in comments.

Comments

5

In your Perl script, use the diamond operator (aka the null file handle):

The null filehandle <> is special: it can be used to emulate the behavior of sed and awk, and any other Unix filter program that takes a list of filenames, doing the same to each line of input from all of them. Input from <> comes either from standard input, or from each file listed on the command line. Here's how it works: the first time <> is evaluated, the @ARGV array is checked, and if it is empty, $ARGV[0] is set to "-", which when opened gives you standard input. The @ARGV array is then processed as a list of filenames.

Here's an example:

#! /usr/bin/perl
# cat command emulator

while ( my $line = <> ) {
    chomp $line;
    say $line;
}

The <> will take each file listed on the command line, open them sequentially, and then read each line, or pull lines in from STDIN.

You can also do this:

while ( my $line = <STDIN> ) {
    chomp $line;
    say $line;
}

Which will read in lines from STDIN, but won't read files from the command line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.