Perl help

Edit the file and don’t use “” to delimit strings, use “”

Does TextEdit change the extension upon saving, even after opening an existing file already called “Hello_World.perl” ? I know .text is the default extension for text files, but does it tack it onto a file that already has a different extension?

Download and use BBEdit.

seconding the advice to stick with ASCII unless necessary. Watch out for “smart quotes” in TextEdit! Indeed, it may be worth it to spend a few seconds to download BBEdit and use that instead.

OK, I logged onto VPN and connected to my work PC via Microsoft Remote Desktop so I could use Notepad, then copied and pasted it into Hello_World.perl:


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

The results:


Johns-MacBook-Pro:Perl [Johnny L.A.]$ chmod a+x Hello_World.perl
Johns-MacBook-Pro:Perl [Johnny L.A.]$ ./Hello_World.perl
-bash: ./Hello_World.perl: /usr/bin/perl^M: bad interpreter: No such file or directory
Johns-MacBook-Pro:Perl [Johnny L.A.]$ ls -al
total 16
drwxr-xr-x   4 [Johnny L.A.]  staff  136 20 Oct 13:46 .
drwx------+ 25 [Johnny L.A.]  staff  850 20 Oct 13:14 ..
-rwxr-xr-x@  1 [Johnny L.A.]  staff   41 20 Oct 13:46 Hello_World.perl
-rwxr-xr-x@  1 [Johnny L.A.]  staff   44 20 Oct 12:55 Hello_World.txt
Johns-MacBook-Pro:Perl [Johnny L.A.]$ 

TextEdit saves with a .txt extension, but I used ‘mv’ to get rid of it. It now shows up as an executable file.

That’s only good for 30 days. :frowning:

No, it just reverts to “newbie” mode, which is good enough.

Try VSCode. Totally free and handles Perl and others.

The “bad interpreter” error is because your file has CR/LF endings. You need to use just LF. I don’t know how to do it in TextEdit. If it were me I would do this, but there’s probably a simpler way by setting options in TextEdit.



tr -d '\r' < Hello_World > Hello_World.new
mv Hello_World.new Hello_World


OK, I downloaded BBEdit, copied the code, and saved it as Hello_World.pl. And…


Johns-MacBook-Pro:Perl [Johnny L.A.]$ ./Hello_World.pl
Hello, world!


Success! :slight_smile:

So to recap:
[ul][li]Make sure the program is in plain text;[/li][li]Make sure it has a .pl extension;[/li][li]Open Terminal;[/li][li]Change the directory;[/li][li]Enter chmod a+x program_name.pl (though I’m not sure what this does);[/li][li]Enter ./program_name.pl to run[/ul][/li]
I think that’s enough for one day. I’ll be back again as I read more of the book.

Thanks!

The chmod makes the program executable.
If you don’t do that, OS X will refuse to run it, assuming it’s just a piece of text (which it is). Also, it’s a security risk to to execute any old piece of text (say, one that someone might have dropped onto your computer).

The Terminal has some very handy short cuts. It you type ./H and then hit the tab key, the terminal will autofill the rest of the program name. If there is more that one file that starts with an H, it will list them.

You can also use the up-arrow key to cycle through the most recent commands - saves a lot of typing.

Read #14 again.

BTW, the chmod isn’t strictly necessary. If you don’t do the chmod you can still run your program by typing


perl Hello_World.pl

instead of


./Hello_World.pl

You can call it whatever you want; .pl or .perl is for your own convenience, making it obvious it’s a Perl script.

That basically just lets you run the program as “./Hello_World.pl” instead of “perl ./Hello_World.pl”

You’re using a Macbook Pro, so that’s probably MacOS 10. Also, you admit you know little about U**X, which is pretty effectively what you’re dealing with on MacOS 10. I’m assuming you’re using “Terminal” for your command line interface, but there are other options that work mostly the same.

Just try this from the command line prompt (which should look something like "Johns-MacBook-Pro:Perl [Johnny L.A.]$ "):

perl -e “print "Hello, World!
";”

The “/usr/bin/perl” should be unnecessary because “/usr/bin/” should already be part of your standard executable search path.

The “-e” is necessary because it’s a signal to the perl interpreter that everything to come until a semicolon is a perl command to be executed.

The "" characters are necessary to “escape” each of the following (single) characters from interpretation by the shell (bash) and ensure they reach the perl interpreter uninterpreted (but with the "" characters stripped).

If you want to put the script into a file, use this text in that file:
#!/usr/bin/perl
print "Hello, World!
";

Of course, you then have to use chmod +x {filename} from the command line to allow the shell to execute the contents.

A warning about TextEdit, by the way. On my Mac, TextEdit does not allow one to save a file as actual text only! “Rich Text” is the default, but others are available, none of them pure 7-bit ASCII. That sucks, IMO, because no interpreter (perl, python, ruby, bash, etc.) is going to handle the encoding properly. If you don’t have time to learn “vi”, there are other editors available at the App Store.

“vi” comes native with MacOS, and it’s really not that difficult to learn. There are lots of tutorials for it online. I think this one is pretty good for a start: https://www.tutorialspoint.com/unix/unix-vi-editor.htm

Good luck!

For the love of God will nobody give this man the advice he needs, which is to step back from Perl and invest a couple of hours in learning about the command shell and the filesystem.

For some reason you want to use Perl, which I feel like we’ve already discussed is not ideal, but I won’t try to dissuade you. But the problems you’re experiencing are nothing to do with Perl, it’s about creating files on your computer and learning to run them.

Spend just a little time learning to run and write shell scripts, then you will be in better shape to process advice about Perl.

To elaborate, I see OP is asking for spoon feeding, and I don’t judge because I have totally been there. Right now you seem to be struggling with how to write a program and run it, which is independent from Perl. I think the way to address that problem is to invest a couple of hours learning about how to write, save, and execute shell scripts. Do whatever you want, but please don’t confuse yourself by framing these things as the same problem.

You mean like back in post #5 where somebody said

?

Ouch, don’t confuse him like that! He has no need to learn how to run Perl snippets from the command line, and certainly doesn’t need to dive into the intricacies of shell metacharacter quoting.

No, that’s wrong. Shebang lines do not use PATH. You have to specify the full path of the interpreter.

As I said at the outset, this is all new to me. Writing, saving and executing is what I’m trying to learn.

And yes, I’m asking for spoon-feeding. My ‘learning style’ is interactive; i.e., in a classroom setting where questions can be asked and answered, and where someone else’s question might be one you didn’t know you had. Since a classroom isn’t an option, the nearest I can come is to ask my questions here.

UNIX Tutorial for Beginners

It looks new and different, but even people at Cal in the ‘Seventies could figure it out, and you can only guess at what kind of drugs they were on. Learn and use the pico editor for basic test processing first, the you can upgrade to vim or Emacs or select a graphical editor. There is no good reason to use TextEdit which really doesn’t support coding with any formatting features and will cause you to develop terrible habits like putting tabs instead of spaces in indentures.

Stranger

If you got the editor working and were able to type in, save, and run a program, then you must be doing something right; that is basic writing, saving, and executing. All you have to do now is get your scripts to do something interesting. In the last thread, you indicated that you ordered a Perl book which never arrived; I hope that got sorted? (See here; some of the books are also available for free online). Any decent textbook will have problems and exercises at the end of each chapter, which should get you up to speed with basic concepts.

Agree with DPRK. Based on #28 he can edit and save a program with BBEdit and now knows how to run it. Undoubtedly he will need to learn more about the shell as time goes on, but right now his goal is to learn Perl and as far as I can see he has everything he needs to start working through the programs in the book.