Question:

Create Perl On A MacBook

by  |  earlier

0 LIKES UnLike

Hi, I have a Perl project to undertake, the only problem is that I'm on a Mac for the next week. I need a program or utility in which I can create and run Perl files on my MacBook (OSX 10.5.2). Thanks!

Also, if anyone knows how to edit a text file (have an example or information?) using Perl, it would be greatly appreciated! Thank you!

 Tags:

   Report

3 ANSWERS


  1. In order to run the sorts of Perl programs that you're likely to be creating when you're starting out you'll need to run the Terminal application.  It's installed by default, but it isn't be default on the dock.  You can find in Finder however under Applications.  You'll need to know the basics of how the terminal shell works -- if you save your Perl programs in your home directory however, you can easily run them by typing:

    ./your_program_name.pl

    If you get an error saying it's not executable, you'll want to make it executable first with

    chmod +x your_program_name.pl


  2. As far as I know, Perl comes preinstalled with OS X 10.5. If you haven't got it, then you can install it via Mac Ports.

    http://www.macports.org/

    You can write Perl using your usual text editor. I'm fond of TextMate.

    http://perldoc.perl.org/perlintro.html#F... describes basic file i/o with Perl.

  3. #!/usr/bin/perl

    open (FILE, ">yourfile.txt");

    print FILE "Hello world";

    close (FILE);

    Some notes:

    Use > to completely overwrite or create file, >> to append to it, < or nothing to read.

    In the example, FILE is the filehandle. You much CLOSE the filehandle or it will not flush the buffer and print.

    Usually you want to error trap. Example,

    open (FILE, ">yourfile.txt") or die ("Cannot open file $!");

    The $! holds system messages, in this example if you cannot write to the directory you are trying to write to, the result would be

    Cannot open file permission denied

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.