Wednesday, 16 February 2011

PERL

# Hello world in perl
print "Hello World!\n";


#[This example does not provide the UNIX file directory path #which is usr/bin. The \n is also used in C and Java; without it #the prompt would remain on the same line]
#ActivePerl from ActiveState is probably now the most the #industry-standard, commercial-grade Perl distribution, 
#available for Solaris, AIX and HP-UX or indeed the Enterprise #Edition. I would actually rely on ActivePerl to protect my #infrastructure and stay competitive with quality-assured #business solutions. Since it is compatible with the reference #distribution of Perl, the Code tested will run on any Perl #installation that has the extensions installed.


#Perl adapts the syntax of the C language including the use of semicolons at
the end of each statement and the use of curly brackets to identify a block of
commands.



$Name = “James “ . “Snyder”;

#the concatenation operator takes the string “James “ and combines it with the second string “Snyder” to create a single
string that contains “James Snyder”.


# In Perl 5.8, the default sorting algorithm is merge sort.
#In earlier versions of Perl, the default sorting algorithm was quick sort.


if $x > 5
{
command1;
command2;
}
#To identify a block of commands in Perl, use curly brackets


$myVar
#Perl allows variables to hold any data types.
variable names begin with the dollar sign symbol
Perl is case-sensitive, so the Perl variable $myVar is considered completely different from $MYVar



< = >
#a Perl only relational operator that compares two values and returns a True or False value

== # The relational operator in Perl is two equal sign symbols (==) whereas the relational operator in other programming languages is just a single equal sign symbol (=).

(
#this is a unique comparison with signed result operator (< = >)
#this compares two values
#this returns 0 (if the two values are equal),
#thias returns 1 (if the first value is greater than the second)
#this returns  –1 (if the first value is less than the second)

&&
# This Logical operator compares two Boolean values
# compares (True [1] or False [0]) and return a single True or False value to a truth table
++ 
#Perl has a special increment (++)  operator, which simply adds to a variable

--
#Perl has a special decrement (--) operator, which simply subtracts 1 to a variable

+=
#Addition assignment operator

-=
# Subtraction assignment operator

if (condition) {
Command1;
Command2;
}
#branching statement
#The simplest branching statement is an IF statement that only runs one or more commands if a Boolean condition is True. #In Perl, the IF statement uses curly brackets to enclose one or more commands

if (condition) {
Command;
Command;
}
else {
Command;
Command;
}
#make the computer choose between two mutually exclusive sets of commands
#use an IF-ELSE statement in Perl

if (condition1) {
Command;
Command;
}
elsif (condition2) {
Command;
Command;
}
elsif (condition3) {
Command;
Command;
}
#The IF-ELSE statement only offers two choices.
#If you want to offer multiple choices, use the IF-ELSEIF statement
#IF-ELSEIF uses two or more Boolean conditions to choose which of two or more groups of commands to run.
#use the ELSIF keyword

SWITCH
#Perl does not provide a SWITCH statement.

sub functionname {
Commands;
return $value;
}
# In Perl, every subprogram is a function that can return a value.
# This is the format of a typical Perl function

sub functionname {
foreach $variablename (@_) {
Commands;
}
return $value;
}
#When you pass parameters to a Perl function,
#that function can access parameters with the foreach keyword and the @_ array

@arrayname = (element1, element2, element3);
# Perl offers three data structures: arrays, hash arrays, and references
#. An array stores multiple items, identified by an index number.
#A hash array stores multiple items, identified by a key, which can be a number or a string.
#Perl arrays are zero-based, so the first element of an array is considered 0, the second is 1, and so on.
#,you must name that array with the @ symbol when you create a Perl array .
#You can define the elements of an array at the time you create the array

@numberarray = (1, 2, 3, 4, 5);
# you can list each number individually if you want to create an array that contains a range of numbers

@numberarray = (1..5);
# use the range operator (..) to define the lower and upper bounds of a range

@numberarray = (1..10);
$thisone = $numberarray[0];
# use the dollar sign ($) symbol in front of the array name to access the individual elements stored in an array
#The value stored in the $thisone variable is the first element of the @numberarray, which is 1.

push(@arrayname, item2add);
#use arrays to mimic a stack data structure with Perl’s push and pop commands.
# use the push command to push a new item onto an array.

$variablename = pop(@arrayname);
# use the pop command to pop an item off the array

%hasharray = (
key1 => value1,
key2 => value2,
key3 => value3,
);
# This is a Perl hash array
# A hash array stores an item along with a key.
#there are two ways to store values and keys; this is the first way
# hash arrays are identified by the percentage (%) symbol.

%hasharray = (“key1”, value1, “key2”, value2, “key3”,
value3);
# A second way to define a hash array

$variable = $hasharray (“key1”);
# you need to know the key associated with that value
#identify the hash array name by using the $ symbol
#This command would store the value associated with

package className;
sub new {
my $objectname = {
Data;
Data;
};
bless $objectname, $className;
return $objectname;
sub methodname{
Commands;
Commands;
};
# Perl is a true object-oriented programming language.
# create and use objects in your programs.
# define a class to create an object
# This is a typical class definition

my $variablename = classname->new();
# After i define a class, i can create an object from that class by declaringconstructor method commonly called "new"
a variable as a new class type.
# I create an object by creating a

package newobject;
use class2inheritfrom;
@ISA = qw(class2inheritfrom);
# use the @ISA variable inside a new class to use inheritance in Perl

$DNA1 = ‘ACTGTTG’;
$DNA2 = ‘TGTACCT’;
$DNA3 = “$DNA1$DNA2”;
print $DNA3;
# concatenation is the simplest form of string manipulation
#concatenation joins multiple strings into one.
“key1” into the $variable.
ACTGTTGTGTACCT
# This is what this simple program will print

$DNA = ‘ACTGTTG’;
$compDNA = tr/ACGT/TGCA/;
# The
# The tr/ACGT/TGCA/; command tells Perl to translate every A into a T, every C into a G, every G into a C, and every A into a T all at once.The two strands of DNA are complimentary sequences.
use a simple program that replaces every A with a T, every C with a G, every T with
an A, and every G with a C.
# This is to calculate a complimentary sequence by knowing only one of the sequences
tr command simply tells Perl to translate or swap one character for another.

$DNA = ‘ACTGTTG’;
$compDNA = tr/ACGT/TGCA/;
$revDNA = reverse $compDNA;
# The second step in determining a complimentary sequence is to reverse the
order of that sequence.
# sequences are always written a specific way
# sequences start with the end of the sequence known as 5’ phosphoryl (also
known as 5 prime or 5’) and ending with 3’ hydroxyl (known as 3 prime or
3’).
# reverse it using this Perl command to display the complimentary sequence correctly

$DNA3 = “$DNA1$DNA2”;#
The first method  to concatenate two strings is to smash two strings together

$DNA3 = $DNA1 . $DNA2;
# The second way to concatenate the same two strings uses this dot operator.

Javascript

JSON is the best way to exchange information.


In a body analogy, JavaScript is the muscle that manipulates the webpage and makes changes without reloading the webpage.



<html>
<body>
<script language="JavaScript" type="text/javascript">
// Hello World in JavaScript
document.write('Hello World');
</script>
</body>
</html>

Tuesday, 15 February 2011

PHP

<html>
<body>

<?php
echo "Hi there!";
$txt="Hi there!"; //a variable containing a string
echo $txt

$txt1="Hi!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2; //the concatenation operator

$var_name = value;
$d=date("D");
if ($d=="Fri") echo "Have a nice weekend!"; //if statement

$d=date("D");
if ($d=="Fri")
  echo "Have a nice weekend!";
else
  echo "Have a nice day!"; // if..else statement

$d=date("D");
if ($d=="Fri")
  echo "Have a nice weekend!";
elseif ($d=="Sun")
  echo "Have a nice lazy Sunday!";
else
  echo "Have a good day!"; // if...elseif....else Statement

$cars[0]="Mini Cooper";
$cars[1]="Rover";
$cars[2]="BMW";
$cars[3]="Toyota";
echo $cars[0] . " and " . $cars[1] . " are BRITISH cars."; // Numeric array: Mini Cooper and Rover are BRITISH cars.

$ages['James'] = "37";
$ages['John'] = "44";
$ages['Anthony'] = "43";

echo "James is " . $ages['JAMES'] . " years old."; // JAMES is 37 years old.
$x=array("ding","dang","dong");
foreach ($x as $value)
  {
  echo $value . "<br />";
  } // ding
       dang
       dong

function writeName()
{
echo "Michael James Snyder";
}

echo "My name is ";
writeName();
?> // PHP Function: My name is Michael James Snyder

function writeName($fname)
{
echo $fname . " Snyder.<br />";
}

echo "My name is ";
writeName("Michael James");
echo "My sister's name is ";
writeName("Wendy");
echo "My brother's name is ";
writeName("John");
?> // My name is Michael James Snyder.
      My sister's name is Wendy.
      My brother's name is John Snyder.

Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old. // Form:
// Welcome John!
// You are 44 years old.

$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
echo "Tomorrow is ".date("Y/m/d", $tomorrow); // Tomorrow is 2011/02/16

?>



<html>

/* HTML creates user interfaces, but to create dynamic websites, or to transfer data from a Web page to another database program, use PHP Hypertext Processor (www.php.net). Dynamic web pages need to respond to the user, and need to retrieve information off a Web page and store it in a database, such as when you type your credit card number to buy something off a Web site. PHP is a popular programming language for running programs on Web servers (those computers responsible for displaying and retrieving information from Web pages, such as shopping data. PHP programs can run only on Web pages under many different operating systems. PHP mimics the syntax from C and Perl. */
<body>
/* If a comment extends over multiple lines,
It’s easier to use these types of comment
symbols instead. */
<?php // single line comment: The <?php and ?> tags define the beginning and ending of a PHP program.
echo “<h1>Greetings from PHP.</h1>”;
</body>
?>
# This is the end of the PHP program
</html>
// PHP program scripts are stored in a file that ends with the .php file extension.

$VariableName = value; // Declare any type of data; a variable holds a string or a number
VariableName
$MyAge // variable 1 is case sensitive
$myage // variable 2 is case sensitive

$myage = 38; //$myage variable is initially set to 38
$yourage = $myage; // the $myage variable also contains the value of 38
$myage = 103; // this line now stores the value 103 into the

$myage = 38;
$yourage = &$myage;
//Reference a variable with the ampersand symbol (&)
// allow a variable to contain identical data without specifically assigning those values.
// The $yourage variable references the $myage variable
// whatever value the $myage variable contains from now on will automatically get stored in the $yourage variable.
$myage = 103; //the $myage variable now contains the value of 103, so the $yourage variable contains 103 too.

if (condition) {
Command;
}

if (condition) {
Command;
}
else {
Command;
}


if (condition1) {
Command;
}
elseif (condition2) {
Command;
}
elseif (condition3) {
Command;
}


switch (expression) {
// use regular expressions that are built-into PHP to implement a finite automaton search algorithm
case value1:
Command;
break;
case value2:
Command;
break; // include the break command to tell the computer when to exit out of the SWITCH statement.
default:
Command;
}

if (expression == value1) {
// use regular expressions that are built-into PHP to implement a finite automaton search algorithm
Command;
}
elseif (expression == value2) {
Command;
}
else {
Command;
}
switch (expression) {
// use regular expressions that are built-into PHP to implement a finite automaton search algorithm
case value1: // check if a variable matches multiple values
case value2: // stack multiple case statements
Command;
break;
case value3:
case value4;
Command;
break;
default:
Command;
}
if (expression == value1) || (expression == value2) {
Command;
}
else if (expression == value3) || (expression == value4) {
Command;
}
else {
Command;
}

for (startvalue; endvalue; increment) { // a loop that repeats for a fixed number of times
Command; // loop until a certain Boolean condition becomes True
}

for ($i = 1; $i <= 4; $i++) { // The FOR loop runs four times, set the Start value to 1 and the End value to 4
Command;
}
while (condition) { // I don’t know how many times I need to repeat commands here
Command; // If the condition is True, the loop runs at least once. If this condition is False, the loop doesn’t run.
}

function functionname (Parameter list)
// this is a subprogram that solves a specific task
// The Parameter list defines any data that the function needs to work.
// If the function doesn’t need to accept any values, the parameter list can be empty.
{
Commands;
return $value; // Defines a value to return.
}

function myfunction () // This function doesn’t return a value or accept any parameters

{
Command;
}
$arrayname[index] = data;
/*Arrays can hold any type of data and grow as large as I need them without having to define a size ahead of time.
To create an array, I define an array name, the data I want to store, and the index number where I want to store that item in the array*/

$myarray[0] = “Hello”; // I want to store the string “Hiya”
$myarray[1] = 3.57; // I want to store the number 3.57 in the first and second elements of an array

$arrayname[“key”] = data; // I create associative arrays to  identify data by a unique string key not an index number

$myarray[“pi”] = 3.14; // I assign the number 3.14 to the “pi” key

$variable = $arrayname[“key”]; I use the key value to retrieve data from an associative array

$myarray[“pi”] = 3.14;
// I want to retrieve data stored under the key “pi
// This first line stores the value 3.14 into the array and assigns it to the key “pi”.
$number2use = $myarray[“pi”];
// This second line pulls out the data, associated with the key “pi”
// This stores that data into the $number2use variable.
// “array_pop” is a built-in array functions for manipulating arrays
// “array_pop” removes the last element from an array
// “array_push” adds an element to the end of an array
// “sort” sorts arrays in ascending order

class classname {  // define a class
public $propertyname; // specify the property to create an object
public function methodname() { // specify the methods to create an object
commands;
}
}

$objectname = new classname; // I use this Syntax to create an object

$objectname->propertyname = value;
// I specify the object name and the property you want to use to assign a value to an object’s property
// the dollar symbol is not used when assigning a value to an object’s property
// ($) isn’t used to designate the property name.

$objectname->methodname();
// specify the object name followed by the method name to tell an object to run a method

class classname1 {
public $propertyname;
public function methodname() {
commands;
}
}
class classname2 extends classname1 {
// use the extends keyword followed by the class name you want to inherit from to inherit from a class
// an object can inherit from one class
// This is “single inheritance” in Object Oriented Programming
public $propertyname;
public function methodname() {
commands;
}
}

include_once "a.php"; // this will include a.php
/*include is a directive or statement that causes another PHP file to be included and evaluated*/

Saturday, 12 February 2011

Simple Object Access Protocol - SOAP

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">


<soap:Header>
...
</soap:Header>


<soap:Body>
...
  <soap:Fault>
  ...
  </soap:Fault>
</soap:Body>


</soap:Envelope>




<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  ...
  Message information goes here
  ...
</soap:Envelope>


soap:encodingStyle="URI" 
[This encodingStyle attribute is used to define the data types used in the document]


soap:mustUnderstand="0|1"
[This SOAP mustUnderstand attribute can be used to indicate whether a header entry is mandatory or optional 
for the recipient to process.
If we add mustUnderstand="1" to a child element of the Header element 
it indicates that the receiver processing the Header must recognize the element.]


A SOAP Request


POST /InStock HTTP/1.1
Host: www.opsecsecurity.com/stock
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn


<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">


<soap:Body xmlns:m="http://www.opsecsecurity.com/stock">
  <m:GetStockPrice>
    <m:StockName>Applied Holographics</m:StockName>
  </m:GetStockPrice>
</soap:Body>


</soap:Envelope>








A SOAP Response:


HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn


<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">


<soap:Body xmlns:m="http://www.opsecsecurity.com/stock">
  <m:GetStockPriceResponse>
    <m:Price>42</m:Price>
  </m:GetStockPriceResponse>
</soap:Body>


</soap:Envelope>






---






soap:actor="URI"
[This SOAP actor attribute is used to address the Header element to a specific endpoint.]
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">


[Question]
<soap:Body>
  <m:GetPrice xmlns:m="http://www.ba.com/prices">
    <m:Item>Dubai</m:Item>
  </m:GetPrice>
</soap:Body>


<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">


[Answer]
<soap:Body>
  <m:GetPriceResponse xmlns:m="http://www.ba.com/prices">
    <m:Price>1000.00</m:Price>
  </m:GetPriceResponse>
</soap:Body>


</soap:Envelope>


</soap:Envelope>


[HTTP Protocol Question]
POST /item HTTP/1.1
Host: 192.168.2.4
Content-Type: text/plain
Content-Length: 200


[HTTP Protocol Answer]
200 OK
Content-Type: text/plain
Content-Length: 200