Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
    0: #!/usr/bin/perl -w 
    1: use strict; 
    2: ## I originally wrote this for a column,
    3: ## but haven't gotten around to using it yet.
    4: ## just think of an animal, and invoke it.
    5: ## It's an example of a self-learning game.
    6: ## When you choose not to continue, it'll dump out
    7: ## the data structure of knowledge it has accumulated.
    8:  
    9: use Data::Dumper; 
    10:  
    11: my $info = "dog"; 
    12:  
    13: { 
    14:   try($info); 
    15:   redo if (yes("play again?")); 
    16: } 
    17: print "Bye!\n"; 
    18: print Dumper($info); 
    19:  
    20: sub try { 
    21:   my $this = $_[0]; 
    22:   if (ref $this) { 
    23:     return try($this->{yes($this->{Question}) ? 'Yes' : 'No' }); 
    24:   } 
    25:   if (yes("Is it a $this")) { 
    26:     print "I got it!\n"; 
    27:     return 1; 
    28:   }; 
    29:   print "no!?  What was it then? "; 
    30:   chomp(my $new = <STDIN>); 
    31:   print "And a question that distinguishes a $this from a $new would be? "; 
    32:   chomp(my $question = <STDIN>); 
    33:   my $yes = yes("And for a $new, the answer would be..."); 
    34:   $_[0] = { 
    35:            Question => $question, 
    36:            Yes => $yes ? $new : $this, 
    37:            No => $yes ? $this : $new, 
    38:           }; 
    39:   return 0; 
    40: } 
    41:  
    42: sub yes { 
    43:   print "@_ (yes/no)?"; 
    44:   <STDIN> =~ /^y/i; 
    45: } 
    

In reply to "animal" by merlyn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (4)
    As of 2020-12-01 01:34 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?