Help for Perl Needed

Discussion in 'Mac Help/Tips' started by macktheknife, Sep 12, 2002.

  1. macktheknife macrumors 6502a

    macktheknife

    Joined:
    Jan 24, 2002
    #1
    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. 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 Perl in Terminal and it gave me this message:

    "Bareword found where operator expected at - line 1, near "$ chmod a"
    (Missing operator before a?)"

    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.
     
  2. macktheknife thread starter macrumors 6502a

    macktheknife

    Joined:
    Jan 24, 2002
    #2
    Well, I had tried typing in "chmod 755 my_program" in Terminal but I get no response. In fact, it seems like it ignored it without by returning me to the command prompt. Can someone give me an idea what I'm doing wrong?

    Thanks!
     
  3. szark macrumors 68030

    szark

    Joined:
    May 14, 2002
    Location:
    Arid-Zone-A
    #3
    Re: Help for Perl Needed

    Code:
    #!/usr/bin/perl
    print "Hello, world!\n";
    
    These two lines should be saved into a file called "my_program" (for this example).

    When you open Terminal, you start in your home directory. Either move the file to your home directory, or enter the following at the Terminal prompt to change directory into your Desktop folder:

    Code:
    cd Desktop
    Typing the next line at the terminal prompt:

    Code:
    chmod a+x my_program
    changes the permission modes so the file you just created (my_program) so that all users can execute the program.

    Finally typing:

    Code:
    ./my_program
    at the terminal prompt will run your new Perl script!
     
  4. macktheknife thread starter macrumors 6502a

    macktheknife

    Joined:
    Jan 24, 2002
    #4
    Thanks, szark, but Terminal keeps saying: "./my_program: Command not found." I made sure that I was in the Desktop directory, and I had entered the "chmod a+x my_program" line. I double-checked my syntax to make sure that I entered the text correctly. I even made sure there were no extensions on my text file.

    Can you or anyone try this on their Terminal? Am I doing something wrong or do I need to change some setting? Thanks.
     
  5. szark macrumors 68030

    szark

    Joined:
    May 14, 2002
    Location:
    Arid-Zone-A
    #5
    Actually, I did try it as I was typing it.

    The file "my_program" should contain only those two lines I listed.

    Also, make sure you saved it as a text file. Type the following in the Terminal:

    Code:
    more my_program
    It should match what you typed in your file exactly.
     
  6. macktheknife thread starter macrumors 6502a

    macktheknife

    Joined:
    Jan 24, 2002
    #6
    It worked!

    It worked!!! But here's the funny thing: I had orginally inserted the code in BBEdit. But it didn't work. I took it out, pasted it into Text Edit, typed chmod again and got a big error. Text Edit apparently inserted a bunch of formatting lines. Then I cleaned it up and ran it without any problems.

    I then wondered why it wouldn't run if I had it as a BBEdit file. I played around and figured out that if I saved my file with UNIX line breaks, it would work.

    So all this time I had done the right thing except on how I saved it. :rolleyes:

    Thanks again!
     
  7. szark macrumors 68030

    szark

    Joined:
    May 14, 2002
    Location:
    Arid-Zone-A
    #7
    TextEdit saves in Rich Text Format (RTF) by default, hence the formatting. It can be changed in Preferences.

    Good luck with Perl! :D
     
  8. Choppaface macrumors 65816

    Joined:
    Jan 22, 2002
    Location:
    SFBA
    #8
    ya i really think that bbedit should have unix line breaks on be default in the prefs....

    also make sure bbedit isn't saving any of the MPW shell info or whatever its called
     
  9. macktheknife thread starter macrumors 6502a

    macktheknife

    Joined:
    Jan 24, 2002
    #9
    Regular Expression Help

    Can anyone explain to me how the binding operator (=~) works? I'm working through "Learning Perl", and I'm having trouble comprehending Schwartz's explanation. Any advice or assistance would be greatly appreciated. Thanks!
     

Share This Page