a.alandkar has asked for the wisdom of the Perl Monks concerning the following question:

Hello all mates, I am a new user to this site and a beginner with PERL. I am trying to get input form keyboard and the am comparing it with stored value but its not working, instead if i dont take value from keyboard and hard write it in the code then it works.

Non-working code: #! usr/bin/perl -w $dynamic = 'D'; $nvolatile = "NV"; print ("Enter class: "); $storage = <STDIN>; if(($storage eq $dynamic) || ($storage eq $nvolatile)) { print("\n $storage"); print("\n Done"); } else { print("\n $storage"); print("\n Wrong Class"); }
Problem Elaboration: When i run above mentioned program and take D or NV as input the if condition fails. --------------------------------------------------------
Working Code: #! usr/bin/perl -w $dynamic = 'D'; $nvolatile = "NV"; print ("Enter class: "); $storage = 'D';#<STDIN>; if(($storage eq $dynamic) || ($storage eq $nvolatile)) { print("\n $storage"); print("\n Done"); } else { print("\n $storage"); print("\n Wrong Class"); }

------------------------------------------------------ Please help out to clarify my concept if there is any missing point.

Replies are listed 'Best First'.
Re: Error with User defined data
by choroba (Cardinal) on Aug 13, 2014 at 08:22 UTC
    The final newline is part of the input. Just chomp it away:
    $storage = <STDIN>; chomp $storage;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Thank you. It works. :)

Re: Error with User defined data
by Athanasius (Archbishop) on Aug 13, 2014 at 09:30 UTC
Re: Error with User defined data
by aitap (Curate) on Aug 13, 2014 at 09:53 UTC

    Having solved the chomp problem, you also may like to implement a dispatch table for different variants of your storage class. Code written this way looks cleaner, has less repetitions and places to make a mistake. Example code:

    my %classes = ( D => sub { code_to_handle_dynamic(); ...; }, NV => sub { handle_non_volatile(); ...; }, ); chomp (my $answer = <STDIN>); die "Invalid answer!\n" unless exists $classes{$answer}; $classes{$answer}->();

    Going one step further, you can use the IO::Prompter module to handle chomping and validation.

      I am getting unexpected output after running the below code. Not sure why while loop is not working here in.
      #!/usr/bin/perl print"Enter the key\n"; my $key = <STDIN>; chomp($key); print"Enter the value for the Key $key\n"; my $value = <STDIN>; chomp($value); my %h = (); while (($key ne "") && ($value ne "")) { $h{$key} = $value; print "Value inserted successfully\n"; print"Enter the key\n"; my $key = <STDIN>; # chomp($key); print"Enter the value for the Key $key\n"; my $value = <STDIN>; # chomp($value); } foreach my $keyy (keys %h) { print "$keyy => $h{$keyy}\n"; }
      The loop is taking the values for key and value pair from standard input but, its not exiting if a null value is given to it.

        The problem is that you're redefining $key and $value inside your loop. The result is that the conditional in the while statement is using the values you entered the first time. Just remove the 'my' from the $key and $value inside your loop, and you should be fine. (That's assuming your chomp statements aren't commented out. Since your code isn't formatted properly, I can only guess.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

        You need to cope with scoping. There are variables $key and $value defined on lines 3 and 6 (outside the loop), and then you define another pair of them inside the loop (lines 12 and 15). Because you use my again, they are not reused, but created anew, masking earlier declaration. Had you used warnings and strict (which is recommended for all programs; use diagnostics will also help you), you would have received a message: "my" variable $key masks earlier declaration in same scope at ..., which means:

        A "my", "our" or "state" variable has been redeclared in the current scope or statement, effectively eliminating all access to the previous instance. This is almost always a typographical error. Note that the earlier variable will still exist until the end of the scope or until all closure referents to it are destroyed.
        (taken from perldiag). Because of this your while loop checks for upper-scope variables, which get assigned in the beginning of the program, but modifies other ones, which were created inside the loop.

        Solution (simplified): use my only once per variable.