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

I'm *really* new to Perl, so my question is probably going to sound real stupid. Anyhow, I am reading "Learning Perl" by Randal Schwartz (AKA "The Llama Book") and I am trying to run my first simple program using Terminal on OS X. Here's the program:
#!/usr/bin/perl print "Hello, world!\n";
I put these lines in a text editor (in BBEdit, actually). The book said to then enter the following command (I'm not sure if Schwartz meant within Perl):
$ chmod a+x my_program
Then, enter this command:
$ ./my_program
Now I had tried running this in Terminal and it gave me this message:
"Bareword found where operator expected at - line 1, near "$ chmod a"<br< (Missing operator before a?)"
I then dropped the $ and tried typing the command "chmod a+x my_program" and it gave me no respone. In fact, it seems like it ignored it without by returning me to the command prompt.
Am I doing something wrong? I also have the document on my Desktop. Am I not supposed to have it there? Also, Schwartz mentioned something about not having an extension on the text file. I don't see any, but should I change it to .plx anyway?
Any advice? Thanks in advance from an aspiring Perl programmer.

Replies are listed 'Best First'.
Re: Using Perl on OS X
by Zaxo (Archbishop) on Sep 13, 2002 at 05:46 UTC

    Your second try worked. The dollar sign in those instructions represents the command line prompt. Most *nix commands are silent on success. You can now run the script by typing ./my_program at the prompt.

    Even without running ./my_program as an interpreter script, with executable permissions set by the chmod command, you can run it by typing perl my_program

    After Compline,
    Zaxo

      Thanks! I actually have a few more questions:

      When I type "chmod a+x my_program" (no $ sign) am I supposed to get some response? It didn't seem to return anything. I then typed "perl my_program" and again, no response. Am I supposed to see a "Hello, world!" line pop up? If not, can someone provide me a simple Perl code that will create a response?

      One more question: I'm supposed to start every new script with:  #!usr/bin/perl right? I've double-checked already and made sure Perl is in that directory!

      Thanks!

        When I type "chmod a+x my_program" (no $ sign) am I supposed to get some response?

        No. That chmod call sets a bit in your file's directory entry that marks it as executable by all users. That way, when you type ./my_program, the system knows it can execute your script. There's usually no response.

        If you're curious, you can type man chmod; that will explain the various bits that can be set to allow reading, writing, and executing the file for the owner, his group, or all users.

        One more question: I'm supposed to start every new script with: #!usr/bin/perl right? I've double-checked already and made sure Perl is in that directory!

        Correct! The '#!' tells the system that you're about to tell it what program to use to run the file.

        You could add a -w to that line if you want to run your program with warnings enabled. The "canonical" way to start a script is:

        #!/usr/bin/perl -w use strict;
        use strict turns on additional features like errors for undeclared variables.

        If you're using a more recent perl, you could replace the -w with "use warnings;", but I think that can wait until you get through your first perl book.

        Good luck!
        --
        Mike

Re: Using Perl on OS X
by Chady (Priest) on Sep 13, 2002 at 05:46 UTC

    The $ actually is the shell symbol, you shouldn't really type it, it's there to indicate that you are typing this on a shell... as for not returning anything, that's what the chmod does... it returns something only when it fails, you can now see if it worked by doing

    $ ls -l my_program

    without the $ of course ;) you'll see the file with the permissions it has

    now you do: ./my_program and it should work.


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/

      Thanks!

      Thanks for the response, everyone. However, my Terminal is still not displaying "Hello, world!" This is the code to my program:

      #!usr/bin/perl print "Hello, world!\n";

      I'm assuming this is all I need to type, right?

      I would type "my_program" or "perl my_program" after "chmod a+x my_program". The latter would cause the Terminal to respond: "my_program: Command not found." The former would just ignore it and ask for a new command without displaying "Hello, World!"

      Any thoughts?!?? Thanks.

        Try changing   #!usr/bin/perl to   #!/usr/bin/perl and see what happens then when you type ./my_program

        You're running into a set of problems that are common to first-time Unix users. Things might go quite a bit better for you if you can find someone who is familiar with Unix or OS X, and buy them a beer/coffee/whatever in exchange for an hour tour.

Re: Using Perl on OS X
by genecutl (Beadle) on Sep 13, 2002 at 19:12 UTC
    BBEdit saves files by default with Mac line endings and not Unix line endings. If that is how you saved the file, it won't run. There are two ways to change the line endings in BBEdit, one is under "Options" in the "Save/Save as" window, the other is under one of the little icons at the top of the text window. Also, you can change the default behavior in the Saving section of the Preferences dialog.
Re: Using Perl on OS X
by csotzing (Sexton) on Sep 13, 2002 at 10:42 UTC
    Also, when writing your perl scripts, you should use "#!/usr/bin/perl -w" instead of "#!/usr/bin/perl"... this will give you more warnings when you run your script, making errors easier to find. "use strict;" is also a nice thing to do, but you can read up on that one later.

    Definitely get familiar with Unix-maybe buy a book. You'll see that alot of perl's syntax and functions are similiar to what you'll find in the Unix shells.

    -C
    print(map(lc(chr),split(6,qw/99672682673683684689632658645641610607/)));