in reply to Re^2: CGI, STDIN and chomp problem: bug?
in thread CGI, STDIN and chomp problem: bug?

I'm sorry, Doctor, I wasn't clear enough. I only get the pain when I do *this*, ouch.

CGI scripts don't read input interactively from users so the "bug" here is your "bug" of trying to use CGI.pm along with stuff that clearly won't work in a CGI script.

No, I doubt CGI.pm is changing $/. Have you looked at the source code? My guess is that CGI.pm is doing binmode on STDIN (which it makes some sense for it to do).

Now, if you want to actually explain why you think it makes sense to combine CGI.pm with interactive input, for example, by telling us more about what you are trying to do, then we could probably tell you the proper way to prevent this problem.

No, setting $/ like you did is not the proper way, actually. Please take a look at what $/ is set to when you don't use CGI.pm. And try not to be confused by the unfortunate wording of perlport. :)

- tye        

  • Comment on Re^3: CGI, STDIN and chomp problem: bug? (input)

Replies are listed 'Best First'.
Re^4: CGI, STDIN and chomp problem: bug? (input)
by Not_a_Number (Prior) on Jul 14, 2007 at 17:23 UTC
    Now, if you want to actually explain why you think it makes sense to combine CGI.pm with interactive input...

    I apologise once again, I was trying to keep it simple, reducing the problem to the simplest case...

    As it is, I see more explanation is in order. I have a perl script using sqlite that works very well. Very much simplified, it looks like this:

    use strict; use warnings; use DBI; my @fields = ( qw/ Genus Species Cultivar Common_Name / ); my $field = get_field( @fields ); my $value = get_value( $field ); # Omitted: code to connect to DB my $select_query = qq/ select * from plant where $field = '$value' /; # Omitted: code to execute, fetch and deallocate SQL query sub get_field { # Highly simplified for testing purposes return $_[3]; } sub get_value { print $_[0], ': '; chomp ( my $choice = <STDIN> ); # <- Problem here! return $choice; }
    ...by telling us more about what you are trying to do...

    I am converting this program to a CGI script. I decided to do the HTML output bit first. Of course, once I have finished, I will not be combining CGI.pm with interactive input once the development phase is finished. However, I was surprised that adding use CGI; to my code added a subtle bug that it took me a very long time to track down.

    ...then we could probably tell you the proper way to prevent this problem

    It's all right, thanks: as I say, I've now tracked down the bug (which thanks to your explanation I accept is in my code, not in CGI.pm :) and found a workaround.