# Perl help

**URL:** <https://boards.straightdope.com/t/perl-help/823256>\
**Category:** Factual Questions\
**Created:** [October 20, 2018, 7:05pm UTC](https://boards.straightdope.com/t/perl-help/823256 "2018-10-20T19:05:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Johnny\_L.A](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/johnny_l.a/32/1084_2.png) [@Johnny\_L.A](https://boards.straightdope.com/u/Johnny_L.A)\
**Post date:** [October 20, 2018, 7:05pm UTC](https://boards.straightdope.com/t/perl-help/823256/1 "2018-10-20T19:05:14Z")

</div>

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:

```auto

#1/usr/bin/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?

---

<div class="post-metadata">

**Author:** ![markn\_1](https://avatars.discourse-cdn.com/v4/letter/m/f9ae1b/32.png) [@markn\_1](https://boards.straightdope.com/u/markn_1)\
**Post date:** [October 20, 2018, 7:22pm UTC](https://boards.straightdope.com/t/perl-help/823256/2 "2018-10-20T19:22:37Z")

</div>

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 “/”.

---

<div class="post-metadata">

**Author:** ![RaftPeople](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@RaftPeople](https://boards.straightdope.com/u/RaftPeople)\
**Post date:** [October 20, 2018, 7:29pm UTC](https://boards.straightdope.com/t/perl-help/823256/3 "2018-10-20T19:29:53Z")

</div>

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:

[http://www.compileonline.com/execute\_perl\_online.php](http://www.compileonline.com/execute_perl_online.php)

---

<div class="post-metadata">

**Author:** ![Johnny\_L.A](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/johnny_l.a/32/1084_2.png) [@Johnny\_L.A](https://boards.straightdope.com/u/Johnny_L.A)\
**Post date:** [October 20, 2018, 7:32pm UTC](https://boards.straightdope.com/t/perl-help/823256/4 "2018-10-20T19:32:35Z")

</div>

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

```auto

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.

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [October 20, 2018, 7:34pm UTC](https://boards.straightdope.com/t/perl-help/823256/5 "2018-10-20T19:34:08Z")

</div>

First, your code should look like this:

```auto

#!/usr/bin/perl
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  
./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.

---

<div class="post-metadata">

**Author:** ![Johnny\_L.A](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/johnny_l.a/32/1084_2.png) [@Johnny\_L.A](https://boards.straightdope.com/u/Johnny_L.A)\
**Post date:** [October 20, 2018, 7:34pm UTC](https://boards.straightdope.com/t/perl-help/823256/6 "2018-10-20T19:34:09Z")

</div>

> [@RaftPeople](#):
>
> 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:
> 
> [http://www.compileonline.com/execute\_perl\_online.php](http://www.compileonline.com/execute_perl_online.php)

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.

---

<div class="post-metadata">

**Author:** ![Johnny\_L.A](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/johnny_l.a/32/1084_2.png) [@Johnny\_L.A](https://boards.straightdope.com/u/Johnny_L.A)\
**Post date:** [October 20, 2018, 7:38pm UTC](https://boards.straightdope.com/t/perl-help/823256/7 "2018-10-20T19:38:59Z")

</div>

> [@friedo](#):
>
> 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.

```auto

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

```

> [@friedo](#):
>
> 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  
> ./Hello\_World

```auto

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.]$ 

```

---

<div class="post-metadata">

**Author:** ![Johnny\_L.A](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/johnny_l.a/32/1084_2.png) [@Johnny\_L.A](https://boards.straightdope.com/u/Johnny_L.A)\
**Post date:** [October 20, 2018, 7:41pm UTC](https://boards.straightdope.com/t/perl-help/823256/8 "2018-10-20T19:41:06Z")

</div>

> [@friedo](#):
>
> (Edit: looks like the CODE feature is broken. It’s not displaying line breaks.)

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

---

<div class="post-metadata">

**Author:** ![markn\_1](https://avatars.discourse-cdn.com/v4/letter/m/f9ae1b/32.png) [@markn\_1](https://boards.straightdope.com/u/markn_1)\
**Post date:** [October 20, 2018, 7:41pm UTC](https://boards.straightdope.com/t/perl-help/823256/9 "2018-10-20T19:41:39Z")

</div>

> [@Johnny\_L.A](#):
>
> ([Johnny L.A.] replaces actual name)
> 
> ```auto
> 
> 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.

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

```auto

mv Hello_World.rtf Hello_World

```

That will rename the file to Hello\_World.

---

<div class="post-metadata">

**Author:** ![markn\_1](https://avatars.discourse-cdn.com/v4/letter/m/f9ae1b/32.png) [@markn\_1](https://boards.straightdope.com/u/markn_1)\
**Post date:** [October 20, 2018, 7:44pm UTC](https://boards.straightdope.com/t/perl-help/823256/10 "2018-10-20T19:44:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Johnny\_L.A](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/johnny_l.a/32/1084_2.png) [@Johnny\_L.A](https://boards.straightdope.com/u/Johnny_L.A)\
**Post date:** [October 20, 2018, 7:49pm UTC](https://boards.straightdope.com/t/perl-help/823256/11 "2018-10-20T19:49:23Z")

</div>

> [@markn\_1](#):
>
> 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.

Thank you.

> [@markn\_1](#):
>
> 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
> 
> ```auto
> 
> mv Hello_World.rtf Hello_World
> 
> ```
> 
> That will rename the file to Hello\_World.

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

```auto

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;}'

```

---

<div class="post-metadata">

**Author:** ![Johnny\_L.A](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/johnny_l.a/32/1084_2.png) [@Johnny\_L.A](https://boards.straightdope.com/u/Johnny_L.A)\
**Post date:** [October 20, 2018, 7:51pm UTC](https://boards.straightdope.com/t/perl-help/823256/12 "2018-10-20T19:51:36Z")

</div>

Oh – What is ‘a+x’?

---

<div class="post-metadata">

**Author:** ![markn\_1](https://avatars.discourse-cdn.com/v4/letter/m/f9ae1b/32.png) [@markn\_1](https://boards.straightdope.com/u/markn_1)\
**Post date:** [October 20, 2018, 7:52pm UTC](https://boards.straightdope.com/t/perl-help/823256/13 "2018-10-20T19:52:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![markn\_1](https://avatars.discourse-cdn.com/v4/letter/m/f9ae1b/32.png) [@markn\_1](https://boards.straightdope.com/u/markn_1)\
**Post date:** [October 20, 2018, 7:54pm UTC](https://boards.straightdope.com/t/perl-help/823256/14 "2018-10-20T19:54:11Z")

</div>

> [@Johnny\_L.A](#):
>
> Oh – What is ‘a+x’?

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.

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [October 20, 2018, 7:54pm UTC](https://boards.straightdope.com/t/perl-help/823256/15 "2018-10-20T19:54:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DPRK](https://avatars.discourse-cdn.com/v4/letter/d/4491bb/32.png) [@DPRK](https://boards.straightdope.com/u/DPRK)\
**Post date:** [October 20, 2018, 7:58pm UTC](https://boards.straightdope.com/t/perl-help/823256/16 "2018-10-20T19:58:08Z")

</div>

> [@Johnny\_L.A](#):
>
> I used TextEdit to create the file. I didn’t see an option to save it as plain text.
> 
> ```auto
> 
> 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;}'
> 
> ```

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.

---

<div class="post-metadata">

**Author:** ![Johnny\_L.A](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/johnny_l.a/32/1084_2.png) [@Johnny\_L.A](https://boards.straightdope.com/u/Johnny_L.A)\
**Post date:** [October 20, 2018, 8:02pm UTC](https://boards.straightdope.com/t/perl-help/823256/17 "2018-10-20T20:02:19Z")

</div>

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

```auto

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

```

---

<div class="post-metadata">

**Author:** ![DPRK](https://avatars.discourse-cdn.com/v4/letter/d/4491bb/32.png) [@DPRK](https://boards.straightdope.com/u/DPRK)\
**Post date:** [October 20, 2018, 8:06pm UTC](https://boards.straightdope.com/t/perl-help/823256/18 "2018-10-20T20:06:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Johnny\_L.A](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/johnny_l.a/32/1084_2.png) [@Johnny\_L.A](https://boards.straightdope.com/u/Johnny_L.A)\
**Post date:** [October 20, 2018, 8:18pm UTC](https://boards.straightdope.com/t/perl-help/823256/19 "2018-10-20T20:18:24Z")

</div>

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:

```auto

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.

```

.

---

<div class="post-metadata">

**Author:** ![Dr.Strangelove](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/dr.strangelove/32/6613_2.png) [@Dr.Strangelove](https://boards.straightdope.com/u/Dr.Strangelove)\
**Post date:** [October 20, 2018, 8:39pm UTC](https://boards.straightdope.com/t/perl-help/823256/20 "2018-10-20T20:39:00Z")

</div>

> [@Johnny\_L.A](#):
>
> Unrecognized character \xE2; marked by \<– HERE after print \<– HERE near column 7 at ./Hello\_World.perl line 2.
> 
> ```auto
> 
> .
> 
> ```

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.

[Next page](https://boards.straightdope.com/t/perl-help/823256.md?page=2)
