Вы находитесь на странице: 1из 14

Standard operators

+,-,*,/,%
eg : $result=3+4;
$ftoc=(212-32)*(5/9);
$square=16*2;
the function automatically use the value of $ if you
fail to specify a variable on which to operate.
Abs---the absolute value
print abs(-1.295);
value will be printed as 1.295
Int converting floating points to integers
print int abs(-1.295);

value will be printed as 1.


No rounding of sort is done.
The number that has been rounded to decimal place
we use
printf or sprintf function

printf(%.2f,abs(-1.295);
number to two decimal places
value will be 1.30.
0 is appended in the output to show the two
decimal places.
Exp---raising e to the power
exponential operator**
eg: $square=4**2;
value =16
The natural base number e to the power, you need to
use the exp function.
exp EXPR
exp
if didnt use EXPR argument, exp use value of the
$_variable as the exponent.
$square = exp(2);
Sqrtthe square root
square root of numberuse sqrt function
$var = sqrt(16384);
nth root** operator with a factional number
$ var=16384**(1/2);
The value will be 128
Cube root
Cube root of 16777216
$var= 16777216**(1/3);
Value will be 256.
Log logarithm
Logarithm(base e)use log function.
$log = log 1.43;
Trigonometric function
Three built-in trigonometric function
Arctangent squared (atan2)
Cosine (cos)
Sine (sin)
Eg: atan2 x,y
cos EXPR
sin EXPR
Arcsine, arccosine and tangent need to be access we
can use the POSIX module, it supplies acos, asin and tan
function.

My ($phys_step, $emot_step, $inte_step)=(23, 28, 33);


Use math::complex;
Print enter the number of days you been alive:\n;
My $alive=<STDIN>
$phys=int(sin(((pi*($alive%phys_step))/($phys_step/2)))*100);
$phys=int(sin(((pi*($alive%emot_step))/($emot_step/2)))*100);
$phys=int(sin(((pi*($alive%inte_step))/($inte_step/2)))*100);

Print your physical is $phys%,emotional $emot%,intellectual


$inte%\n;

Conversion between bases


converting decimal for numerical binary, octal, hexa decimal.
it will not translate automatically values contained within string.
it can defined using string literals or from string imported from
the outside worlds(files,userinputs).
string based literal use oct or hex function.
hexa decimal will be print with or without 0x
Eg: print hex (ff47ace3);
print hex (0xff47ace3);
hexa function doesnt work with other numbers.
string starts with 0,0b or 0x better of using oct function.
oct function interprets a string without a prefix as an octal
string and raise an error.
print oct(755); is valid
printf oct(aef);will fail
string using one of the literal formats prefix, oct will convert
it.
print oct(0755);
print oct(0x7f);
print oct(0b0010001);
both oct and hex default using the $_variable, if you fail
to supply and arugument.
to print decimal value into octal,hexa,binary use printf or
sprintf to string.
Printf(%1b%1o%1x,
oct(0b00010001),oct(0755),oct(0x7f));
Conversion between characters and
numbers
to insert specific character into a string by its numerical
value use \0 or \x
print \007;
print \x07;
by using two function together
print chr(ord(b));

ord function returns the numeric value of the first


character of EXPR.
value will be according to ASCII table.
Random numbers
perl provides a built-in random number generator.
randam number needs a seed value,which is used in an
algorithm.
format for random function is
rand EXPR
rand
function returns a floating-point random number between 0
and EXPR or between 0 and 1 if EXPR is not specified.
integer random number use the int function to return
reasonable value.
print int (rand(16)),\n;
srand function toseed the random number
generate with a specific value.
the rand function calls the srand function. if it fail to call it
gives predictable value.
to get unpredictable value to calculate current time,
process id and user id.
srand((time() ^ (time() % $ ] )) ^ exp(length($0))**$$);
My %randres;
My $counter=1;
srand((time() ^ (time() % $ ] )) ^ exp(length($0))**$$);
While (my $val =rand())
{
Last if (defined($randres{ $val }));
Printcurrent count is $counter\n if (($counter %10000)==0);
$randres {$val} =1;
$counter++;
}
Print out of $counter tries I encountered a duplicate
random number\n;
Math::TrulyRandom module provides more robust system
for generating random numbers.
truly_random_value function n place as rand function in
the predicating program, it takes longer time before
random number reappears.

Working with very small integers


Perl uses 32-bit integers for storing integer.
Store and handle integers that are smaller than the
standard 32-bit integers.
Single character value for each boolean value will take
up 8-bits.
Vec function supports storage of multiple integers as
strings.
Vec EXPR, OFFSET, BITS
EXPRto store information, OFFSET and BITS element of
integer string and size of each element.
Vec($optstring, 0, 1)=$print ? 1:0;
vec($optstring, 1, 1)=$display ? 1:0;
vec($optstring, 2,1)=$delete ? 1:0;
print length($optstring),\n;

Less than one real byte of information can store three


boolean values.
Bits argument allows you to select large bit strings:
Perl supports values of 1,2,4,8,16,32 bits.
In a single byte we can store four 2-bits integers.
Vec function used to extract and update any string,
modify bits at a time.
First bits of string can be accessed with
vec($var, 0, 8);
Vec function Used with function that require bitsets such
as select function.

Вам также может понравиться