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

Re: [MacPerl] Please don't Laugh at me...



Jorge A. Chica, <jac4586@acf2.nyu.edu> wrote:

> I am relatively new to MacPerl and I would like to know what is wrong
> with my code.

You are, however, about to get HELP.  Others may point out the core of 
your current problem, but they're just treating the symptoms.  What 
follows is intentional overkill, cleverly designed to set you on the path 
toward writing better Perl overall:

> print ('Please type something: ');
> $input = <STDIN>;
> if ($input = PAULINE){
> print ("Hola amigo\n")
> }else{
> print("I don't know you\n")
> }

Ouch! *That* is REALLY BAD for the EYES! Happily it's non-fatal in this 
instance (some would say saintly, but I digress).  What I mean is that 
your code isn't stylistically very pretty. Clean code is so much easier 
to follow.  What I'm about to do is a bit verbose with comments. I'm 
being a bug, I know, but:

#!perl -w

use strict                       # Our friend the 'strict' pragma
my($login)                       # Only one variable, but since 'strict'
                                 # is now in force, we must declare it.

print 'Please type something: '; # Parens not needed here

$input = <STDIN>;

chomp($input);                   # Remove newline from STDIN!

if ($input eq "Pauline") {       # Quotes make this clearer
                                 # Note that I'm using the string equate
                                 # and not the ASSIGNMENT operator! Here's
                                 # your problem.
    print "Hola amiga\n";        # Indented! (I could be wrong, but 
Pauline 
                                 # sounds female) Again, terminating 
semicolon
                                 # isn't necessary in context, just a 
stylistic 
                                 # nicety.
   }
   else                          # else statments uncuddled.
   {  
   print "I don't know you\n";   # again stylistic niceness with the 
semicolon!
}

__END__

I use the __END__ mostly because in the context of an email message, the 
code may be surrounded by non-code text.  Perl has ways of dealing with 
text BEFORE the #! line, and ignores (unless there's __DATA__) what's 
beyond the __END__ statement.  Long time perlers should forgive me for 
making this OBVIOUS statement.

>I want it to print out hola amigo if the STDIN is Pauline.  If not I want
>it to print out I don't know you.  Sorry to bother you guys...

And now it does!

Okay, to summarize:

Turn on warnings. Syntax checking alone isn't always going to help. Use 
the 'strict' pragma, unless you have a *really* good reason NOT to.  The 
'strict' pragma will rap you on the knuckles with a willow switch, just 
like my late grandmother used to do, but you'll learn to miss it when 
it's not there (Grandma died when I was fifteen, and I've had to iron my 
own shirts ever since).  So use strict, unless you really LIKE ironing 
your own shirts (@#$%^&! mixed metaphor)!

"=" is the ASSIGNMENT operator.  Common error for new users is to use it 
in place of "eq" or "==".  Drill it into your brain that "=" is for 
assigning values to variables and NOTHING MORE.  In the above example, I 
actually might have done better to use "=~" rather than "eq", and avoided 
using "chomp()", but I needed to show you chomp() to indicate a potential 
problem with the input.

The semi-colon is your best friend.  Learn to use it even when you don't 
really need to.  I know this is at odds with what Larry Wall says is good 
style, but not using the semi-colon and then using it is inconsistant.  
There are times when it's needed, and times where it isn't.  However 
trying to determine when NOT to use it, is harder than just using it 
IMHO.  You never know when that single line block may mutate into six 
hundred lines of code, to the point where you can't FIND the original 
line without the terminating semi-colon.  The syntax checker may put you 
somewhere in the general neighborhood of the error, but it's not perfect, 
nor should it need to be.

Finally, Perl doesn't enforce good style.  This doesn't mean that you 
should abandon style and produce obsfucated code, but that you must apply 
the stylistic rules yourself. Read "perlstyle.pod" (the Perl Style 
Guide).  I diverge slightly from what's indicated in that document, in 
that I tend to be liberal with the use of semicolons in short blocks, but 
only because I feel this is clearer, and not for any other reason. 
Personal choice.

If you stick around this list long enough, you'll see some excellent 
examples of good style.

final advice from perlstyle.pod:

	*	Be consistent. 

	*	Be nice. 

BTW, Pauline will still have to be capitalized.

Just TRY getting this type of assistance on comp.lang.perl.misc!

--B


# Fungal Parataxonomy                   Mycology Information (Mycoinfo)
# Webmaster, Staff Writer      **The World's First Mycology E-Journal**   
# <mailto:webmaster@mycoinfo.com>            <http://www.mycoinfo.com/> 
#
# First they ignore you. Then they laugh at you. Then they fight you.
# Then you win.                                     --Mohandas Gandhi


===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org