Questions about this topic? Sign up to ask in the talk tab.

Difference between revisions of "Perl/Basics/Your First Program"

From NetSec
Jump to: navigation, search
(Created page with "===Code=== {{:Perl/Basics/Your First Program/Code}} ===Analysis=== {{:Perl/Basics/Your First Program/Analyzing Your First Program}}")
 
(No difference)

Latest revision as of 00:12, 19 July 2012

Code

To run this code, you'll only need to put it in a text file. Save it as "hello.pl", and then you can execute the following to run it from either cygwin or bash:

  • chmod +x hello.pl
  • ./hello.pl

Alternatively you can simply type:

  • perl hello.pl

<syntaxhighlight lang="perl">#!/usr/bin/perl use strict; use warnings; print "Hello world!\n";</syntaxhighlight>

Analysis

The shebang declares the location of the code's interpreter. I.e. if you're writing bash, you'll need to put:

 #!/bin/bash

at the top of your file. In perl, it's typically:

 #!/usr/bin/perl

This should be the first line in any perl you write. You can also use:

 #!env perl

If you are unsure of the path and you have it in your environment variables. If for some reason `#!env perl' and `#!/usr/bin/perl' do not work, running `which perl' from the bash command line will return the proper path.

This is only required if you want to directly execute your script (i.e. ./script.pl). If you get permissions errors when attempting this, you can execute it via `perl script.pl' or running `chmod +x script.pl' before running `./script.pl'.

With perl in particular, its real easy for ugliness to occur. To counter this, the next lines are:

<syntaxhighlight lang="perl">use strict; use warnings;</syntaxhighlight>

Strict perl forces you to maintain some semblence of syntax. Without the strict usage, you can basically run amok with code, perl will not care.

The print "Hello world!\n" line simply prints "Hello world!" with a newline character on the end. On windows you may need to change "\n" to "\r\n", depending on which interpreter you've installed.

You can also reference the hex code for this via a \x character, "\x0a\x0d".