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

Re: Perl High School

by cLive ;-) (Prior)
on Feb 22, 2002 at 07:03 UTC ( [id://146889]=note: print w/replies, xml ) Need Help??


in reply to Perl High School

Good luck with your project - I've just taken a look and it looks like it's going in the right direction.

A few comments to keep you on your toes:

  • 'If' should be 'if'
  • for and foreach are exactly the same - they just make reading particular statements more natural
  • "While version" - $count += 1; also works - you might also want to cover ++$count and explain the difference between that and $count++ (hint - using print)
  • obscurated => obfuscated
  • gt/le comparison operators - I think you need to explain why one string would be greater than another (see below)
  • rock/scissors/paper - how about a hash version? - you could also introduce tr/[A-Z]/[a-z]/ in this example, to pick up people typing with CAPSLOCK on.
  • you cover tables, but don't mention CGI.pm - when you do, remember that Tr is a special case, and tell them why
  • subs - if you did have warnings on, you'd see that @_[0] is better written as $_[0] :)
  • subs - question for you. If you have a sub called test_sub, what's the difference between "test_sub", test_sub()" and "&test_sub" (not a trick question :)
  • regex - "changes any" to "changes first" - add info about modifiers (g, i & s for now) and examples perhaps - eg s/\n/<P>/g; remember - <nobr>(g)lobal</nobr>, case <nobr>(i)nsensitive</nobr>, treat as a <nobr>(s)ingle</nobr> line
  • matching - (/dog/) needs to be ($animal =~ /dog/)
  • spliting =~ s/t/tt/;
  • recommend students use strict; !! Learn it now before you pick up too many bad habits :)
  • open (OUT,">"C:/temp/newnames.txt") => open (OUT,">C:/temp/newnames.txt")
  • I don't get "Chomp". - you won't if you change $sex into $seatbelt when chomping.
gt example:
if ('four' gt 'five'){ print "four is greater than five\n"; }
I would also look at fun stuff early on - eg, grab a web page and parse some information from it:
#!/usr/bin/perl -w use strict; use LWP::Simple; my $page = get('http://www.google.com'); # regex to extract certain text etc...
Just a personal preference, but I prefer to use CGI in OO way, because for me the code is easier to read, eg:
#!/usr/bin/perl -w use strict; use CGI; my $q = new CGI; print $q->header, $q->start_html, $q->table( $q->Tr( $q->th( 'This is a table header', ), $q->td( 'This is a table cell', ), ), ), $q->end_html; exit(0);

For me, $q is short for $query (but I don't just use it for forms these days...)

That way, you can 'hide' the details of namespaces and exporting a little longer. Or at least avoid issues when they use two modules that export subs with the same names to the main script (ack, I'm getting ham-handed in explaining this, sorry).

I see you have the SAMS book, but I'd suggest you also read "Learning Perl" (the 'Llama').

Something I don't see is an explanation of the line:

#!/usr/bin/perl

but perhaps I missed it...

Oh, and on the subject of first line, I think the -w is ignored under windows (someone, please correct me if I'm wrong here) and you should use "use warnings;" instead.

That's all for now. Perhaps when you have more up, you could ask the monastery to take a look - after all, the last thing we want is for a bunch of little brats coming here to come here and ask us questions when you get paid to do it* ;-)

good luck

cLive ;-)

* - err, that's meant to be humorous - sometimes I have to be explicit because people miss the wink...

--
seek(JOB,$$LA,0);

Replies are listed 'Best First'.
Re: (cLive ;-) Re: Perl High School
by davorg (Chancellor) on Feb 22, 2002 at 07:18 UTC
    my $q = new CGI;

    Or rather:

    my $q = CGI->new;

    The indirect object notation will bite you at some point - please don't use it.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      The indirect object notation will bite you at some point - please don't use it.

      Aw c'mon. People can get bit by indirect object syntax whether or not they use it intentionally. At some point, usually in the 10th hour of a codefest, a typo is going to trigger a cryptic  Can't locate object method "foo" via package "bar" error message.

      The only risk of using the syntax consistently is one might come to believe that new is special.

      If people stick with the new Package form, they're going to be just fine.

        Wrong. Look at the following code:
        package Foo; sub new { print "Right one!\n"; } 1; -------- #!/usr/local/bin/perl use Foo; # Insert 1000 lines of code here sub new { print "Wrong one!\n"; } # Insert 1000 lines of code here my $bar = new FOO; # Compare that with my $bar = FOO->new;
        By using indirect notation, you're removing the capability for the interpreter to spellcheck you if you have defined a new function in the current package. My example is contrived, but what if you're creating an object from within another object? That shows up every once in a while, right? :-)

        The only reasons I can ever see to use indirect notation are the following:

        1. You're a C++ programmer that doesn't want to really learn Perl
        2. You want to do that "cool thing" in Llama3 with no parentheses on method calls.
        (In case you can't tell, I find both those reasons ... lacking.)

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      dave - can you explain in a little more detail why not? No-ones pointed this out to me before...

      thanks

      cLive ;-)

      --
      seek(JOB,$$LA,0);

        For more details see this node (and for even more details, the pages quoted in that node from The Perl Cookbook and Object Oriented Perl).

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

        Here's the (HTML-ized) warning from perlop. Note the word "exclusively" at the end:

        WARNING

        While indirect object syntax may well be appealing to English speakers and to C++ programmers, be not seduced! It suffers from two grave problems.

        The first problem is that an indirect object is limited to a name, a scalar variable, or a block, because it would have to do too much lookahead otherwise, just like any other postfix dereference in the language. (These are the same quirky rules as are used for the filehandle slot in functions like "print" and "printf".) This can lead to horribly confusing precedence problems, as in these next two lines:

        move $obj->{FIELD}; # probably wrong! move $ary[$i]; # probably wrong!

        Those actually parse as the very surprising:

        $obj->move->{FIELD}; # Well, lookee here $ary->move->[$i]; # Didn't expect this one, eh?

        Rather than what you might have expected:

        $obj->{FIELD}->move(); # You should be so lucky. $ary[$i]->move; # Yeah, sure.

        The left side of "->" is not so limited, because it's an infix operator, not a postfix operator.

        As if that weren't bad enough, think about this: Perl must guess (at compile time) whether name and move above are functions or methods. Usually Perl gets it right, but when it doesn't it, you get a function call compiled as a method, or vice versa. This can introduce subtle bugs that are hard to unravel. For example, calling a method "new" in indirect notation--as C++ programmers are so wont to do--can be miscompiled into a subroutine call if there's already a "new" function in scope. You'd end up calling the current package's "new" as a subroutine, rather than the desired class's method. The compiler tries to cheat by remembering bareword "require"s, but the grief if it messes up just isn't worth the years of debugging it would likely take you to to track such subtle bugs down.

        The infix arrow notation using "->" doesn't suffer from either of these disturbing ambiguities, so we recommend you use it exclusively.

Re: (cLive ;-) Re: Perl High School
by hsweet (Pilgrim) on Feb 23, 2002 at 01:42 UTC
    I gotta print this out to parse out all the stuff you wrote. It looks great. Thanks for taking the time.
Re: (cLive ;-) Re: Perl High School
by hsweet (Pilgrim) on Feb 23, 2002 at 02:54 UTC
    I printed out your post. I see I have some re-writing to do. I'm wondering when and if to introduce objects. Thanks for all the detailed suggestions. VERY helpful.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://146889]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-04-19 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found