Asson Computer Services Limited (Call for Availabilty)
HomeServices offered/ skill setCustomer baseCompany Profile/ Curriculum VitaeContact usFirst Aid InstructionBasic Perl help
Basic Perl help

Any comments, please send to Gary@AAnimated Mailbox Flag Up and Downsson.co.uk

Under Construction Perl CGI

                                                           Perl 

Some Perl that I find useful. Nothing fancy, just the basics.
I find an example that proves how a structure or command works is far better in the first instance than a syntax diagram. Once you understand the basics, the full syntax becomes useful.

First line, make sure this works from the command line, then {Ctrl}C to exit.
If /usr/bin/perl fails, try /usr/local/bin/perl, after that you are left with trying to find it:           
 
Find / -name perl –print
 

After that, you will need to download it http://www.perl.com/download.csp  

#! /usr/bin/perl –w                    # w = extra warnings

 

$ = variable
$Item = DOG

$NUmber = 10

@ = array       

@words = (“This”,”That”,”The other”);
@words = qw(This that );   # quotes “ are treated as strings @words is This That

                        $words[0] is This

 

$count = 1;

$count = $count++;

$count++

$count = $count * 2;

$count *= 2

Numeric 

< < = == >= > !=

2 > 3   

3 == 3

2 >= 1

|| OR && AND

 Strings

eq ne

 
Hash 

%number = qw(

One      1

Two     2

Three   3

};

$numbers{one} gives 1

 
if ( $a =~ /^$String/i ) {
        print "Yes $String is in $a\n";
}
else {
        print "$String is not in         $a\n";} 

# Ignores case

for($i = 1 ; $i < 31; $i++)

{

        $count *= 2;

        print "$count ";

}

print "Standard while";

$count = 1;

while ($count <= 9 ) {

        print "$count\n";

        $count++;

}

print "Do until statement";

$count = 1;

do {

        print "$count\n";

        $count++;

} until ($count >= 9 );

 

last      # breaks out of loop

next     # skips rest of block without

               terminating, or moves the continue

redo     # Restart loop

 

SOMELABEL: while (condition){

    Things to do;

     If (other condition) {

        Lass SOMELABEL; # Break out of

                                            Block labelled

                                            SOMELABEL

        }

      }

         

print "Enter number :>";

$number = <STDIN>;

chomp ($number);

$number =~ tr/A-Z/a-z/;

 

delete $word{“one”}    # Removes the element One 1.

 

&a = “This is a test”

 

print "Enter :>";

$Input1 = <STDIN>;

chomp ($Input1); # remove crlf

print "You entered $Input1\n";

 
Reads standard in from command line 

#! /usr/bin/perl

# add line number to each line of input file

$count=0;

while (<>) {

        print "$count $_" ;

        $count++;

}

#! /usr/bin/perl

# awk and sed type substitution

while (<>) {

        s/a/@@@/g;

        print "$_" ;

}

 

./Prog File

Create a log file 

#! /usr/bin/perl -w

#

open (LOG, ">>/tmp/logfile.out") || die "Could not open /tmp/logfile.out";

print LOG "This is the first line\n";

 

open (PWD,"/etc/passwd");

while (<PWD>) {

        chomp;

        print "$_\n";

}

Select();                 # Changes (redirects)

                               file handle

Select(LOG);         # All output to log

Select(STDOUT);  # Back to screen

What Operating system 

my $os = $^O;
print "Running in: $os\n"; 
$input = <STDIN>;
chomp ($input);            # Removes crlf  
if (2 != 3){
      print “OK”;
}else {
      Print “Not OK”;
} 

if ( try eq try ) {
        print "Yes\n";
}else {
        print "No\n";
}
=~           # similar to grep 

if ( “Try this” =~ /^Tr/ ) {
                Print “Yes, Tr is contained in the string Try this”;
}
else {
                Print “No, Tr is not contained in the string Try this”;
} 
Hash must be included %number 

print “Enter number :>;
$number = <STDIN>;
Chomp ($number);
$number =~ tr/A-Z/a-z/; 

if ($words{$number} ) {
    print “$words{$number}\n;
}
else {
         print “Not in list\n”;
}
 
print "Standard until";
$count = 1;
until ($count >= 9 ) {
        print "$count\n";
        $count++;
}
@a = (1,2,3,4,5,6,7,8,9);
foreach (reverse @a) {
        print "$_\n";
}
 
Unless (this) {that;} = this || that
if ( $words{$number} ){
        print "$words{$number}\n";
}
else {
        print "Not in list\n";
} 
/^Tr/i    # Ignore case 

$_ 

Is a scratch variable used by perl. Printing it will return your last var 
printf “%15s %d $10.2f\n”, $s, $n, $r; 
                             
# Print string in a 15 char field
                                                                       
                             # Print number in a 5 char field
                             # Print floating point number to 2 decimal
                             # places in a 10 character field
/Fred\b/;                       # Boundaries    matches fred,
                                    elfred but not Fredrick
/\bFred/;                       # Boundary matches
                                     Fredrick, but not elFred
/\bFred\b/;                     # Fred only
/\b+\b/;                          # “y+x” but not “++” or “ + “
/\bFred\B;                     # “Fredrick” but not “Fred Flintstone”
[0123456789] [0-9]     #
[0-9\-]                          # 0 to 9 or –
[one|two|three]             # one or two or three
Functions 

#! /usr/bin/perl
#
sub ADD {
        my ($times);            # sum is now a local variable
        local ($value) = 5;   # This is a semi private variable, LOOKUP
        $times=50;
        return $_[0] * $times;
} 
$times=10;       # This will be printed bellowawk;
print ADD(10);
print "\n$times\n"; 
#! /usr/bin/perl -w
#
opendir (ETC, "/etc") || die "Could not open /etc";
while ($name = readdir(ETC)){
        print "$name\n";
}
closedir (ETC);
chdir
unlink         # Delete
link(“existing file”,”Linked file”) # Hard link
symlink(May exist”,”Link file”)  ~ symbolic
chmod(0666,”file1M”,”file2”,”fileEtc”

chown(500,35,”file1”.”file2”,”fileETC”);

utime

rename

Process as input 

#! /usr/bin/perl -w
open (WHO, "who|");
system ("clear");
while (<WHO>) {
        print $_;
}
close WHO;
Globing @a = </etc/hosts*>;@a = glob(“/etc/hosts*”); 
Substitution #
! /usr/bin/perl -w
## First char is 0
print substr("one two three",4,3);      # Returns two
print "\n";
# From end of string
print substr("one two three",-5,5);     # Returns three
print "\n";
print substr("one two three",4,100);    # Returns “two three”
print "\n";
System errors #
! /usr/bin/perl -w
system("date;who");
print `ls -l`;

print "Error no = $?\n";        # ls -l worked, so error = 0

system("ls -l notfile");
print "Error no = $?\n";       # If nofile does not exists, error = 512

Enter supporting content here