http://www.tutorialspoint.com/perl/perl_pop.htm
Perl How To's
Monday, February 20, 2012
Wednesday, November 16, 2011
Replacing Values in text File
#!/usr/bin/perl -w
# Print the value of the command line arguments
#$numArgs = $#ARGV + 1;
#print "You provided $numArgs arguments\n";
print "Input file is $ARGV[0]\n";
print "Output file is $ARGV[1]\n\n";
# Open input file in read mode
open INPUTFILE, "<", $ARGV[0] or die $!;
# Open output file in write mode
open OUTPUTFILE, ">", $ARGV[1] or die $!;
# Read the input file line by line
while (<INPUTFILE>) {
$_ =~ s/Fusion.test/sriram.test/g;
$_ =~ s/weblogin.test/newnewlogin.test/g;
print OUTPUTFILE $_;
}
close INPUTFILE;
close OUTPUTFILE;
====================================
sriram@ubuntu:~/perl$ ./check.pl value.txt output.txt
Input file is value.txt
Output file is output.txt
sriram@ubuntu:~/perl$
# Print the value of the command line arguments
#$numArgs = $#ARGV + 1;
#print "You provided $numArgs arguments\n";
print "Input file is $ARGV[0]\n";
print "Output file is $ARGV[1]\n\n";
# Open input file in read mode
open INPUTFILE, "<", $ARGV[0] or die $!;
# Open output file in write mode
open OUTPUTFILE, ">", $ARGV[1] or die $!;
# Read the input file line by line
while (<INPUTFILE>) {
$_ =~ s/Fusion.test/sriram.test/g;
$_ =~ s/weblogin.test/newnewlogin.test/g;
print OUTPUTFILE $_;
}
close INPUTFILE;
close OUTPUTFILE;
====================================
sriram@ubuntu:~/perl$ ./check.pl value.txt output.txt
Input file is value.txt
Output file is output.txt
sriram@ubuntu:~/perl$
Reading a argument in perl
$ cat argv.pl
#!/usr/bin/perl
print 'First Argument: ' . $ARGV[0];
print "\n********************\n";
print 'Second Argument: ' . $ARGV[1] ;
print "\n********************\n";
print 'Third Argument: ' . $ARGV[2];
sriram@ubuntu:~/perl$ ./argv.pl 1 2 333First Argument: 1
********************
Second Argument: 2
********************
Third Argument: 333
#!/usr/bin/perl
print 'First Argument: ' . $ARGV[0];
print "\n********************\n";
print 'Second Argument: ' . $ARGV[1] ;
print "\n********************\n";
print 'Third Argument: ' . $ARGV[2];
sriram@ubuntu:~/perl$ ./argv.pl 1 2 333First Argument: 1
********************
Second Argument: 2
********************
Third Argument: 333
Sunday, November 13, 2011
How To Configure Perl with Httpd/Apache in Fedora.
Install httpd and mod_perl using yum
check perl module is available in /etc/httpd/modules/mod_perl.so
Load Module :
in /etc/httpd/conf/httpd.conf
LoadModule perl_module modules/mod_perl.so
Add this line , which will enable you to run perl :
ScriptAlias /perl /var/www/perl/
<Location /perl/>
allow from all
SetHandler perl-script
PerlHandler ModPerl::Registry
PerlResponseHandler ModPerl::PerlRun
AddHandler cgi-script .cgi .pl
#PerlHandler ModPerl::PerlRun
Options FollowSymLinks +ExecCGI
PerlSendHeader On
</Location>
Permissions of directories:
#cd /var/www/
# ls -la
total 28
drwxr-xr-x. 7 root root 4096 Nov 13 19:55 .
drwxr-xr-x. 23 root root 4096 Nov 5 15:11 ..
drwxr-xr-x. 2 root root 4096 Oct 27 2010 cgi-bin
drwxr-xr-x. 3 root root 4096 Nov 13 18:53 error
drwxr-xr-x. 3 root root 4096 Oct 27 2010 html
drwxr-xr-x. 3 root root 4096 Nov 13 18:53 icons
drwxr-xr-x. 2 root root 4096 Nov 13 20:08 perl
# cd /var/www/perl
# ls -la
total 12
drwxr-xr-x. 2 root root 4096 Nov 13 20:08 .
drwxr-xr-x. 7 root root 4096 Nov 13 19:55 ..
-rwxr-xr-x. 1 apache apache 77 Nov 13 20:08 test.pl
#
check perl module is available in /etc/httpd/modules/mod_perl.so
Load Module :
in /etc/httpd/conf/httpd.conf
LoadModule perl_module modules/mod_perl.so
Add this line , which will enable you to run perl :
ScriptAlias /perl /var/www/perl/
<Location /perl/>
allow from all
SetHandler perl-script
PerlHandler ModPerl::Registry
PerlResponseHandler ModPerl::PerlRun
AddHandler cgi-script .cgi .pl
#PerlHandler ModPerl::PerlRun
Options FollowSymLinks +ExecCGI
PerlSendHeader On
</Location>
Permissions of directories:
#cd /var/www/
# ls -la
total 28
drwxr-xr-x. 7 root root 4096 Nov 13 19:55 .
drwxr-xr-x. 23 root root 4096 Nov 5 15:11 ..
drwxr-xr-x. 2 root root 4096 Oct 27 2010 cgi-bin
drwxr-xr-x. 3 root root 4096 Nov 13 18:53 error
drwxr-xr-x. 3 root root 4096 Oct 27 2010 html
drwxr-xr-x. 3 root root 4096 Nov 13 18:53 icons
drwxr-xr-x. 2 root root 4096 Nov 13 20:08 perl
# cd /var/www/perl
# ls -la
total 12
drwxr-xr-x. 2 root root 4096 Nov 13 20:08 .
drwxr-xr-x. 7 root root 4096 Nov 13 19:55 ..
-rwxr-xr-x. 1 apache apache 77 Nov 13 20:08 test.pl
#
Tuesday, November 8, 2011
Writing to File using Perl
sriram@ubuntu:~/perl$ cat file1.txt (File toWrite to)
The Rock|Cheer|Rock Bottom
Triple H|Boo|Pedigree
Stone Cold|Cheer|Stone Cold Stunner
sriram@ubuntu:~/perl$
sriram@ubuntu:~/perl$ cat write.pl(Perl Script to Write)
#!/usr/bin/perl
open (TEST, '>>file1.txt')
print TEST "Sriram Test \n";
close(TEST);
Result :
sriram@ubuntu:~/perl$ cat file1.txt (Result)
The Rock|Cheer|Rock Bottom
Triple H|Boo|Pedigree
Stone Cold|Cheer|Stone Cold Stunner
Sriram Test
sriram@ubuntu:~/perl$
The Rock|Cheer|Rock Bottom
Triple H|Boo|Pedigree
Stone Cold|Cheer|Stone Cold Stunner
sriram@ubuntu:~/perl$
sriram@ubuntu:~/perl$ cat write.pl(Perl Script to Write)
#!/usr/bin/perl
open (TEST, '>>file1.txt')
print TEST "Sriram Test \n";
close(TEST);
Result :
sriram@ubuntu:~/perl$ cat file1.txt (Result)
The Rock|Cheer|Rock Bottom
Triple H|Boo|Pedigree
Stone Cold|Cheer|Stone Cold Stunner
Sriram Test
sriram@ubuntu:~/perl$
Reading from a File in Perl
sriram@ubuntu:~/perl$ cat file1.txt
The Rock|Cheer|Rock Bottom
Triple H|Boo|Pedigree
Stone Cold|Cheer|Stone Cold Stunner
sriram@ubuntu:~/perl$ cat read.pl
#!/usr/bin/perl
open (MYFILE, 'file1.txt');
while (<MYFILE>) {
# To clear the new line use chomp
chomp;
print "$_\n";
}
close (MYFILE);
output:
sriram@ubuntu:~/perl$ ./read.pl
The Rock|Cheer|Rock Bottom
Triple H|Boo|Pedigree
Stone Cold|Cheer|Stone Cold Stunner
Few explanations :
Inside the loop, we use the chomp function to clear off the newlines from the end of each line, then we print the value of $_ to show that it was read.
chomp;
print "$_\n"; MYFILE is File Handle created you can use anything.
For more info, check this :
http://perl.about.com/od/perltutorials/a/readwritefiles.htm
Friday, November 4, 2011
Function - Substring (substr)
Substring (substr)
The substring function is a way to get a portion of a string value, rather than using the entire value.
The value can then be used in a loop or a conditional statement, or just for its own purposes.
For this one, you will probably want the general form of the function first.
The function is usually set to a variable so that the variable contains the value of the substring:
$ cat substr.pl
#!/usr/bin/perl
use strict;
my $test="sriram";
my $new = substr($test,3,5);
print "the new value of substring is $new\n";
sriram@ubuntu:~/perl$ ./substr.pl
the new value of substring is ram
sriram@ubuntu:~/perl$
The substring function is a way to get a portion of a string value, rather than using the entire value.
The value can then be used in a loop or a conditional statement, or just for its own purposes.
For this one, you will probably want the general form of the function first.
The function is usually set to a variable so that the variable contains the value of the substring:
$ cat substr.pl
#!/usr/bin/perl
use strict;
my $test="sriram";
my $new = substr($test,3,5);
print "the new value of substring is $new\n";
sriram@ubuntu:~/perl$ ./substr.pl
the new value of substring is ram
sriram@ubuntu:~/perl$
Subscribe to:
Posts (Atom)