Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Using user input to return variable typed.

by ColonelPanic (Friar)
on Nov 13, 2012 at 10:54 UTC ( #1003589=note: print w/ replies, xml ) Need Help??


in reply to Using user input to return variable typed.

I agree with others' design suggestions. However, it is worth noting that it is possible to do exactly what you requested:

use warnings; use strict; my ($thing,$foo,$blah) = (1,2,3); my $done=0; while (!$done) { print "What would you like to know? "; $_=<>; chomp; if (/^exit$/) { $done=1; } else { my $result = eval "\$$_"; if ($@) { print "bad variable name\n"; } else { print "\$$_=$result\n";; } } }
Generally speaking, eval is not "good design". Why not? Because the user could type this:
What would you like to know? foo;system("any malicious command here")

You could make this somewhat safer by sanitizing your input, but it is still better to avoid eval in production code. Besides the security issue, it also makes for confusing, hard to maintain code.

Nevertheless, I wanted to point this out because it can be very useful, especially for the quick-and-dirty tasks that Perl handles so well.



When's the last time you used duct tape on a duct? --Larry Wall


Comment on Re: Using user input to return variable typed.
Select or Download Code
Re^2: Using user input to return variable typed.
by myelinviolin (Novice) on Nov 13, 2012 at 14:45 UTC
    I think I mostly figured out the hash thing. (The answer is always hashes, isn't it!) Thanks guys! BUT for instance I have a variable called CMB, for example, that uses a dice rolling subroutine, that gives me random numbers before the hash. (Can you tell what I'm doing? :P) I'm having a problem with this giving me the same dice roll every time. Let's say A = 14 because I rolled a 14. But, after this value goes in the hash, it gets stuck at 14. So if I want to be able to stay in the program and keep typing in numbers, I will always get a 14 for A. Before it goes in the hash, I can keep getting new numbers for the rolls, but I want to get new numbers afterwards as well. Any help for this?
    sub roll{ my ($a)=@_; #chop($a); ($b,$constant)=split(/\+|\-/,$a); ($number,$dice)=split(/[dD]/, $b); if ($constant==''){ $constant=0; } if($a=~ m/\-/){ $constant*=(-1); } $count=0; for ($c=1; $c<=$number; $c++){ $d[$c]=int(rand($dice))+1; $count += $d[$c]; } $count += $constant; if ($count<0){ $count=0; } return "$count"; } $A=roll("1d20"); $B=roll("1d20"); $C=roll("1d20"); print "BEFORE HASH:$A\t$B\t$C\n"; %hash= ( A => "$A", B => "$B", C => "$C", ); print "What would you like to know?\n\n"; $type=<STDIN>; chomp($type); until ($type eq "Q"){ #print "You typed .$type.\n"; print "\n\n$type: $hash{$type}\n"; print "What would you like to know?\n\n"; $type=<STDIN>; chomp($type); }

      A new number will be generated when roll is called. If you want to get a new number, then the value, of "A" for example, needs to be updated each time you want to get it. Once it's stored in a hash, it's stored in a hash. Referring to the variable in the code won't update the value, even if you put the call to roll("1d20") directly into the hash, as in:

      %hash = ( A => roll("1d20"), B => roll("1d20"), C => roll("1d20"), ); # As soon as you get to this point in the code, the value is stored + (for good).

      You need to figure out when you will want to store the value that was rolled, and when you will want to get a new value, and handle that accordingly. If the user wants to get a new "A" value, then you need to call roll again. If they want to get an old "A" value, then you need to refer to the value stored in the hash.

      It seems like the point of the hash in your situation is to store things that don't dynamically change, but things that change as a result of the user doing something. However, if you want a roll to occur, you'll want a different mechanism for actually getting the roll, and the hash will be used just to store that roll.

      I hope I am making sense.

        It had seemed weird to do ALL my calculations each time a person asked something, but that's all I had to do, was to move all the calculations inside the loop where it asks for input. Since I had just figured out how hashes worked, so was still confused on how they worked. I know it all seems so simple once you have the answer, so thanks for putting up with me!

      What is in $b? Where is $CMB? Why do you expect the value stored in a hash to change once it's been set? Why are you not rolling the die each time the user inputs a request?

      If the computer were a person, here is what that person is doing currently:

      1. Roll a die, and write down the result.
      2. Roll another, and write down the result.
      3. Roll again and write down the result.
      4. Read the first result and write it again into a little book of indexes and corresponding values.
      5. Read the second result and write it again into a little book of indexes and corresponding values.
      6. Read the third result and write it again into a little book of indexes and corresponding values.
      7. Repeatedly ask someone what they would like to know.
      8. Look in your little index book and find the answer that was written down for the question that was asked.
      9. Read back to the user the answer that you wrote in your little book.
      10. Wonder why the answer written in the little book hasn't been magically erased and re-written without your intervention.

      In real life, as a person, you would roll the dice each time someone requests a roll. In your program, you roll just one time and store the result. I don't think that caching a die roll is the right approach.


      Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003589]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2014-10-01 20:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (38 votes), past polls