3

I want to read from STDIN and have everything in a variable, how can I do this?

I'm know know almost nothing about Perl, and needed to create CGI script to read data from POST request, and was not able to find anything how to do that.

2
  • 3
    Presuming that you know how to assign to variables and read STDIN, I imagine you're having some other unspecified problem. Are you trying to read to EOF (list context)? Are you reading from a pipe that does not close? Are you reading from a Terminal? Commented Dec 22, 2015 at 18:13
  • Related: stackoverflow.com/questions/953707/… Commented Oct 30, 2018 at 16:34

2 Answers 2

9

This is probably not the most definitive way:

my $stdin = join("", <STDIN>);

Or you can enable slurp mode and get the whole file in one go:

local $/;
my $stdin = <STDIN>;

[but see man perlvar for caveats about making global changes to special variables]

If instead of a scalar you want an array with one element per line:

my @stdin = <STDIN>;
Sign up to request clarification or add additional context in comments.

Comments

3
my $var = do { local $/; <> };

This doesn't quite read Stdin, it also allows files to specify on the command line for processing (like sed or grep).

This does include line feeds.

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.