http://www.perlmonks.org?node_id=1015225

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

I am just beginning to learn Perl so I can handle the forms on my website. The very first thing is to do the "Hello,World" code. I typed and saved the code as a ".pl" file. But when I try to get it to run, I get this long string of mistakes. I am using an iMac OS 10.6.8, which is running perl, v5.10.0 built for darwin-thread-multi-2level. Here's my code:


#!/usr/bin/perl
use strict;
use warning;

print "Hello, World!\n";


Here's my error list:


Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 1, near "rtf1\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 1, near "ansi\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 1, near "ansicpg1252\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 1, near "cocoartf1038\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 2, near "fonttbl\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 2, near "f0\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 2, near "fswiss\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 3, near "red255\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 3, near "green255\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 4, near "margl1440\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 4, near "margr1440\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 4, near "vieww9000\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 4, near "viewh8400\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 5, near "viewkind0 \" (Missing semicolon on previous line?) Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 5, near "pard\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 5, near "tx720\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 5, near "tx1440\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 5, near "tx2160\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 5, near "tx2880\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 5, near "tx3600\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 5, near "tx4320\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 5, near "tx5040\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 5, near "tx5760\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 5, near "tx6480\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 5, near "tx7200\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 5, near "tx7920\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 5, near "tx8640\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 5, near "ql\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 5, near "qnatural\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 7, near "pardirnatural \" (Missing semicolon on previous line?) Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 7, near "f0\" Backslash found where operator expected at /Users/patnonadmin/Desktop/hello#1.pl line 7, near "fs24 \" (Do you need to predeclare fs24?) syntax error at /Users/patnonadmin/Desktop/hello#1.pl line 1, near "rtf1\" syntax error at /Users/patnonadmin/Desktop/hello#1.pl line 3, near "red255\" syntax error at /Users/patnonadmin/Desktop/hello#1.pl line 4, near "margl1440\" "use" not allowed in expression at /Users/patnonadmin/Desktop/hello#1.pl line 8, at end of line "use" not allowed in expression at /Users/patnonadmin/Desktop/hello#1.pl line 9, at end of line syntax error at /Users/patnonadmin/Desktop/hello#1.pl line 11, near ";}" Execution of /Users/patnonadmin/Desktop/hello#1.pl aborted due to compilation errors.


Why is there such a long list? I'm assuming that the Terminal is running "macperl" but it doesn't say so.


How do I get past this mess?


Thanks.

Replies are listed 'Best First'.
Re: Beginning Problems
by aitap (Curate) on Jan 24, 2013 at 20:15 UTC

    I typed and saved the code as a ".pl" file.
    It looks like you saved the file using a word processor as formatted text (RTF), while Perl programs should be written as plain text (*.txt) files.

    Do you see your code when you enter `cat /Users/patnonadmin/Desktop/hello#1.pl` (without quotes) in the terminal, or do you see something else?

    Sorry if my advice was wrong.
Re: Beginning Problems
by mhearse (Chaplain) on Jan 24, 2013 at 20:27 UTC
    If you are writing perl scripts on Mac OS X, I recommend the following:

    1. Go to Applications => Utilities => Terminal
    2. Adjust the window and text size to your liking
    3. Use nano to edit your files: nano hello.pl
    4. After saving your changes, check the syntax: shell> perl -c ./hello.pl
    5. Feed your script to the perl interpreter (it is hungry): perl ./hello.pl

      I hope this was meant as a joke!

      I mean, there's nothing wrong with nano if all you have is a terminal connection somewhere, but suggesting this to someone who's apparently new to programming and (to some extent) computers in general? Someone, I bet, used to GUIs?

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.

        I take your point. Maybe this was a poor suggestion. I tend to see things through my own eyes when I began(1996 using the FreeBSD easy editor). But that doesn't mean everyone else has to suffer.

        Most of the GUI text editors make it difficult to save in a plain-ascii plain-text format. They tend to use fancy file formats, save as UTF-16, insert byte-order-marks, or have odd newlines. Dropping down to a terminal-based text editor may be easier than teaching someone how to find the options to turn each of these off in a program you haven't used previously.

        (And anyway, nano sucks. vim is better. And probably easier to learn, too.)

Re: Beginning Problems
by Kenosis (Priest) on Jan 24, 2013 at 20:16 UTC

    Is your five-line script is named hello#1.pl located on your Desktop? One error message mentions line 11, near ";}" but you have no line 11 nor a "}" in the script you've shared. It actually looks like you're trying to execute an rtf file as the errors you've shown appear to contain rtf control information.

    Update: Consider trying TextWrangler as an editor. Even better, try Eclipse (Eclipse Classic 4.2.1, as of this write) with the EPIC Perl IDE installed.

Re: Beginning Problems
by blue_cowdawg (Monsignor) on Jan 24, 2013 at 21:16 UTC

    Don't use a word processor to edit Perl code. Looks to me like .pl file was treated like rich text when it was saved to disk.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Beginning Problems
by rnaeye (Friar) on Jan 25, 2013 at 03:58 UTC
    If you are using TextEdit, click on Preferences and choose "Plain text" option. Alternatively, there is a nice free text editor called TextWrangler for Mac at http://www.barebones.com/products/textwrangler/