Perl help

Yes, I know there are websites; but I’ll need feedback and I know there are a lot of experts here.

I know how to write programs using Easytrieve. I want to learn Perl so that I can convert Easytrieve into something free and modern. I have no unix experience, and no experience with anything that looks remotely like Perl. I would like to post questions, receive answers, and then go from there.

I have written this from Learning Perl and saved it to Desktop => Perl:

print “Hello, world!

I opened a Terminal on my Mac and typed Hello_World. It says ‘-bash: Hello_World: command not found’. I received the same error when I typed chmod a+x Hello_World. The book says, 'Now you're ready to run it:', so I typed /Hello_World into the terminal. It said, ‘-bash: $: command not found’.

It’s pretty obvious I have no idea what I’m doing; but it seems obvious to me that I’m not telling the terminal where ‘Hello_World’ lives. So to start off this thread…

Having written the first program, how do I run it?

  1. You don’t type the "". The book is showing you that that's the prompt you see, and you type what comes after the . (It’s possible but not likely you’re seeing a different prompt than the ; that doesn't matter, just type what comes after the .)

  2. You first need to make sure you’re in the Desktop folder since that’s where you saved the file. Type “cd Desktop” and ENTER. If that doesn’t work, type “pwd” to see which folder you are in.

  3. You have an extraneous “1” in the first line after the hash. Remove that.

  4. After you do the chmod, you should type the Hello_World command with the two characters “./” in front of it. It looks like you just used “/”.

Sometimes when learning it’s nice to just code and execute in an online system, here’s one, click execute and you’ll see the results on the right. Change the code, add some lines, hit execute again:

([Johnny L.A.] replaces actual name)

Johns-MacBook-Pro:~ [Johnny L.A.]$ cd Desktop/Perl
Johns-MacBook-Pro:Perl [Johnny L.A.]$ chmod a+x Hello_World
chmod: Hello_World: No such file or directory

So I changed the directory to Desktop and the Perl folder. Unless I didn’t. As I said, I have zero experience here.

The extraneous ‘1’ should have been a ‘!’. Apparently I missed the Shift. That’s been corrected and saved.

First, your code should look like this:

print "Hello, world!

(Edit: looks like the CODE feature is broken. It’s not displaying line breaks.)

Note that you have a 1 instead of a ! and the shebang line (#! thing) must be on a line by itself.

But what you really need is Unix shell help.

I assume when you say that you saved the file to “Desltop => Perl” you mean that you saved it inside a folder called Perl in your desktop, and the actual file name is Hello_World.

(It’s not strictly necessary, but it’s conventional for Perl programs to use the extenstion .pl, so you may want to do that.)

In that case, you need to navigate to the correct directory first, by using cd Desktop/Perl

Then use ls -al to list all the files in that folder. You should see your Hello_World file there along with some other stuff.

Make sure you are in the right place before trying to execute commands. To make the file executable (once you’re in the right place) you can then do

chmod a+x Hello_World

The dot symbol references the current working directory (where you are) and is necessary because modern OSes usually don’t put the current working directory in your executable search path by default.

Thank you. Since I copied the code out of the book, I assume it works. What I need to know is how to execute it on my computer.

Johns-MacBook-Pro:Perl [Johnny L.A.]$ ls -al
total 8
drwxr-xr-x   3 [Johnny L.A.]  staff  102 20 Oct 12:25 .
drwx------+ 25 [Johnny L.A.]  staff  850 20 Oct 12:07 ..
-rw-r--r--@  1 [Johnny L.A.]  staff  394 20 Oct 12:25 Hello_World.rtf

Johns-MacBook-Pro:Perl [Johnny L.A.]$ chmod a+x Hello_World
chmod: Hello_World: No such file or directory
Johns-MacBook-Pro:Perl [Johnny L.A.]$ ./Hello_World
-bash: ./Hello_World: No such file or directory
Johns-MacBook-Pro:Perl [Johnny L.A.]$ 

CODE doesn’t work in Sultan. It does in Straight Dope v3.7.3.

Yes, you successfully changed to the Desktop/Perl directory. You can tell that because (1) the “cd” command didn’t print an error , and (2) your prompt now includes the word “Perl”. Your directory appears after the colon in the prompt.

The ls command shows that you somehow saved the file as Hello_World.rtf rather than just Hello_World. What program did you use to edit the code? Anyway, the simplest fix at this point it type this at the prompt

mv Hello_World.rtf Hello_World

That will rename the file to Hello_World.

Wait, actually it might be worse that that. If that’s really an rtf file just renaming it won’t work. You need to save the file as PLAIN TEXT when you save it.

Thank you.

I used TextEdit to create the file. I didn’t see an option to save it as plain text.

Johns-MacBook-Pro:Perl [Johnny L.A.]$ mv Hello_World.rtf Hello_World
Johns-MacBook-Pro:Perl [Johnny L.A.]$ chmod a+x Hello_World
Johns-MacBook-Pro:Perl [Johnny L.A.]$ ./Hello_World
./Hello_World: line 1: {rtf1ansiansicpg1252cocoartf1404cocoasubrtf470: command not found
./Hello_World: line 2: syntax error near unexpected token `}'
./Hello_World: line 2: `{\fonttbl\f0\fswiss\fcharset0 Helvetica;}'

Oh – What is ‘a+x’?

Yeah, your file is rtf not text. You need to figure out how to save it as text. In TextEdit I think you do Format->Make Plain Text, which is a pretty weird way to do it.

The chmod command changes the “mode” of the file. The details are complicated and probably more than you really want to know right now, but “a+x” changes the file to be “executable” by everyone. That means you can run it as a program. Without that, it’s considered a data file and the system won’t let you execute it.

When you do “ls -l”, you’ll see 10 characters on the left that represent the file mode. If every third character is an “x” then it’s executable. If they are dashes then it’s not executable.

You should really be working with a real programming text editor, but for now you can use TextEdit by going to the Format menu and selecting Make Plain Text. Then save your file again.

That is definitely the problem. See if TextEdit has an option to edit plain text under the “Format” menu (“make plain text” or something similar).

From the shell in Terminal you can type “cat ./Hello_World” to display your file; then you can see if the first line is the correct #!/usr/bin/perl or the rtf1ansi… stuff which should not be there.

OK, I converted to text. When I click on the file in the folder, it says it’s text. So…

Johns-MacBook-Pro:~ [Johnny L.A.]$ cd Desktop/Perl
Johns-MacBook-Pro:Perl [Johnny L.A.]$  chmod a+x Hello_World
chmod: Hello_World: No such file or directory
Johns-MacBook-Pro:Perl [Johnny L.A.]$ ./Hello_World
-bash: ./Hello_World: No such file or directory
Johns-MacBook-Pro:Perl [Johnny L.A.]$ cat ./Hello_World
cat: ./Hello_World: No such file or directory

TextEdit may have changed the extension so it’s now called Hello_World.text or something. Run “ls -la” as described above to see what’s actually there. (Or open the folder in the Finder.) You could also rename it to Hello_World.perl instead of Hello_World, just to keep things clear.

I tried saving it as Hello_World.perl, but it saves it as Hello_World.perl.txt. I don’t know how to get rid of the .txt.

EDIT: Tried this:

Johns-MacBook-Pro:Perl [Johnny L.A.]$ mv Hello_World.perl.txt Hello_World.perl
Johns-MacBook-Pro:Perl [Johnny L.A.]$ chmod a+x Hello_World.perl
Johns-MacBook-Pro:Perl [Johnny L.A.]$ ./Hello_World.perl
Unrecognized character \xE2; marked by <-- HERE after print <-- HERE near column 7 at ./Hello_World.perl line 2.


Your “curly quotes” are causing problems. You need the character ", not “ or ”. Not sure how to do it in your editor, but see if it has an ASCII mode.