views:

126

answers:

5

I know to simply run perl in linux you do perl program.pl args . How do I make convert that for a makefile?

CC=perl
CFLAGS=-O0
TARGET=./problem1
OUTFILE=./log.txt

$(TARGET): problem1.o 
    $(CC) $(CFLAGS) -o $(TARGET) problem1.o
problem1.o: problem1.cpp
    $(CC) $(CFLAGS) -c problem1.perl
clean:
    rm -f *.o $(TARGET) $(OUTFILE)
run: $(TARGET)
    $(TARGET) <$(INFILE) >$(OUTFILE)

I have this, but it really wrong in a way. I have never touched perl before in my life... any help? thanks

My error message is : make[1]: * No rule to make target problem1.cpp', needed byproblem1.o'. Stop. So its confuseing it with C++

+2  A: 

You do not need a Makefile to run a perl program. Just say perl progname, as you have said.

Perl is an interpreted language, so it just runs on the Perl interpreter.

rmk
Yeah i know. I stated it above. My professor wants us to use a make file. I only made a makefile for a cpp file. So im kinda lost. Any help or tutorials will be welcomed. Thanks
+2  A: 

how about something like:

project: project.pl
    echo "#! /usr/bin/env perl" > project
    cat project.pl >> project
    chmod a+x project
TokenMacGuy
It says now Makefile:2: *** missing separator. Stop.I just need to run problem1.pl and create outfile.txt as well in the makefile...
makefiles must have tabs, but that's kind of hard to type on stack overflow. Make sure the indent is with a single tab
TokenMacGuy
@Token: Makefile fragments are a pain on SO for that reason; however, you *can* write the code in an external editor and paste in, then use the code button. Or at least, *I* can using carbon emacs and FF on Mac OS X. Not that I'd have bothered for this little bit here.
dmckee
edit: well, there's tabs in the *source*, but if you try to copy and paste from the rendered view, you still get spaces
TokenMacGuy
+1  A: 

To run your program, just put the call to the Perl program on the command line after your target, like so:

target:
        /path/to/program.pl

Or, if you need to call perl explicitly:

target:
        /path/to/perl /path/to/program.pl

Make sure to get the tabs right. (You need them before the calls to run program.pl.)

GreenMatt
+5  A: 

Does your professor literally want a makefile for use with make(1), or just the equivalent for Perl?

If the latter, the standard in Perl is to us Module::Build. A basic Build.PL looks like this:

use Module::Build;

my $build = Module::Build->new (
    module_name     => 'Foobar',
    license         => 'perl',
    dist_author     => 'Drew Stephens <[email protected]>',
    dist_version    => '0.1',
    dist_abstract   => 'Make widgets',
    build_requires  => {
        #'dependent::module'     => 'version_num',
    }
);

$build->create_build_script;
Drew Stephens
+1  A: 

If the Perl script is a standalone program, rather than a module which will be used by other Perl scripts, then you will probably end up copying (installing) the file in a bin directory, likely your own ${HOME}/bin directory. At that point, you don't want to have to type perl $HOME/bin/script.pl; you want to type just script. So, you probably 'compile' the Perl script by copying it to the file with the basename of the script.

In terms of make, you have to educate it on how to convert script.pl to script. In the simplest case, all that takes is:

CP = cp
MX = chmod +x

.SUFFIXES: .pl

.pl:
    ${CP} $< $@
    ${MX} $@

SCRIPT = script

all: ${SCRIPT}

The result can be installed by another copy, or a move. As long as the script starts with a shebang that identifies a working Perl interpreter (so #!/bin/perl or #!/usr/bin/perl), then you don't have to worry any more. One trick to get the script to use the Perl found on your PATH is:

#!/usr/bin/env perl

The env command sets environment variables (not used here) and executes the program named afterwards (with arguments, in the general case; in a shebang line, you are limited to one argument, which is 'perl' here). Using this saves you having to edit your scripts.

Note that this does not ensure that prerequisite modules are installed, whereas the Build module does do that. For details, see the answer describing Build. However, if your script only uses standard (Core) modules, you don't have to fret about that. Since you can't pass the '-w' option via the shebang any more, it is normally a very good idea to include the following lines at the top of your script:

use strict;
use warnings;

If you need to set the correct Perl in the shebang, then you need a program to fix the interpreter. The first edition of the Camel book included a script, fixin, which can do that job.

Jonathan Leffler