[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Programming help



<P><FONT SIZE=-1>I am a beginner in MacPerl.&nbsp; To be very honest</FONT>
<BR><FONT SIZE=-1>it is my first try at learning a language.&nbsp; I have</FONT>
<BR><FONT SIZE=-1>encountered some problems.&nbsp; Can anyone be</FONT>
<BR><FONT SIZE=-1>of some help?</FONT>

<P><FONT SIZE=-1>I have transcribed below a programme from Learning</FONT>
<BR><FONT SIZE=-1>Perl by Randal Shwartz and Tom Christiansen</FONT>
<BR><FONT SIZE=-1>(Chapter 1) which has come to confuse me in the end.</FONT>
<BR><FONT SIZE=-1>As there is no explicit instructions given, I am unable</FONT>
<BR><FONT SIZE=-1>to configure a secret-words list&nbsp; to make the programme</FONT>
<BR><FONT SIZE=-1>work.&nbsp; The lesson in the end of the Chapter 1 says:</FONT>

<P><FONT SIZE=-1>&nbsp; 1. Type in the example programs and get them to</FONT>
<BR><FONT SIZE=-1>&nbsp;work.&nbsp; (You will need to create a secret-word
lists</FONT>
<BR><FONT SIZE=-1>&nbsp;as well).&nbsp; Consult your local Perl guru if
you</FONT>
<BR><FONT SIZE=-1>&nbsp;need assistance.</FONT>
<BR><FONT SIZE=-1>&nbsp;</FONT>
<BR><FONT SIZE=-1>&nbsp;I am sorry to say there is no Perl guru that I
know</FONT>
<BR><FONT SIZE=-1>&nbsp;of in India.&nbsp; I am sending this to the MacPerl
Mailing</FONT>
<BR><FONT SIZE=-1>&nbsp;list to have someone answer my question.</FONT>
<BR><FONT SIZE=-1>&nbsp;</FONT>
<BR><FONT SIZE=-1>&nbsp;thanks in advance</FONT>
<BR><FONT SIZE=-1>&nbsp;</FONT>
<BR><FONT SIZE=-1>&nbsp;Amitava Basu</FONT>
<BR><FONT SIZE=-1>&nbsp;</FONT>
<BR>&nbsp;

<P><FONT SIZE=-1>#!/usr/bin/perl</FONT>
<BR><FONT SIZE=-1>init_words ();</FONT>
<BR><FONT SIZE=-1>print "What is your name? ";</FONT>
<BR><FONT SIZE=-1>$name = &lt;STDIN>;</FONT>
<BR><FONT SIZE=-1>chomp ($name);</FONT>
<BR><FONT SIZE=-1>if ($name =~/^randal\b/i) {</FONT>
<BR><FONT SIZE=-1>&nbsp;print "Hello, Randal, How good of you!/n";</FONT>
<BR><FONT SIZE=-1>&nbsp;}else{</FONT>
<BR><FONT SIZE=-1>&nbsp; print "hello, $name!/n";</FONT>
<BR><FONT SIZE=-1>&nbsp; print "What is the secret word?";</FONT>
<BR><FONT SIZE=-1>&nbsp; $guess = &lt;STDIN>;</FONT>
<BR><FONT SIZE=-1>&nbsp; chomp ($guess);</FONT>
<BR><FONT SIZE=-1>&nbsp; while (! good_word ($name, $guess)) {</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp; print "Wrong, Try Again!!";</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp; $guess = &lt;STDIN>;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp; chomp ($guess);</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=-1>&nbsp;</FONT>
<BR><FONT SIZE=-1>&nbsp; dbmopen (%last_good, "lastdb", 0666);</FONT>
<BR><FONT SIZE=-1>&nbsp; $last_good {$name} = time;</FONT>
<BR><FONT SIZE=-1>&nbsp; dbmclose&nbsp; (%last_good);</FONT>
<BR><FONT SIZE=-1>&nbsp; sub init_words {</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp; while ($filename = &lt;*.secret>) {</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp; open (WORDSLIST, $filename) ||</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; die "can't open
$filename: $!";</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (-M WORDSLIST
&lt; 7.0) {</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp; while ($name = &lt;WORDSLIST>)
{</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; chomp ($name);</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $word = &lt;WORDSLIST>;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; chomp ($word);</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $words {$name} = $word;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rename ($filename,
"$filename.old") ||</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
die "can't rename $filename.old: $!";</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
close WORDSLIST;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</FONT>
<BR><FONT SIZE=-1>&nbsp;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp; sub good_word {</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp; my ($somename, $someguess) = @_;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp; $somename =~ s/\W.*//;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp; $somename =~ tr/A-Z/a-z/;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp; if ($somename eq "randal") {</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp; return 1;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp; } elsif (($words {$somename} || "groucho")
eq $someguess) {</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp; return 1;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp; } else {</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; open (MAIL, "|mail YOUR_ADDRESS_HERE");</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print MAIL "bad news:
$somename guessed $someguess \n";</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; close MAIL;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT>

<P><FONT SIZE=-1>&nbsp;&nbsp; #!/usr/bin/perl</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp; while ($filename = &lt;*.secret>)
{</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp; open (WORDSLIST, $filename)
||</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp; die "can't open $filename: $!";</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp; if (M WORDSLIST &lt; 7.0) {</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ($name = &lt;WORDSLIST>)
{</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; chomp ($name);</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $word = &lt;WORDSLIST>;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; chomp ($word);</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; close (WORDSLIST);</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; format STDOUT =</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
@&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $filename, $name, $word</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; format STDOUT_TOP =</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Page @&lt;&lt;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $%</FONT>
<BR><FONT SIZE=-1>&nbsp;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Filename&nbsp; Name&nbsp;
Word</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ============== ==========
===========</FONT>
<BR><FONT SIZE=-1>&nbsp;</FONT>
<BR><FONT SIZE=-1>&nbsp;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #!/usr/bin/perl</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbmopen (%last_good, "lastdb",
0666),;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach $name (sort keys
%last_good) {</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $when = $last_good {$name};</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $hours = (time - $when)
/ 3600;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; write;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=-1>&nbsp;</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; format STDOUT =</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; User @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;:
last correct guess was @&lt;&lt;&lt; hours ago</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $name, $hours</FONT>
<BR><FONT SIZE=-1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .</FONT></HTML>

</html>