Krem, I couldn't help but be a stickler and correct your Perl code for you. I hope you can forgive me, but s/w dev is what I do for a living, and Perl is a language we use extensively at our company. Please take this in the jest in which it is offered.
use strict;
use warnings;
print "Please specify which code you'd like me to write: ";
my $lang = <>; chomp($lang);
my $hit = 0;
foreach (qw{php perl mysql})
{
$hit = 1 if $lang eq $_;
}
print $hit
? "I'd be delighted to write $lang for you."
: "I'm sorry, I don't understand $lang."
, "\n";
You forgot to include the strict and warnings pragmas. Perl code can't be considered "tested" until it has run correctly with those pragmas turned on.
"@code[$a]" is incorrect. By using an index, you're implying a scalar context. While @ is used to denote an array variable, you're not actually requesting the array, you're requesting a scalar from that array, thus you need to refer to it as a scalar: $code[$a]. The only instances where you would keep the @ identifier would be if you are extracting (and expanding) an array reference which happens to be stored in the @code array at index $a, but then your notation would need to be @{$code[$a]} (or optionally without the braces as @$code[$a], but I'm not personally fond of that style).
By doing your print() inside the loop, you would end up with 3 lines of output. In the case where they input a language in your approved list, then the user would see one line saying you're delighted to use that language, and two lines saying you don't understand other languages they didn't even type. If they input a language not present in your approved list, then they see three lines of output each stating you don't understand a language, none of which may even be close to what they typed.
When you read from stdin, you also read in the newline character. Without chomp/chop()ing it first, it won't matter what they type, you won't match any of your languages against it. Also, specifying the stdin filehandle explicitly is often optional. "<>" will default to stdin until you change your active filehandle. It doesn't hurt to be explicit about it, though, especially if you have code dealing directly with a lot of files.
!= is a numeric "not" comparison. While Perl can internally treat strings as numbers (and this comes in handy when you want to do really weird things), it would be much clearer and safer to use the string comparion operators: eq (equivalent), ne (not equivalent).
Worst of all, you forgot to print a newline after your output. This is a terrible, unforgivable thing, as it leaves the user's shell prompt on the same line as your output! Tsk tsk.
I will commend you on at least explicitly defining and scoping all your variables. Gold star for that.
And if I made any typo or mistake in my revision of your code, you have every reason to tear me a new one. :)
Edited, Thu Jan 19 01:04:03 2006 by galkagoby Edited, Thu Jan 19 01:43:02 2006 by galkagoby