Formats are defined by using a keyword format and ended by a dot(.) and to write using that format we have to assign this value to system variable $~.
Below are some examples of formats:
Program Code:
#! /Perl/bin/perl
$~ ="MYFORMATHEADER";
write;
format...
Method 1:
Program Code:
#! /Perl/bin/perl
print ("Enter the start number:\n");
$start = <STDIN>;
chop ($start);
print ("Enter the end number:\n");
$end = <STDIN>;
chop ($end);
@list=($start..$end);
$count=0;
print("Here is the...
To read files from any directory first we have to access that directory. We have used opendir() to get that directory.Now to get all the files we have used readdir() so that all the files are listed in that directory.At last we have closed...
Program Code:
#!/Perl/bin/perl
print("Enter the question you want to ask \n");
$input=<STDIN>;
if($input =~ /please/)
{
print("You are good in nature.My name is Abhishek \n");
}
else
{
print("First use word please");
}
Output:
Program...
rand() function is used to generate a random number.
First it will generate a random number between 0 and 1 and then it will multiply it with the number which is passed as argument in rand() function.So whatever is the argument the number...
Sleep is used to stop Perl interpreter executing the next statement for the specified time
In following example the next statement will be executed after 60 seconds.
Program Code:
# !/Perl/bin/perl
print("This is a demo of sleep...
Program Code:
#! /Perl/bin/perl
$var=1234;
$var="This is the number with value $var \n";
print($var);
$var=12;
print("This is a number with value $var \n");
# \L is used to convert the letters to lower case and \E is used to disable...
Subroutines are like functions in C language.We can define a subroutine by writing sub and then variable name.
For example:
sub read{
$line=<STDIN>;
}
Above is a subroutine which is used to read input from Standard Input Device (such...
Program Code:
#!/Perl/bin/perl
print("Enter a sentence to convert into lower form\n");
while($input=<STDIN>)
{
$input=~ tr/A-Z/a-z/;
print($input);
}
Output:
...
Program Code:
#!/Perl/bin/perl
print("Enter the variable name \n");
$input=<STDIN>;
chop($input);
if($input =~ /^\$[A-Za-z][_0-9a-zA-Z]*/)
{
print("$input is a valid scalar variable name in perl \n");
}
elsif($input =~ /^\@[A-Za-z][_0-9a-zA-Z]*/)
{
...
Program Code:
# !/Perl/bin/perl
print ("Enter 0 or 1\n");
$input=<STDIN>;
if($input==0){
warn("Hi This is a warning\n");
}
else{
warn("Hi This is a warning with info about the program");
}
print("\nActually i was just giving...
When we want to abort the program then we can use die.If we will use "\n" at the end then it will not explain the line number where it has aborted and other info. If you will not use "\n" at end then it will give the info.
Program Code:
#...
In this post i will explain some subroutine in Perl.
1)AUTOLOAD subroutine:
In perl , if this subroutine is defined in the package, then if any undefined subroutine in that package is called then then the statements of this AUTOLOAD...
In this post i have written a code to sort an array.
Program Code:
#! /Perl/bin/perl
print("Enter elemnets of array to be sorted\n Enter an empty line to quit\n");
$count=1;
$inputline=<STDIN>;
chop($inputline);
while($inputline...
1)Length of Array:
To find the length of any given array just use a scalar variable and do the following:
$len=@array;
where len is a scalar variable and array is an array variable
Program Code:
#! /Perl/bin/perl
@array=(1,2,3,4,5,6);
$len=@array;
print("Length of array is $len");
Output:
Length of array is 6
2)Array Modification:
We...
1)join():
This function is used to join the elements using some specific character which has been passed as argument in this function. So the first character is used to join the elements and then after that we can pass as many arguments as possible.These arguments can be either strings,numbers or array.
#! /Perl/bin/perl
@array=(I,am,a,good,boy);
$str=join(":",@array,1,2,"no","its");
print($str);
Output:
...
chop():
The chop function is used to remove the last character of any element.
I have taken the example of array and then applied the chop() function on it.
#! /Perl/bin/perl
@array=(aa,bb,cc,dd,ee,fd,gf,hg,sd);
print("Array before...
In this post i will explain the functions that can be used with array in Perl.
sort():
This function is used to sort the elements of an array. This function is specially used to sort elements of type string.
#! /Perl/bin/perl
print("Enter...
In this post i will explain about aliases in array
In the program first we have defined one scalar variable and an array variable with same name, We printed the value of variable which is 100.After that we have called a subroutine with argument....
In this post i will explain about the arrays in Perl.
When we want to declare an array variable we will use @ symbol so that perl can know that it is an array variable.
for ex
@array=(1,2,3);
Suppose...
Program Code
#!/Perl/bin/perl
$var=2;
print("The value assigned to variable initially is ".$var."\n After Multiplication ");
$var=$var*2;
print($var,"\n After Addition and subtraction ");
$var=3+45-23;
print($var);
Output
...