Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

The basics

by root (Monk)
on Oct 29, 1999 at 01:12 UTC ( [id://860]=perltutorial: print w/replies, xml ) Need Help??

So you want to learn Perl eh??? If you're unsure of that much you might want to check What is Perl?

The first thing you might be asking is how do you run a Perl program? The way to do it on most systems is by simply typing:

perl program.pl

This will work so long as perl is on your system and your PATH points to where it is stored. If you're on a Unix or This will make your computer read this as a series of shell commands stored in a file. However this isn't quite what we want. We want it to first run the perl interpreter and then have that process and run the rest of the program.Linux system another more common way of running your program is to make it executable. To do this you type something like

chmod +x programfile.pl

To do this we have to add one line at the beginning of the program file. Most of the time that looks something like this

#!/usr/bin/perl

If this doesn't work your perl interpreter may be in another location to find out where that might be you could type

where perl
to find where it's stored if this yields something like /usr/contrib/bin/perl simply change that first line to #!/usr/contrib/bin/perl and you should be golden.

To make sure all that is working we may as well write up the obligatory "Hello World" program to test it out. How about something like:
#!/usr/bin/perl print "Hello World!\n";

Now save that in a file like helloworld.pl do a chmod +x helloworld.pl and then try running it by just typing helloworld.pl at your command prompt. With any luck it should print out:

Hello World!
Now you should check out the basic datatypes, three

Replies are listed 'Best First'.
Re: The basics
by Anonymous Monk on Jun 24, 2006 at 18:47 UTC
    what is the use of #!/usr/bin/perl other than to interpreter?
      The #! line has two programs that "interpret" it:
      • To the shell. An executable file that starts with a #! line will cause the executing shell to start up the program indicated in that line. That way, you can use #!/bin/sh to create a shell script, #!/usr/bin/perl to create a perl script etc. At least on unixish systems, there is always a shell involved in this process (but for instance windows doesn't work like that)

      • (Possibly) to the interpreter indicated in the #! line.

        For instance, perl reads the additional arguments on the #! line as a sort of "setup parameters". Example: #!/usr/bin/perl -wT will run perl with global warnings enabled and in taint mode.

        To the shell. [...] At least on unixish systems, there is always a shell involved in this process

        The #! line, also called shebang, tells nothing to any shell. It's the kernel who interprets it. When the first two bytes of an executable file are #!, the kernel runs the command that follows them, with the parameters specified, and feeds it the whole file on its standard input:

        $ cat > a #!/usr/bin/tail -n2 1 2 3 4 5 6 7 ^D $ chmod +x a $ ./a 6 7 $ strace ./a 2>&1 | grep -c sh 0

        --
        David Serrano

Re: The basics
by Anonymous Monk on Oct 10, 2008 at 10:18 UTC
    can use #!/usr/binsh and #!/usr/bin/perl at the same time in the same file directly?
      Note:

      where perl

      Should instead read

      which perl

      Then enter the reported directory at the #! interpreter line.

        One could use #!/usr/bin/env perl?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perltutorial [id://860]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-28 19:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found