in reply to Increasing Buffer size of STDIN on PERL Program

Try the following code....

{ local $/; # turn off input separator $input = <>; }
This reads everything from <STDIN> into the variable $input in one go. I believe this is the quickest method of reading input.

Replies are listed 'Best First'.
Re: Re: Increasing Buffer size of STDIN on PERL Program
by williamp (Pilgrim) on Sep 10, 2003 at 06:08 UTC
    Hi O Monks, Changing $/ looks a cool way of getting your data in. However are there any occasions where you could run into problems?. I notice you use "local", would "my" be even safer within those brackets?. Thank you for your reply.
      The reason why I use local $/ instead of my $/ is the difference between how local and my work.

      The keyword local has been around for a long time, way before the keyword my (which was introduced only after Perl 5).

      The difference is that local is run-time scoping, while my is compile-time scoping. Local $/ will first push the value of current $/ onto the stack, and then when the code leave the scope, automatically restores its previous content. my $/ will not work here, it will give you a perl compilation error, any $-punctuation variable will have to be localized with local, it's a limitation (or feature?) of perl. local keyword reuses a variable already exists in the global context.

      This method is not efficient if you have lots of data (hundreds of Mb) pumping out from another process, because it will have to allocate memory to hold the input.

Re: Re: Increasing Buffer size of STDIN on PERL Program
by BazB (Priest) on Sep 10, 2003 at 21:44 UTC

    This is only advisable if the input data will fit into the available memory without compromising other processes at the same time.
    You don't want the system to start paging heavily or exhaust memory just for the sake of having all data in memory at once.

    Slurping files can sometimes be advantageous.
    Other times using read() or sysread() to read large blocks of data might do the trick.
    Personally I still find myself using a simple  while (<FH>){...} for most jobs, from logfiles to multi-gigabyte datasets.

    Cheers,

    BazB


    If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
    That way everyone learns.