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

Dear Monks,
I am trying to learn perl, using a mac (osx 10.6.6).
In Terminal, when I input:
#!/usr/bin/perl
print "Hello, world!\n";
I receive:
-bash: !\n": event not found
If I just input:
print "Hello, world";
I receive:
-bash: print: command not found
I think I have the right path to perl because when I input:
which perl
I get:
/usr/bin/perl
and I when I type:
perl -v
I get:
This is perl, v5.10.0 etc.....
Any idea what I'm doing wrong????
Thanks,

Replies are listed 'Best First'.
Re: Falling at the first hurdle
by Corion (Patriarch) on Feb 19, 2011 at 18:12 UTC

    You need to put your Perl script into a text file, and not type it immediately into the shell (the "terminal" you see).

    To run your very first Perl program immediately, use this in the shell:

    perl -we 'print "Hello World!"'

    but for longer scripts, create them using a text editor like TextPad or TextMate or in my example vi:

    > vi myscript.pl ... enter the script > ls -l myscript.pl -rw-r--r-- 1 corion corion 96 19. Feb 18:09 myscript.pl > perl -w myscript.pl ... output of the script

    vi is a versatile text editor, and it likely already is installed on your Mac, but it is not very friendly for a beginner just starting out. Most likely there is a friendly text editor using the OSX GUI included with OSX, but I don't know it. vi has the huge advantage of being available almost everywhere , which is why it helps to have knowledge about it in your toolbox. The two most important things to know about it are the command :q! which quits vi without saving, and :wq, which saves and then quits it.

      Thanks!!!!
Re: Falling at the first hurdle
by moritz (Cardinal) on Feb 19, 2011 at 18:11 UTC
    Don't type Perl code at the prompt of your shell. Rather write it into a text file, let's say greeting.pl, and then run perl greeting.pl to run your code.

    Remember to save the file before running whenever you modify it.

      Thank you!!!
Re: Falling at the first hurdle
by luis.roca (Deacon) on Feb 19, 2011 at 18:46 UTC

    First, points for trying to write your first Perl program on the command line (Terminal). Since you're already there here are a few commands to begin reading the documentation:

    • perldoc perltoc (Perl Documentation Table of Contents)
    • perldoc perlintro (Introduction to Perl)

    In addition to the good advice already provided to you, in terms of using a "Programmer's" text editor, there are some nice choices on the Mac. You can try out Vi right away as Corion suggested. It's a little strange at first but a perfect way to start is going back to the Terminal and enter vimtutor. You'll then be prompted to hit enter/return. This interactive tutorial takes about half an hour and is REALLY worth the time. You can also download a GUI version of Vim for the Mac called MacVim and have access to some helpful plugins for programming Perl.

    There is also BBEdit, which I've used for a while and really like. It's pricey (especially compared to Vi ($99.99 US) but there's a free trial as well as a basic version called Text Wrangler.

    Good luck and have fun.

    "...the adversities born of well-placed thoughts should be considered mercies rather than misfortunes." — Don Quixote

      If you’re on a Mac and willing to pay, the right thing to buy is TextMate. Single license is $55 US right now and it’s a very deep, scriptable, pluggable editor for code and templates.

        Oh I've used TextMate. (Although not since it was more around $40) It's well designed, easy to jump into and grow with. I just like BBEdit more. But like I said - the Mac has nice choices :)


        "...the adversities born of well-placed thoughts should be considered mercies rather than misfortunes." — Don Quixote
Re: Falling at the first hurdle
by 7stud (Deacon) on Feb 19, 2011 at 20:46 UTC

    For a good text editor, try vim. Type:

    $ vimtutor

    to start an interactive lesson on how to use vim. Once you get the basics down, there are lots of features you can enable.