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

I have been trying to take an input from command line using STDIN.I am using the following code: print "Enter the message:"; $message=<STDIN>; Obviusly when I run the script I am prompted Enter the message: On this prompt I am copying and pasting an input coinsisting of characters. The problem is that only 257 characters of the message can be pasted into the prompt.Even if I copy and paste the whole message(consisting much more than 257 chars) only 257 characters and getting pasted in the prompt.All the characters afer 257 are getting truncated and are not getting pasted.Also , the prompt gets hanged and I am not able to press enter. Strtangely , for any input less than 257 characters , the script takes the input from STDIN. Could you please advise why only 257 characters of the imput is accepted at the prompt and all the other fopllwing it are rejected with the prompt getting hanged with the neter key not working. Many thanks in advance for your help. Gyatso

Replies are listed 'Best First'.
Re: Problem with STDIN input
by VinsWorldcom (Prior) on Jun 11, 2009 at 18:44 UTC
    Sounds like a shell limitation, not a Perl issue. I'm using Activestate 5.8.8 on Windows and I can paste way more than 257 characters to the prompt and then press enter.

    What version of Perl are using (perl -v)? What system (Windows/Linux)? What shell (bash, csh, command.com, cmd.exe, etc...)?

Re: Problem with STDIN input
by CountZero (Bishop) on Jun 11, 2009 at 18:44 UTC
    It must be an artefact of your OS or shell. On my Windows XP Pro box I run the following program:
    $test = <STDIN>; print length $test;
    and it accepts a maximum of 4095 characters (actually 4094 plus the EOL) at a time. I'm running ActiveState Perl 5.8.8.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Problem with STDIN input
by Your Mother (Archbishop) on Jun 11, 2009 at 18:48 UTC

    You will (should) be able to get more if you accept it as lines (where newlines come before the limit is reached) instead of a single string. Try this-

    print "Enter text with returns. Press Control+D to submit-\n"; my @input = <STDIN>; print join("", @input);
      Would try this out
Re: Problem with STDIN input
by ikegami (Patriarch) on Jun 11, 2009 at 19:28 UTC
    Perl has no problems with very long lines.

    Linux:

    $ perl -le'print "x" x ($ARGV[0]-1)' 100 | perl -le'print length <>' 100 $ perl -le'print "x" x ($ARGV[0]-1)' 1000 | perl -le'print length <>' 1000 $ perl -le'print "x" x ($ARGV[0]-1)' 10000 | perl -le'print length <>' 10000 $ perl -le'print "x" x ($ARGV[0]-1)' 1000000 | perl -le'print length < +>' 1000000

    WinXP:

    >perl -le"print 'x' x ($ARGV[0]-1)" 100 | perl -le"print length <>" 100 >perl -le"print 'x' x ($ARGV[0]-1)" 1000 | perl -le"print length <>" 1000 >perl -le"print 'x' x ($ARGV[0]-1)" 10000 | perl -le"print length <>" 10000 >perl -le"print 'x' x ($ARGV[0]-1)" 1000000 | perl -le"print length <> +" 1000000

    It must be an limit in your environment's pasting mechanism.

    Why don't you put your data in a file and pass the file to Perl?

      Hi ikegami, Thanks for the input.I too suspect that it should be shell's limitation.Not sure if the length is controlled by a environment/shell varaible of my ksh(*NIX box) which I strongly suspect.I am really after this Env/shell varaible whcih I belive if I can export prior to taking the input then I should be able to input more characters. FYI:As you suggested reading from file would be ideal but the requirement is to take a direct input from command line.The users would be invoking the script from a menu of options where they would be asked for the message through th e command propmt.They are not alloed to create files on unix box. By the way would you mind if I ask you what actually is the line doing: perl -le'print "x" x ($ARGV[0]-1)' 100 | perl -le'print length <>'

        I too suspect that it should be shell's limitation.

        "too"? I didn't mention the shell at all, but I suppose it could also be the shell's fault.

        what actually is the line doing

        The first perl creates a long string which is sent to the second perl's STDIN as if someone had typed it. It shows that Perl has no problem reading a line one million bytes long.

Re: Problem with STDIN input
by Marshall (Canon) on Jun 11, 2009 at 21:19 UTC
    If you are on Windows, this might be helpful:
    http://support.microsoft.com/kb/830473
    The max command length is 8191 chars on Win XP or 4191 on Win 2K.
    BUT in order to get that max limit, you must have an environment size that allows it.
    This might be helpful:
    http://msdn.microsoft.com/en-us/library/ms682653(VS.85).aspx

    If you are a *NIX platform there are similar limits.

    There are reasons to have large command lines when working with various utilities and when using a large number of pipe commands. But this definitely not a Perl limitation. Personally when something like this: >proga ....parms.. | progr B .... | prog c ... gets more than a couple of lines, I split it up and use temp files. Normally these very lengthy things are shell commands. I don't know why your Perl program would have to deal with such a thing.

    Update: I've never seen a limit as low as 257 on a modern O/S. All of these things can do more, it sounds like a configuration problem to me.

      The problem got solved about the number of character inputs when my unix administrator ported the program to another unix box with bash.So , it really was a problem/limitation with the env (the unix env where i was testing the script)and not perl.Now , the script is taking any length of input in the new unix box and the problem is solved.