in reply to user input behavior from within a subroutine

It looks to me as though you have a foreach {} loop that doesn't end properly (well, it is ended by what you seem to think, based on indentation, is the end of the subroutine). Maybe it is just a typo in your posted code ... ?

It seems more likely (based on the code) that you would be repeatedly prompted for attribute value answers ... and since you're not printing a \n on the prompt line, it may look as though you're not really stepping through the attributes ... but still, I would think that your responses would be printed and would result in a carriage return/line feed ...

Can you check your curly braces and make sure the code properly closes out the foreach loop, and post the actual output, please?


No good deed goes unpunished. -- (attributed to) Oscar Wilde
  • Comment on Re: user input behavior from within a subroutine

Replies are listed 'Best First'.
Re^2: user input behavior from within a subroutine
by bw (Novice) on Aug 02, 2006 at 18:15 UTC
    The code is:
    if ($value_add gt 0){ foreach $value (@value_add){ print "Is $value an attribute (y or n)? "; $response = <STDIN>; chomp($response); if ($response =~ "y" || $response =~ "Y"){ print "response is $response\n"; print "configure as an attribute\n"; } else { print "response is $response\n"; print "configure as a parser\n"; } } # end foreach $value (@value_add) }

    The @value_add array is:
    elevation location_type point_name position road type road type description vegetation
    (note: 'road type' and 'road type description' are separate elements).

      So, unless you declare $value up above this snippet, you aren't using strict, which is foolhardy. You seem to be using a string comparison operator (gt) for a numerical comparison, which doesn't seem the best way to skin this cat.

      In any case, the following standalone snippet works fine under perl 5.8.8 on a Solaris platform:

      #!/usr/local/bin/perl use strict; use warnings; Main: { my @param_names = ('elevation','location_type','point_name','positi +on','road_type','road_type_description','vegetation'); categorize_attributes(\@param_names); } sub categorize_attributes { my ($value_ref) = @_; my @value_add = (); @value_add = @{$value_ref} if ref($value_ref) eq 'ARRAY'; if (scalar(@value_add) > 0){ foreach my $value (@value_add){ print "Is $value an attribute (y or n)? "; my $response = <STDIN>; chomp($response); if ($response =~ /^y$/i){ print "response is $response\n"; print "configure as an attribute\n"; } else { print "response is $response\n"; print "configure as a parser\n"; } } # end foreach $value (@value_add) } }

      Update: Modified code sample so that the user input was handled by a subroutine, as specified by the OP.

Re^2: user input behavior from within a subroutine
by ikegami (Patriarch) on Aug 02, 2006 at 18:10 UTC
    Good point, it could be a cacheing issue. Adding $|=1; before the prompt might do the trick.

    Update: Oops. Reading from STDIN flushes STDOUT.

Re^2: user input behavior from within a subroutine
by bw (Novice) on Aug 08, 2006 at 17:02 UTC
    Thanks to ptum, rodion, ikegami, and liverpole for all your assistance. I found out that the source of my problem was a line that looked like:

    my @array=`cat path_to_target_file`;

    I changed that portion of the code to an open()/close() operation and the problem went away.