Recently one of my friends wrote a guess-the-word game in c, which prompted me to write a shorter one in perl, with more features. The coding was fine until I decided to support an option to use a random word from /usr/dict/words as the answer as opposed to one entered to <>.

Essentially the problem was that I passed the dict as $ARGV[0] and subsequently attempted to use <>, only to find that the behavior changed due to the presence of @ARGV as per perlop <EXPR>. While I was able to "fix" the code by clobbering @ARGV, equating it to the empty list, this brings me to the crux of my question:

When using both @ARGV and <STDIN>, what's the perlish implementation without clobbering @ARGV?

I would presume explicit use of <STDIN> or even readline(*STDIN) is better coding practice, but I'm at a loss as to a perlish impelementation.

UPDATE: By request, the guess-the-word game code:

#!/usr/bin/perl -w use strict; my ($answer, $guess, $guesses) = ("", "", 0); if (@ARGV) { open FILE, $ARGV[0] or die "No such dictionary..."; rand($.) < 1 && ($answer = (split(/[\r\n]+/,$_))[0]) while <FI +LE>; close FILE; @ARGV = (); } else { $answer = (split(/[\r\n]+/,<>))[0]; } system 'clear'; do { $guess = (split(/[\r\n]+/,<>))[0]; unless ($guess eq $answer) { if (length $guess != length $answer) { print "The answer is ", length $guess > length + $answer ? "shorter" : "longer", "\n"; } else { print "The $$_[0] letter ($$_[1]) is ", $$_[2] +? "" : "in", "correct\n" foreach map { [$_, (split //, $guess)[$_], ( +split //, $guess)[$_] eq (split //, $answer)[$_] ? 1 : 0 ] } (0..(len +gth $guess) - 1); } } $guesses++; } while ($answer ne $guess); print "You figured it out in a mere $guesses guesses\n";

Lastly, I should note that this was a question of style and practice, rather than a request for help with a particular code. After reading the reply by EvanCarroll, which concurs with my presumption that using <STDIN> is better programming practice, I re-read perlop and discovered several loops of the form:

while (<>) { ... }

When I read such passages while learning perl, I misunderstood <> for <STDIN>. While this misunderstanding has taken awhile to surface, I now know the distinction between <> and <STDIN>.

Thanks to everyone who helped out -- and feel free to try the game :-)


In reply to Using <> in the presence of @ARGV by eibwen

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, details, 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, summary, 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.