#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    1
    Rep Power
    0

    While loop with diamond operator


    Hello

    I am trying to run a perl script on tomcat server. I am putting the incoming http request in a variable data which collects the headers as well as the text file attachment data. The code is like this :

    ....

    while(<>){

    $data = $data . $_ ;

    }

    ...



    This works fine on apache but on tomcat 6.0 this is getting hanged.

    What is the issue?



    Thanks in advance

    Harsh
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    49
    Rep Power
    14
    I don't know why the different servers are having issues, maybe the CGI configuration?

    However, could you try this in place of the `while` loop?:

    [code=perl]
    $data = do{ local $/; <>}
    [/code]

    What it does is reads <> line by line and appends each line into local variable $/, which $data gets assigned the value of $/ when the do loop exits (caused by EOF or an error from <>).
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    You probably mean:

    Perl Code:
    $data = do{ local $\; <>}


    And it does not do what you say.

    It gives a local undefined value to $\, which is the input record separator, thus enabling the whole file to be slurped directly into the $data variable.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    macaswell's use of $/ is correct ($\ is the output record separator automatically added to any print), but Laurent_R properly describes the behavior. If you want to avoid confusing incantations, there are modules on CPAN (Perl6::Slurp and File::Slurp) that package up that functionality.

    Is there any reason why you're not using one of the existing CGI modules to handle processing of the HTTP request? (Assuming this is CGI--you don't say how your script is being called from tomcat).
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    Originally Posted by OmegaZero
    macaswell's use of $/ is correct ($\ is the output record separator automatically added to any print),
    Right, I mixed up input and output record separators. I should have checked in the documentation before I answered.

IMN logo majestic logo threadwatch logo seochat tools logo