NoobiePerlist has asked for the wisdom of the Perl Monks concerning the following question:

Hey Monks, I am getting a book to start off with. It is highly recommended by ybiC and other wise monks so I raided the piggy banks and got a copy. The thing that still puzzles me is that even though I know nothing about Perl Programming or Programming for that matter, why is it a big black screen that when you type does nothing and yells at you about things that are confusing and uncomprehendable? This is very weird I was expecting more of a interactive or read me type program that you type lines and expect something other than guessing at what it is going to yell at you about. Anyway, if any monk would like to comment on this that will be great but I think my posts get lost because I can't find them even right after I post them. :S later monks. -Noobie

Replies are listed 'Best First'.
(jeffa) Re: Perl What for?
by jeffa (Bishop) on Sep 08, 2002 at 02:53 UTC
    "...yells at you about things that are confusing and uncomprehendable?"

    Hi NoobiePerlist. There is indeed a very good reason for that. Because the interpreter does not know what you are trying to do, it only does what you tell it to do. When i first started seriously programming back in 1996 (with C++ no less) i felt the same way. "What the heck does that mean?!?!?!" With C++, there is no interpreter, but instead a compiler. The principle is still the same though ... something reads your program one line at a time and attempts to do something with it. When you tell something to do an instruction it does not understand, that something tries very hard to tell you why it cannot. Unfortunately, until you learn what is really going on under that hood, you will not understand what something is trying to tell you.

    Now, fast forward a couple of years. I am taking a compiler course where we have to write our own compiler that will take a language that we design and turn a program written in that language into instructions (called machine code) that makes the computer do stuff. Add numbers, assign values to variables, etc. Suddenly, i have to decide what my compiler will do when it encounters an instruction that it cannot parse. What the heck was the person who wrote this trying to do? I don't know. I will never know. Are you starting to see the light? A compiler (in the case of languages like C++ and the minimal one i wrote) or an interpreter (in the case of languages like Perl) can only do what the person who wrote the program tells it to do. It cannot guess what they are trying to do, and as such, it can only hint at what is wrong. And let me tell, those wacky confusing incomprehensable messages that the compiler/interpreter spits out at you when you do something wrong are really quite wonderful, once you get some experience under your belt.

    My best advice to you is the same advice i give to my C++ lab students. First: code in small chunks - don't write an entire program first and then try to debug the entire thing. Instead, code a couple of lines and debug those - at least then you will be more likely to guess what really went wrong. Second: when you do have errors, focus on the first one - not the last one. Solve the first one and the others might just go away too.

    Also, you can use perldiag to help you decipher some error messages that you will encounter. Good luck, and remember, learning to program does not happen overnight. :)

    Oh yeah - regarding this statement:

    "...my posts get lost because I can't find them..."

    Registered members can always find their posts by going to their home node and clicking on the number next to the Writeups attribute. For example, here are yours . :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Perl What for?
by BrowserUk (Patriarch) on Sep 08, 2002 at 03:22 UTC

    Hi NoobiePerlist,

    As I know from the CB that you are using Windows and ActiveState Perl, you might like to try this. Cut and paste the first program below into Notepad and save it as play.pl

    #!perl -w $|++;$,=' '; print $/,'perl> '; eval($_) || print $@,$/ and print $/,'perl> ' while(<>);

    Then open a CMD window and type "perl play.pl". You end up with a prompt that looks like this perl>. This will allow you to try out simple perl programs easily, and if you make a mistake, you can use the arrow keys to retrieve the line and edit it before trying again.

    I've pasted a short example session below. Have fun!

    C:\test>perl play.pl perl> print 'hello world!' hello world! perl> $x = 100 perl> $y = 10 perl> print $x/$y 10 perl> print $_, "\n" for 1 .. 10 1 2 3 4 5 6 7 8 9 10 perl>

    Well It's better than the Abottoire, but Yorkshire!

      This is close to what you probably have in mind when saying "interactive Perl". Unfortunately, this one has some confusing behaviour with scopes, caused by the eval block:

      perl> $_ = 'Foo'; print; Foo perl> print; print; perl> my $a = 'Bar'; print $a; Bar perl> print $a; Use of uninitialized value in print at (eval 4) line 1, <> line 4.
      Writing the same commands in a file scoped script would produce different results.

      I think it might be better to go this way:
      Write a testscript in your favorite text editor. It might look like this:

      #!perl # header use warnings; # enable warnings use diagnostics; # explain warnings # code to test: print "Hey world, I'm coming!!!\n";
      Then you can save the script (e.g. "C:\test.pl"). Open your DOS command window and type in the following to run your script:
      perl C:\test.pl
      When your script succeeds, you can modify it (trying out something else), save it and run the command again.

      Hope this helps, have a lot of fun with Perl!

      ~Django
      "Why don't we ever challenge the spherical earth theory?"

        I tried to put myself in the place of a 13 y/o kid with no programming experience sitting at his Windows/ME machine staring at a screen that looked like:

        C:\>perl ashdahskjd kajhsdakjshd

        I am aware that play.pl has scoping problems, but once his copy of Programming Perl for Win32 arrives, he may arrive at the point where he knows what scoping is.

        I also thought about what would happen if his "favorite editor" is Word.

        C:\>perl test.pl Can't open perl script "test.pl": No such file or directory C:\>dir test.pl Volume in drive C has no label. Volume Serial Number is 8C87-E163 Directory of C:\ File Not Found C:\>dir test* Volume in drive C has no label. Volume Serial Number is 8C87-E163 Directory of C:\ 02/09/08 07:18a <DIR> test 02/09/08 05:16p 19,456 test.pl.doc 2 File(s) 19,456 bytes 302,397,440 bytes free C:\>perl test.pl.doc Unrecognized character \xD0 at test.pl.doc line 1. C:\>

        Well It's better than the Abottoire, but Yorkshire!
      Why all the effort? perl -wde1 and then just play on the prompt. :-) It also lacks the scoping problems Django mentioned.

      Makeshifts last the longest.

        Maybe this is a matter of taste, but I think the Perl debugger might be confusing for the newbie. It has its own commands and doesn't execute programs the way they would normally run. Learning one thing after another, not all simultaneously is very important. I'd start with the most essential features and move on to the "specials" later. But as always, TMWTDI -- so check it out.

        ~Django
        "Why don't we ever challenge the spherical earth theory?"

        C:\>perl -wde1 Default die handler restored. Loading DB routines from perl5db.pl version 1.07 Editor support available. Enter h or `h h' for help, or `perldoc perldebug' for more help. main::(-e:1): 1 DB<1>

        "Ah! Help!"

        DB<1>h T Stack trace. s [expr] Single step [in expr]. n [expr] Next, steps over subroutine calls [in expr]. <CR> Repeat last n or s command. r Return from current subroutine. c [line|sub] Continue; optionally inserts a one-time-only breakpoin +t at the specified position. l min+incr List incr+1 lines starting at min. l min-max List lines min through max. l line List single line. l subname List first window of lines from subroutine. l $var List first window of lines from subroutine referenced +by $var. l List next window of lines. - List previous window of lines. w [line] List window around line. . Return to the executed line.

        "Um!"

Re: Perl What for?
by peschkaj (Pilgrim) on Sep 08, 2002 at 03:39 UTC

    If you are purchasing "Programming Perl" (which I feel would be a wise choice once you have devoured "Learning Perl") you will find a marvelous section in the back of the book which catalogs many of the incomprehensible errors that you can receive.

    Perl is, pretty much, designed to be fed a whole program and it will then attempt to interperet it. I remember the first Perl program that I wrote on my own. I typed and consulted references until I was sure that I had written a nice little command line based mailer that did only what I wanted. I ran the program and did not receive any useful error. Rather, I saw something along the lines of "You fool! You managed to core dump perl!"

    Needless to say, I was astonished. But I have learned through trial and error, over the past two years, a bit more about coding. I cannot agree with my fellow monks enough: code in snippets.

    A great thing I figured out is this: If you can contain some code in a subroutine (which you'll find out about soon enough) you can run it as a separate program. By breaking your code down into the smallest logical components you can think of, you can test them as small programs and then bring them back into the whole. This will let you test the functionality of each piece and you can learn where to look for errors.

    Best of luck to you, and don't get disheartened by the learning curve. We've all gone through it, and are all willing to help as much as we can.

    Your posts will appear lost unless you are looking the "Newest Nodes" section. Sometimes they might not appear for a while, depending on how long you have caching set for. But that's just a good guess on my part.



    If you make something idiot-proof, eventually someone will make a better idiot.
    I am that better idiot.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Perl What for?
by grantm (Parson) on Sep 08, 2002 at 22:53 UTC

    When you install ActivePerl on Windows, the default setting is to associate the .pl extension with perl.exe. When you double click a .pl file in explorer, windows opens a cmd window, runs the script and closes the window. I don't find this very useful and I know a lot of newbies find it confusing (error messages flash up and disappear).

    I recommend associating .pl with your favourite editor instead (I'll refrain from making a recommendation).

    I also recommend installing Micorosoft's Power Toys. Specifically the doshere.inf which allows you to right click on a folder in explorer and select 'Open a command prompt here'.