SlideShare ist ein Scribd-Unternehmen logo
1 von 43
Downloaden Sie, um offline zu lesen
Perl for System Automation
1
Danairat T.
Line ID: Danairat
FB: Danairat Thanabodithammachari
+668-1559-1446
Danairat T.
Contents
Module-1: Advanced File Processing with Perl
• Types of open Filehandles and File Locking
• Read/Write File line by line
– Read Line
– Write Line
– Append Line
• Lock File
• Read entire files into Array
• Read entire files into Scalar using file record separator
• Read file paragraph by paragraph using file record separator
• Read files in the Directory
• Read SubDir in the Directory
• Read files recursively in Directory
Danairat T.
Contents
Module-2: System Interaction
• Enumerating OS user identity
• Enumerating OS group identity
• Activating System Command
– Using exec()
– Using system()
• Grabbing a program's output
– Using qx()
• Display current running processes
– Using ProcessTable module
• Terminate the Process
– Built in kill() function
Danairat T.
Contents
Module-3: Managing Networks with Perl
• Host file
• IP Lookup
• FTP
• E-mail
• Web
• LDAP
Danairat T.
Contents
Module-4: Perl Coding Guidelines
• Perl Coding Style Guides
– The 12 general guidelines
– Don’t do in perl codes
– To avoid in perl codes
– Performance coding guidelines
• Perl Coding Guides for Security
– Command line inputs
– The exec(), system(), qw() and Perl modules.
Danairat T.
What is Perl?
(Practical Extraction and Report Language )
• Perl, release in 1987, is a high-level
programming language written by Larry Wall.
Perl's process, file, and text manipulation
facilities make it particularly well-suited for
tasks system utilities, system management
tasks, database access, networking. These
strengths make it especially popular with
system administrators.
6
Danairat T.
Which version of Perl should I use?
• No one is actively supporting Perl 4. Five years ago it
was a dead camel carcass
• There is no Perl 6 release scheduled.
• The immediate releases are Perl 5.8 (i.e. Perl5.8.x )
7
Danairat T.
OS Platforms Support
• UNIX: More or less every UNIX or UNIX-like operating system ever created, notably
Linux , Solaris, AIX, IRIX, HP/UX, BSD, and Tru64
• MS Windows: DOS, Windows 3.1, 95, 98, NT and 2000
• Other Desktop OSs: Apple Macintosh (68k and PPC, both pre and post MacOS X),
Acorn Risc OS, Amiga, BeOS, OS/2, and many others
• Mainframes: AS/400, OS390, VMS and OpenVMS, Stratus (VOS), and Tandem
• PDAs: EPOC (Psion/Symbian), but not PalmOS or Windows CE at time of writing,
although porting efforts are being made for both platforms
8
Danairat T.
Identify the Perl version
• perl –v
• perl -V
9
Danairat T.
www.CPAN.org
• CPAN stands for
Comprehensive Perl
Archive Network,
containing libraries,
documentation, and many
Perl third-party modules
and extensions
10
Danairat T.
Install Perl
• On Unix System
– pkgadd -d /cdrom/sol_10_1009_x86/Solaris_10/Product SUNWperl584core
– pkgadd –d /cdrom/sol_10_1009_x86/Solaris_10/Product SUNWperl584usr
– pkgadd –d /cdrom/sol_10_1009_x86/Solaris_10/Product SUNWperl584man
• On Windows System
– run ActivePerl-5.8.9.826-MSWin32-x86-290470.msi
Available for download at www.activestate.com
11
Danairat T.
Perl file structure and location
• Perl executable: /usr/perl5/bin/perl or /usr/bin/perl
• Perl Pain Old Documentation: /usr/perl5/pod
• Perl Manual: /usr/perl5/man
12
Danairat T.
Perl Libraries (Perl Modules)
• The Perl Libraries directories
– /usr/perl5/5.8.4/lib/i86pc-solaris-64int
– /usr/perl5/5.8.4/lib
– /usr/perl5/site_perl/5.8.4/i86pc-solaris-64int
– /usr/perl5/site_perl/5.8.4
– /usr/perl5/site_perl
– /usr/perl5/vendor_perl/5.8.4/i86pc-solaris-64int
– /usr/perl5/vendor_perl/5.8.4
– /usr/perl5/vendor_perl
13
Danairat T.
Perl Library (The modules for Unix)
http://www.cpan.org/modules/01modules.index.html
14
Danairat T.
Perl Library (PPM modules for windows)
http://ppm.activestate.com/PPMPackages/zips/8xx-builds-only/Windows/
15
Danairat T.
Install Perl Modules on Unix
gzip -dc yourmodule.tar.gz | tar -xof -
cd /your/new/lib/to/install/
perl Makefile.PL
make
make test
make install
Or, if you have internet access
perl -MCPAN -e shell
cpan> i /Time/
cpan> install Time::CTime
16
Danairat T.
Install Perl Modules on Unix
bash-3.00# cd /tmp/Class-Simple-0.19
bash-3.00# perl Makefile.PL
Checking if your kit is complete...
Looks good
Writing Makefile for Class::Simple
bash-3.00# make
cp lib/Class/Simple.pm blib/lib/Class/Simple.pm
Manifying blib/man3/Class::Simple.3
bash-3.00# make test
PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_harness(0,
'blib/lib', 'blib/arch')" t/*.t
t/anon.............ok
t/attributes.......ok
t/Class-Simple.....ok
t/inherit..........ok
t/isa..............ok
t/nonew............ok
t/override.........ok
All tests successful.
Files=13, Tests=95, 3 wallclock secs ( 0.99 cusr + 0.52 csys = 1.51 CPU)
bash-3.00# make install
Installing /usr/perl5/site_perl/5.8.4/Class/Simple.pm
Installing /usr/perl5/5.8.4/man/man3/Class::Simple.3
Writing /usr/perl5/site_perl/5.8.4/i86pc-solaris-64int/auto/Class/Simple/.packli
st
Appending installation info to /usr/perl5/5.8.4/lib/i86pc-solaris-64int/perlloca
l.pod
bash-3.00#
Example:-
17
Danairat T.
Installing Perl modules on Sun Solaris 10
1. Login as root
2. Add path /usr/sfw/bin to /.profile
3. Download and unzip the Perl module then cd
into its directory
4. Follow the below command steps:-
– /usr/perl5/bin/perlgcc Makefile.PL
– gmake
– gmake test
– gmake install
18
Danairat T.
Install Perl Module on Windows
1. Unzip the lib file
2. Cd to the downloaded lib
3. ppm install yourdownloadedlib.ppd
19
Module-1: Advanced File
Processing with Perl
20
Danairat T.
Topics
• Types of open Filehandles and File Locking
• Read/Write File line by line
– Read Line
– Write Line
– Append Line
• Lock File
• Read entire files into Array
• Read entire files into Scalar using file record separator
• Read file paragraph by paragraph using file record separator
• Read many files in a Directory
• Read SubDir in the Directory
• Read files recursively from Directories
21
Danairat T.
File Handle Options
22
mode operand create
delete and recreate
file if file exists
read <
write > ✓ ✓
append >> ✓
read/write +<
read/write +> ✓ ✓
read/append +>> ✓
Danairat T.
Open File Options
23
• Using < for file reading.
#!/usr/bin/perl
use strict;
use warnings;
my $myFile = "fileread.txt"; # the file “filetest.txt” must be exist
my $myLine;
if (open (MYFILEHANDLE, '<' , $myFile)) { # using ‘<‘ and . for file read
while ($myLine = <MYFILEHANDLE>) { # read line
chomp($myLine); # trim whitespace at end of line
print "$myLine n";
}
close (MYFILEHANDLE);
} else {
print "File could not be opened. n";
}
exit(0);
OpenFileReadEx01.pl
Results:-
<print the file content>
Danairat T.
Open File Options
24
• Using > for file writing to new file.
#!/usr/bin/perl
use strict;
use warnings;
my $myFile = "filewrite.txt";
my @myData = ("line1", "line2", "line3");
if (open (MYFILEHANDLE, '>' , $myFile)) {
foreach my $myLine (@myData) {
print MYFILEHANDLE "$myLine n"; # print to filehandle
}
close (MYFILEHANDLE);
} else {
print "File could not be opened. n";
}
exit(0);
OpenFileWriteEx01.pl
Results:-
<see from the output file>
Danairat T.
Open File Options
25
• Using >> to append data to file. If the file does not exist then it is create a
new file.
#!/usr/bin/perl
use strict;
use warnings;
my $myFile = "filewrite.txt";
my @myData = ("line4", "line5", "line6");
if (open (MYFILEHANDLE, ‘>>' , $myFile)) {
foreach my $myLine (@myData) {
print MYFILEHANDLE "$myLine n"; # print to filehandle
}
close (MYFILEHANDLE);
} else {
print "File could not be opened. n";
}
exit(0);
OpenFileAppendEx01.pl
Results:-
<see from the output file>
Danairat T.
File Locking
• Lock File for Reading (shared lock): Allow other to
open the file but no one can modify the file
• Lock File for Writing (exclusive lock): NOT allow
anyone to open the file either for reading or for
writing
• Unlock file is activated when close the file
26
Shared lock: 1
Exclusive lock: 2
Unlock: 8
Danairat T.
File Locking – Exclusive Locking
27
#!/usr/bin/perl
use strict;
use warnings;
use Fcntl;
my $file = 'testfile.txt';
# open the file
open (FILE, ">>", "$file") || die "problem opening $filen";
# immediately lock the file
flock (FILE, 2);
# test keeping the lock on the file for ~20 seconds
my $count = 0;
while ($count++ < 30)
{
print "count = $countn";
print FILE "count = $countn";
sleep 1;
}
# close the file, which also removes the lock
close (FILE);
exit(0);
FileExLockEx01.pl
Please run this concurrence
with FileExLockEx02.pl, see
next page.
Results:-
<see from the output file>
Danairat T.
File Locking – Exclusive Locking
28
#!/usr/bin/perl
use strict;
use warnings;
use Fcntl;
my $file = 'testfile.txt';
# open the file
open (FILE, ">>", "$file") || die "problem opening $filen";
# immediately lock the file
flock (FILE, 2);
# test keeping the lock on the file for ~20 seconds
my $count = 0;
while ($count++ < 30)
{
print "count : $countn";
print FILE "count : $countn";
sleep 1;
}
# close the file, which also removes the lock
close (FILE);
exit(0);
FileExLockEx02.pl
Please run this concurrency
with FileExLockEx01.pl
Results:-
<see from the output file>
Danairat T.
File Locking – Shared Locking
29
#!/usr/bin/perl
use strict;
use warnings;
use Fcntl;
my $file = 'testfile.txt';
# open the file
open (FILE, "<", "$file") || die "problem opening $filen";
# immediately lock the file
flock (FILE, 1);
# test keeping the lock on the file for ~20 seconds
my $count = 0;
while ($count++ < 30)
{
print "Shared Lockingn";
sleep 1;
}
# close the file, which also removes the lock
close (FILE);
exit(0);
FileShLockEx01.pl
Please run this concurrency
with FileExLockEx02.pl
Results:-
<see from the output file>
Danairat T.
Read Files from Command Line
• The command line argument will be stored in
@ARGV as default or you may straight away using <>
for file read.
• The below codes is to read file from command line
argument and print their content to the screen.
30
#!/usr/bin/perl -w
while (<>) { print $_; }
Danairat T.
Read Files from Command Line
31
#!/usr/bin/perl
use strict;
use warnings;
my $lineNo = 1;
# read values from @ARGV
foreach my $myFile (@ARGV) {
# read each file
if (open (FILEREAD, "<", $myFile)) {
flock (FILEREAD,1);
print "====Start $myFile====nn";
while (my $myLine = <FILEREAD>) {
chomp($myLine);
print "$lineNo: $myLine";
print "n";
$lineNo++;
}
close (FILEREAD);
} else {
print "Failed to open $myFilenn";
}
}
exit(0);
01_ReadFilesFromARGVEx01.pl
perl 01_ReadFilesFromARGVEx01.pl data01.dat data02.dat
Please see the result screen.
Danairat T.
Read File as a whole into Array
• Perl allows you to read file into array in a single
statement using = (assign) operator.
32
open (FILEREAD, $myFile);
@myLines = <FILEREAD>;
close (FILEREAD);
Danairat T.
Read File as a whole into Array
33
#!/usr/bin/perl
use strict;
use warnings;
# read values from @ARGV
foreach my $myFile (@ARGV) {
# read each file
die "Failed to open $myFilenn" unless (open (FILEREAD, "<", $myFile));
flock (FILEREAD,1);
print "====Start $myFile====nn";
my @myLines = <FILEREAD>; # read file into single array
close(FILEREAD);
my $lineNo = 1;
foreach my $myLine (@myLines) {
chomp($myLine);
print $lineNo++ . ": ". $myLine . "n";
}
}
exit(0);
02_ReadFilesIntoArrayEx01.pl
perl 02_ReadFilesIntoArrayEx01.pl data01.dat data02.dat
Please see the result screen.
Danairat T.
Read File as a whole into Scalar
• Perl provides you the quick way to read file into a
single scalar variable and process in your program
structure. Eg. You may need to read many input files
and append to one output file.
• $/ is a file record separator. Default is n. You need to
turn it off otherwise you can only read just one line.
34
undef $/; # undefine the file record separator
my $myLines = <FILEREAD>;
Danairat T.
Read File as a whole into Scalar
35
#!/usr/bin/perl
use strict;
use warnings;
# read values from @ARGV
foreach my $myFile (@ARGV) {
# read each file
die "Failed to open $myFilenn" unless (open (FILEREAD, "<", $myFile));
flock (FILEREAD,1);
print "====Start $myFile====nn";
undef $/; # undefine the file record separator
my $myLines = <FILEREAD>;
close(FILEREAD);
print "$myLinesn";
}
exit(0);
03_ReadFilesIntoScalarEx01.pl
perl 03_ReadFilesIntoScalarEx01.pl data01.dat data02.dat
Please see the result screen.
Danairat T.
Read File as Paragraph
• To read file paragraph by paragraph, you need to
assign “” the empty string to the file record separator
$/ for perl to detect the blank line as a break of
elements until match with the next line content
36
$/ = ""; # assign empty string to the file record separator
my @myParagraphs = <FILEREAD>;
Danairat T.
Read File as Paragraph
37
#!/usr/bin/perl
use strict;
use warnings;
# read values from @ARGV
foreach my $myFile (@ARGV) {
# read each file
die "Failed to open $myFilenn" unless (open (FILEREAD, "<", $myFile));
flock (FILEREAD,1);
print "====Start $myFile====nn";
$/ = ""; # assign empty string to the file record separator
my @myParagraphs = <FILEREAD>;
close(FILEREAD);
my $paragraphNo = 1;
foreach my $myParagraph (@myParagraphs ) {
chomp($myParagraph);
print "====Paragraph " . $paragraphNo++ . "====n" . $myParagraph . "n";
}
}
exit(0);
04_ReadFilesParagraphEx01.pl
perl 04_ReadFilesParagraphEx01.pl data01.dat data02.dat
Please see the result screen.
Danairat T.
Read many files in the Directory
• Perl allow you to open the directory using:-
– opendir (MYDIR, “/mydirectory”);
• To read the item from directory, using:-
– readdir (MYDIR);
• To close the directory, using:-
– closedir (MYDIR, “/mydirectory”);
38
#!/usr/bin/perl –w
opendir(DIR, '.') or die "Couldn't open directory, $!";
foreach (sort grep(/^.*.txt$/,readdir(DIR))) {
print "$_n";
}
closedir (DIR);
Danairat T.
Read Files in the Directory
39
#!/usr/bin/perl -w
use strict;
my $dir = '.';
opendir(DIR, $dir) or die $!;
while (my $file = readdir(DIR)) {
# We only want files
next unless (-f "$dir/$file");
# Use a regular expression to find files ending with .txt
next unless ($file =~ /.txt$/);
print "$filen"; # you can open the file here
}
closedir(DIR);
exit(0);
05_ReadFilesFromDirectory.pl
Please see the result screen.
Danairat T.
Read SubDir in the Directory
40
#!/usr/bin/perl -w
use strict;
my $dir = '../';
opendir(DIR, $dir) or die $!;
while (my $subDirName = readdir(DIR)) {
next unless (-d "$dir/$subDirName");
print "$subDirName";
}
closedir(DIR);
exit 0;
06_ReadSubDirFromDirectory.pl
Please see the result screen.
Danairat T.
Read File Recursively in Directory
1. The Cwd module for you to find the current
working directory.
2. The chdir() function is to use when you want
to change “perl program working directory”
to the another directory.
3. The recursive programming provides the
repeat of the same action with keeping your
history variables in the program stack and
automatically pop it up to complete the
program
41
Danairat T.
Read File Recursively in Directory
42
#!/usr/bin/perl -w
use strict;
use Cwd; # module for finding the current working directory
my $myDirectory = '../../';
ScanDirectory($myDirectory);
exit 0;
sub ScanDirectory {
my $workdir = shift; # same as shift(@_)
my $startdir = cwd; # keep track of where we began
chdir $workdir or die "Unable to enter dir $workdir: $!n";
opendir (DIR, '.') or die "Unable to open $workdir: $!n";
my @names = readdir (DIR) or die "Unable to read $workdir: $!n";
closedir (DIR);
foreach my $name (@names) {
next if ( $name eq '.' );
next if ( $name eq '..' );
if ( -d $name ) { # is this a directory?
ScanDirectory($name);
next;
}
if ( $name =~ /.txt$/i ) { # is this a file named "core"?
print "$startdir/$workdir/$name n";
}
}
chdir $startdir or die "Unable to change to dir $startdir: $!n";
}
07_ReadFilesFromDirectoryRecursive_Ex01.pl
Please see the result screen.
Danairat T.
Line ID: Danairat
FB: Danairat Thanabodithammachari
+668-1559-1446
Thank you

Weitere ähnliche Inhalte

Was ist angesagt?

Unix - Filters/Editors
Unix - Filters/EditorsUnix - Filters/Editors
Unix - Filters/Editors
ananthimurugesan
 

Was ist angesagt? (20)

Course 102: Lecture 6: Seeking Help
Course 102: Lecture 6: Seeking HelpCourse 102: Lecture 6: Seeking Help
Course 102: Lecture 6: Seeking Help
 
Linux networking
Linux networkingLinux networking
Linux networking
 
Linux Fundamentals
Linux FundamentalsLinux Fundamentals
Linux Fundamentals
 
System Programming and Administration
System Programming and AdministrationSystem Programming and Administration
System Programming and Administration
 
Course 102: Lecture 7: Simple Utilities
Course 102: Lecture 7: Simple Utilities Course 102: Lecture 7: Simple Utilities
Course 102: Lecture 7: Simple Utilities
 
2015 bioinformatics python_io_wim_vancriekinge
2015 bioinformatics python_io_wim_vancriekinge2015 bioinformatics python_io_wim_vancriekinge
2015 bioinformatics python_io_wim_vancriekinge
 
Hive data migration (export/import)
Hive data migration (export/import)Hive data migration (export/import)
Hive data migration (export/import)
 
Ansible for Beginners
Ansible for BeginnersAnsible for Beginners
Ansible for Beginners
 
Linux Network commands
Linux Network commandsLinux Network commands
Linux Network commands
 
Course 102: Lecture 3: Basic Concepts And Commands
Course 102: Lecture 3: Basic Concepts And Commands Course 102: Lecture 3: Basic Concepts And Commands
Course 102: Lecture 3: Basic Concepts And Commands
 
Linux Shell Basics
Linux Shell BasicsLinux Shell Basics
Linux Shell Basics
 
Centralized + Unified Logging
Centralized + Unified LoggingCentralized + Unified Logging
Centralized + Unified Logging
 
Sahul
SahulSahul
Sahul
 
(Practical) linux 104
(Practical) linux 104(Practical) linux 104
(Practical) linux 104
 
Unix - Filters/Editors
Unix - Filters/EditorsUnix - Filters/Editors
Unix - Filters/Editors
 
(Practical) linux 101
(Practical) linux 101(Practical) linux 101
(Practical) linux 101
 
Linux basic for CADD biologist
Linux basic for CADD biologistLinux basic for CADD biologist
Linux basic for CADD biologist
 
linux-commandline-magic-Joomla-World-Conference-2014
linux-commandline-magic-Joomla-World-Conference-2014linux-commandline-magic-Joomla-World-Conference-2014
linux-commandline-magic-Joomla-World-Conference-2014
 
Course 102: Lecture 12: Basic Text Handling
Course 102: Lecture 12: Basic Text Handling Course 102: Lecture 12: Basic Text Handling
Course 102: Lecture 12: Basic Text Handling
 
Basic linux commands
Basic linux commands Basic linux commands
Basic linux commands
 

Andere mochten auch

The Business value of agile development
The Business value of agile developmentThe Business value of agile development
The Business value of agile development
Phavadol Srisarnsakul
 

Andere mochten auch (16)

JEE Programming - 03 Model View Controller
JEE Programming - 03 Model View ControllerJEE Programming - 03 Model View Controller
JEE Programming - 03 Model View Controller
 
Setting up Hadoop YARN Clustering
Setting up Hadoop YARN ClusteringSetting up Hadoop YARN Clustering
Setting up Hadoop YARN Clustering
 
Big data Hadoop Analytic and Data warehouse comparison guide
Big data Hadoop Analytic and Data warehouse comparison guideBig data Hadoop Analytic and Data warehouse comparison guide
Big data Hadoop Analytic and Data warehouse comparison guide
 
Digital Transformation, Enterprise Architecture, Big Data by Danairat
Digital Transformation, Enterprise Architecture, Big Data by DanairatDigital Transformation, Enterprise Architecture, Big Data by Danairat
Digital Transformation, Enterprise Architecture, Big Data by Danairat
 
The Business value of agile development
The Business value of agile developmentThe Business value of agile development
The Business value of agile development
 
The Face of the New Enterprise
The Face of the New EnterpriseThe Face of the New Enterprise
The Face of the New Enterprise
 
Glassfish JEE Server Administration - The Enterprise Server
Glassfish JEE Server Administration - The Enterprise ServerGlassfish JEE Server Administration - The Enterprise Server
Glassfish JEE Server Administration - The Enterprise Server
 
IBM Cognos Analytics: Empowering business by infusing intelligence across the...
IBM Cognos Analytics: Empowering business by infusing intelligence across the...IBM Cognos Analytics: Empowering business by infusing intelligence across the...
IBM Cognos Analytics: Empowering business by infusing intelligence across the...
 
JEE Programming - 05 JSP
JEE Programming - 05 JSPJEE Programming - 05 JSP
JEE Programming - 05 JSP
 
A Guide to IT Consulting- Business.com
A Guide to IT Consulting- Business.comA Guide to IT Consulting- Business.com
A Guide to IT Consulting- Business.com
 
JEE Programming - 01 Introduction
JEE Programming - 01 IntroductionJEE Programming - 01 Introduction
JEE Programming - 01 Introduction
 
JEE Programming - 06 Web Application Deployment
JEE Programming - 06 Web Application DeploymentJEE Programming - 06 Web Application Deployment
JEE Programming - 06 Web Application Deployment
 
JEE Programming - 02 The Containers
JEE Programming - 02 The ContainersJEE Programming - 02 The Containers
JEE Programming - 02 The Containers
 
JEE Programming - 08 Enterprise Application Deployment
JEE Programming - 08 Enterprise Application DeploymentJEE Programming - 08 Enterprise Application Deployment
JEE Programming - 08 Enterprise Application Deployment
 
Glassfish JEE Server Administration - JEE Introduction
Glassfish JEE Server Administration - JEE IntroductionGlassfish JEE Server Administration - JEE Introduction
Glassfish JEE Server Administration - JEE Introduction
 
Strategic IT Consulting
Strategic IT ConsultingStrategic IT Consulting
Strategic IT Consulting
 

Ähnlich wie Perl for System Automation - 01 Advanced File Processing

Introduction to Unix
Introduction to UnixIntroduction to Unix
Introduction to Unix
Sudharsan S
 
Linux Presentation
Linux PresentationLinux Presentation
Linux Presentation
Muhammad Qazi
 
Linuxtraining 130710022121-phpapp01
Linuxtraining 130710022121-phpapp01Linuxtraining 130710022121-phpapp01
Linuxtraining 130710022121-phpapp01
Chander Pandey
 
Introduction to-linux
Introduction to-linuxIntroduction to-linux
Introduction to-linux
rowiebornia
 
Introduction khgjkhygkjiyhgikjyhgikygkii
Introduction khgjkhygkjiyhgikjyhgikygkiiIntroduction khgjkhygkjiyhgikjyhgikygkii
Introduction khgjkhygkjiyhgikjyhgikygkii
cmdept1
 

Ähnlich wie Perl for System Automation - 01 Advanced File Processing (20)

Unix
UnixUnix
Unix
 
Introduction to UNIX
Introduction to UNIXIntroduction to UNIX
Introduction to UNIX
 
Karkha unix shell scritping
Karkha unix shell scritpingKarkha unix shell scritping
Karkha unix shell scritping
 
File management
File managementFile management
File management
 
Linux
LinuxLinux
Linux
 
Introduction to Unix
Introduction to UnixIntroduction to Unix
Introduction to Unix
 
lec1.docx
lec1.docxlec1.docx
lec1.docx
 
Writing Character driver (loadable module) in linux
Writing Character driver (loadable module) in linuxWriting Character driver (loadable module) in linux
Writing Character driver (loadable module) in linux
 
Raspberry Pi - Lecture 2 Linux OS
Raspberry Pi - Lecture 2 Linux OSRaspberry Pi - Lecture 2 Linux OS
Raspberry Pi - Lecture 2 Linux OS
 
Linux Presentation
Linux PresentationLinux Presentation
Linux Presentation
 
Linuxtraining 130710022121-phpapp01
Linuxtraining 130710022121-phpapp01Linuxtraining 130710022121-phpapp01
Linuxtraining 130710022121-phpapp01
 
UNIX Basics and Cluster Computing
UNIX Basics and Cluster ComputingUNIX Basics and Cluster Computing
UNIX Basics and Cluster Computing
 
Introduction-to-Linux.pptx
Introduction-to-Linux.pptxIntroduction-to-Linux.pptx
Introduction-to-Linux.pptx
 
Introduction to-linux
Introduction to-linuxIntroduction to-linux
Introduction to-linux
 
Introduction-to-Linux.pptx
Introduction-to-Linux.pptxIntroduction-to-Linux.pptx
Introduction-to-Linux.pptx
 
Introduction khgjkhygkjiyhgikjyhgikygkii
Introduction khgjkhygkjiyhgikjyhgikygkiiIntroduction khgjkhygkjiyhgikjyhgikygkii
Introduction khgjkhygkjiyhgikjyhgikygkii
 
Unix Shell Script - 2 Days Session.pptx
Unix Shell Script - 2 Days Session.pptxUnix Shell Script - 2 Days Session.pptx
Unix Shell Script - 2 Days Session.pptx
 
Linux ppt
Linux pptLinux ppt
Linux ppt
 
Unix
UnixUnix
Unix
 
Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式
 

Mehr von Danairat Thanabodithammachari

Mehr von Danairat Thanabodithammachari (16)

Thailand State Enterprise - Business Architecture and SE-AM
Thailand State Enterprise - Business Architecture and SE-AMThailand State Enterprise - Business Architecture and SE-AM
Thailand State Enterprise - Business Architecture and SE-AM
 
Agile Management
Agile ManagementAgile Management
Agile Management
 
Agile Organization and Enterprise Architecture v1129 Danairat
Agile Organization and Enterprise Architecture v1129 DanairatAgile Organization and Enterprise Architecture v1129 Danairat
Agile Organization and Enterprise Architecture v1129 Danairat
 
Blockchain for Management
Blockchain for ManagementBlockchain for Management
Blockchain for Management
 
Enterprise Architecture and Agile Organization Management v1076 Danairat
Enterprise Architecture and Agile Organization Management v1076 DanairatEnterprise Architecture and Agile Organization Management v1076 Danairat
Enterprise Architecture and Agile Organization Management v1076 Danairat
 
Agile Enterprise Architecture - Danairat
Agile Enterprise Architecture - DanairatAgile Enterprise Architecture - Danairat
Agile Enterprise Architecture - Danairat
 
Big data hadooop analytic and data warehouse comparison guide
Big data hadooop analytic and data warehouse comparison guideBig data hadooop analytic and data warehouse comparison guide
Big data hadooop analytic and data warehouse comparison guide
 
Perl Programming - 02 Regular Expression
Perl Programming - 02 Regular ExpressionPerl Programming - 02 Regular Expression
Perl Programming - 02 Regular Expression
 
JEE Programming - 04 Java Servlets
JEE Programming - 04 Java ServletsJEE Programming - 04 Java Servlets
JEE Programming - 04 Java Servlets
 
JEE Programming - 07 EJB Programming
JEE Programming - 07 EJB ProgrammingJEE Programming - 07 EJB Programming
JEE Programming - 07 EJB Programming
 
Glassfish JEE Server Administration - Clustering
Glassfish JEE Server Administration - ClusteringGlassfish JEE Server Administration - Clustering
Glassfish JEE Server Administration - Clustering
 
Glassfish JEE Server Administration - Module 4 Load Balancer
Glassfish JEE Server Administration - Module 4 Load BalancerGlassfish JEE Server Administration - Module 4 Load Balancer
Glassfish JEE Server Administration - Module 4 Load Balancer
 
Java Programming - 07 java networking
Java Programming - 07 java networkingJava Programming - 07 java networking
Java Programming - 07 java networking
 
Java Programming - 08 java threading
Java Programming - 08 java threadingJava Programming - 08 java threading
Java Programming - 08 java threading
 
Java Programming - 06 java file io
Java Programming - 06 java file ioJava Programming - 06 java file io
Java Programming - 06 java file io
 
Java Programming - 05 access control in java
Java Programming - 05 access control in javaJava Programming - 05 access control in java
Java Programming - 05 access control in java
 

KĂźrzlich hochgeladen

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 

KĂźrzlich hochgeladen (20)

Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 

Perl for System Automation - 01 Advanced File Processing

  • 1. Perl for System Automation 1 Danairat T. Line ID: Danairat FB: Danairat Thanabodithammachari +668-1559-1446
  • 2. Danairat T. Contents Module-1: Advanced File Processing with Perl • Types of open Filehandles and File Locking • Read/Write File line by line – Read Line – Write Line – Append Line • Lock File • Read entire files into Array • Read entire files into Scalar using file record separator • Read file paragraph by paragraph using file record separator • Read files in the Directory • Read SubDir in the Directory • Read files recursively in Directory
  • 3. Danairat T. Contents Module-2: System Interaction • Enumerating OS user identity • Enumerating OS group identity • Activating System Command – Using exec() – Using system() • Grabbing a program's output – Using qx() • Display current running processes – Using ProcessTable module • Terminate the Process – Built in kill() function
  • 4. Danairat T. Contents Module-3: Managing Networks with Perl • Host file • IP Lookup • FTP • E-mail • Web • LDAP
  • 5. Danairat T. Contents Module-4: Perl Coding Guidelines • Perl Coding Style Guides – The 12 general guidelines – Don’t do in perl codes – To avoid in perl codes – Performance coding guidelines • Perl Coding Guides for Security – Command line inputs – The exec(), system(), qw() and Perl modules.
  • 6. Danairat T. What is Perl? (Practical Extraction and Report Language ) • Perl, release in 1987, is a high-level programming language written by Larry Wall. Perl's process, file, and text manipulation facilities make it particularly well-suited for tasks system utilities, system management tasks, database access, networking. These strengths make it especially popular with system administrators. 6
  • 7. Danairat T. Which version of Perl should I use? • No one is actively supporting Perl 4. Five years ago it was a dead camel carcass • There is no Perl 6 release scheduled. • The immediate releases are Perl 5.8 (i.e. Perl5.8.x ) 7
  • 8. Danairat T. OS Platforms Support • UNIX: More or less every UNIX or UNIX-like operating system ever created, notably Linux , Solaris, AIX, IRIX, HP/UX, BSD, and Tru64 • MS Windows: DOS, Windows 3.1, 95, 98, NT and 2000 • Other Desktop OSs: Apple Macintosh (68k and PPC, both pre and post MacOS X), Acorn Risc OS, Amiga, BeOS, OS/2, and many others • Mainframes: AS/400, OS390, VMS and OpenVMS, Stratus (VOS), and Tandem • PDAs: EPOC (Psion/Symbian), but not PalmOS or Windows CE at time of writing, although porting efforts are being made for both platforms 8
  • 9. Danairat T. Identify the Perl version • perl –v • perl -V 9
  • 10. Danairat T. www.CPAN.org • CPAN stands for Comprehensive Perl Archive Network, containing libraries, documentation, and many Perl third-party modules and extensions 10
  • 11. Danairat T. Install Perl • On Unix System – pkgadd -d /cdrom/sol_10_1009_x86/Solaris_10/Product SUNWperl584core – pkgadd –d /cdrom/sol_10_1009_x86/Solaris_10/Product SUNWperl584usr – pkgadd –d /cdrom/sol_10_1009_x86/Solaris_10/Product SUNWperl584man • On Windows System – run ActivePerl-5.8.9.826-MSWin32-x86-290470.msi Available for download at www.activestate.com 11
  • 12. Danairat T. Perl file structure and location • Perl executable: /usr/perl5/bin/perl or /usr/bin/perl • Perl Pain Old Documentation: /usr/perl5/pod • Perl Manual: /usr/perl5/man 12
  • 13. Danairat T. Perl Libraries (Perl Modules) • The Perl Libraries directories – /usr/perl5/5.8.4/lib/i86pc-solaris-64int – /usr/perl5/5.8.4/lib – /usr/perl5/site_perl/5.8.4/i86pc-solaris-64int – /usr/perl5/site_perl/5.8.4 – /usr/perl5/site_perl – /usr/perl5/vendor_perl/5.8.4/i86pc-solaris-64int – /usr/perl5/vendor_perl/5.8.4 – /usr/perl5/vendor_perl 13
  • 14. Danairat T. Perl Library (The modules for Unix) http://www.cpan.org/modules/01modules.index.html 14
  • 15. Danairat T. Perl Library (PPM modules for windows) http://ppm.activestate.com/PPMPackages/zips/8xx-builds-only/Windows/ 15
  • 16. Danairat T. Install Perl Modules on Unix gzip -dc yourmodule.tar.gz | tar -xof - cd /your/new/lib/to/install/ perl Makefile.PL make make test make install Or, if you have internet access perl -MCPAN -e shell cpan> i /Time/ cpan> install Time::CTime 16
  • 17. Danairat T. Install Perl Modules on Unix bash-3.00# cd /tmp/Class-Simple-0.19 bash-3.00# perl Makefile.PL Checking if your kit is complete... Looks good Writing Makefile for Class::Simple bash-3.00# make cp lib/Class/Simple.pm blib/lib/Class/Simple.pm Manifying blib/man3/Class::Simple.3 bash-3.00# make test PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t t/anon.............ok t/attributes.......ok t/Class-Simple.....ok t/inherit..........ok t/isa..............ok t/nonew............ok t/override.........ok All tests successful. Files=13, Tests=95, 3 wallclock secs ( 0.99 cusr + 0.52 csys = 1.51 CPU) bash-3.00# make install Installing /usr/perl5/site_perl/5.8.4/Class/Simple.pm Installing /usr/perl5/5.8.4/man/man3/Class::Simple.3 Writing /usr/perl5/site_perl/5.8.4/i86pc-solaris-64int/auto/Class/Simple/.packli st Appending installation info to /usr/perl5/5.8.4/lib/i86pc-solaris-64int/perlloca l.pod bash-3.00# Example:- 17
  • 18. Danairat T. Installing Perl modules on Sun Solaris 10 1. Login as root 2. Add path /usr/sfw/bin to /.profile 3. Download and unzip the Perl module then cd into its directory 4. Follow the below command steps:- – /usr/perl5/bin/perlgcc Makefile.PL – gmake – gmake test – gmake install 18
  • 19. Danairat T. Install Perl Module on Windows 1. Unzip the lib file 2. Cd to the downloaded lib 3. ppm install yourdownloadedlib.ppd 19
  • 21. Danairat T. Topics • Types of open Filehandles and File Locking • Read/Write File line by line – Read Line – Write Line – Append Line • Lock File • Read entire files into Array • Read entire files into Scalar using file record separator • Read file paragraph by paragraph using file record separator • Read many files in a Directory • Read SubDir in the Directory • Read files recursively from Directories 21
  • 22. Danairat T. File Handle Options 22 mode operand create delete and recreate file if file exists read < write > ✓ ✓ append >> ✓ read/write +< read/write +> ✓ ✓ read/append +>> ✓
  • 23. Danairat T. Open File Options 23 • Using < for file reading. #!/usr/bin/perl use strict; use warnings; my $myFile = "fileread.txt"; # the file “filetest.txt” must be exist my $myLine; if (open (MYFILEHANDLE, '<' , $myFile)) { # using ‘<‘ and . for file read while ($myLine = <MYFILEHANDLE>) { # read line chomp($myLine); # trim whitespace at end of line print "$myLine n"; } close (MYFILEHANDLE); } else { print "File could not be opened. n"; } exit(0); OpenFileReadEx01.pl Results:- <print the file content>
  • 24. Danairat T. Open File Options 24 • Using > for file writing to new file. #!/usr/bin/perl use strict; use warnings; my $myFile = "filewrite.txt"; my @myData = ("line1", "line2", "line3"); if (open (MYFILEHANDLE, '>' , $myFile)) { foreach my $myLine (@myData) { print MYFILEHANDLE "$myLine n"; # print to filehandle } close (MYFILEHANDLE); } else { print "File could not be opened. n"; } exit(0); OpenFileWriteEx01.pl Results:- <see from the output file>
  • 25. Danairat T. Open File Options 25 • Using >> to append data to file. If the file does not exist then it is create a new file. #!/usr/bin/perl use strict; use warnings; my $myFile = "filewrite.txt"; my @myData = ("line4", "line5", "line6"); if (open (MYFILEHANDLE, ‘>>' , $myFile)) { foreach my $myLine (@myData) { print MYFILEHANDLE "$myLine n"; # print to filehandle } close (MYFILEHANDLE); } else { print "File could not be opened. n"; } exit(0); OpenFileAppendEx01.pl Results:- <see from the output file>
  • 26. Danairat T. File Locking • Lock File for Reading (shared lock): Allow other to open the file but no one can modify the file • Lock File for Writing (exclusive lock): NOT allow anyone to open the file either for reading or for writing • Unlock file is activated when close the file 26 Shared lock: 1 Exclusive lock: 2 Unlock: 8
  • 27. Danairat T. File Locking – Exclusive Locking 27 #!/usr/bin/perl use strict; use warnings; use Fcntl; my $file = 'testfile.txt'; # open the file open (FILE, ">>", "$file") || die "problem opening $filen"; # immediately lock the file flock (FILE, 2); # test keeping the lock on the file for ~20 seconds my $count = 0; while ($count++ < 30) { print "count = $countn"; print FILE "count = $countn"; sleep 1; } # close the file, which also removes the lock close (FILE); exit(0); FileExLockEx01.pl Please run this concurrence with FileExLockEx02.pl, see next page. Results:- <see from the output file>
  • 28. Danairat T. File Locking – Exclusive Locking 28 #!/usr/bin/perl use strict; use warnings; use Fcntl; my $file = 'testfile.txt'; # open the file open (FILE, ">>", "$file") || die "problem opening $filen"; # immediately lock the file flock (FILE, 2); # test keeping the lock on the file for ~20 seconds my $count = 0; while ($count++ < 30) { print "count : $countn"; print FILE "count : $countn"; sleep 1; } # close the file, which also removes the lock close (FILE); exit(0); FileExLockEx02.pl Please run this concurrency with FileExLockEx01.pl Results:- <see from the output file>
  • 29. Danairat T. File Locking – Shared Locking 29 #!/usr/bin/perl use strict; use warnings; use Fcntl; my $file = 'testfile.txt'; # open the file open (FILE, "<", "$file") || die "problem opening $filen"; # immediately lock the file flock (FILE, 1); # test keeping the lock on the file for ~20 seconds my $count = 0; while ($count++ < 30) { print "Shared Lockingn"; sleep 1; } # close the file, which also removes the lock close (FILE); exit(0); FileShLockEx01.pl Please run this concurrency with FileExLockEx02.pl Results:- <see from the output file>
  • 30. Danairat T. Read Files from Command Line • The command line argument will be stored in @ARGV as default or you may straight away using <> for file read. • The below codes is to read file from command line argument and print their content to the screen. 30 #!/usr/bin/perl -w while (<>) { print $_; }
  • 31. Danairat T. Read Files from Command Line 31 #!/usr/bin/perl use strict; use warnings; my $lineNo = 1; # read values from @ARGV foreach my $myFile (@ARGV) { # read each file if (open (FILEREAD, "<", $myFile)) { flock (FILEREAD,1); print "====Start $myFile====nn"; while (my $myLine = <FILEREAD>) { chomp($myLine); print "$lineNo: $myLine"; print "n"; $lineNo++; } close (FILEREAD); } else { print "Failed to open $myFilenn"; } } exit(0); 01_ReadFilesFromARGVEx01.pl perl 01_ReadFilesFromARGVEx01.pl data01.dat data02.dat Please see the result screen.
  • 32. Danairat T. Read File as a whole into Array • Perl allows you to read file into array in a single statement using = (assign) operator. 32 open (FILEREAD, $myFile); @myLines = <FILEREAD>; close (FILEREAD);
  • 33. Danairat T. Read File as a whole into Array 33 #!/usr/bin/perl use strict; use warnings; # read values from @ARGV foreach my $myFile (@ARGV) { # read each file die "Failed to open $myFilenn" unless (open (FILEREAD, "<", $myFile)); flock (FILEREAD,1); print "====Start $myFile====nn"; my @myLines = <FILEREAD>; # read file into single array close(FILEREAD); my $lineNo = 1; foreach my $myLine (@myLines) { chomp($myLine); print $lineNo++ . ": ". $myLine . "n"; } } exit(0); 02_ReadFilesIntoArrayEx01.pl perl 02_ReadFilesIntoArrayEx01.pl data01.dat data02.dat Please see the result screen.
  • 34. Danairat T. Read File as a whole into Scalar • Perl provides you the quick way to read file into a single scalar variable and process in your program structure. Eg. You may need to read many input files and append to one output file. • $/ is a file record separator. Default is n. You need to turn it off otherwise you can only read just one line. 34 undef $/; # undefine the file record separator my $myLines = <FILEREAD>;
  • 35. Danairat T. Read File as a whole into Scalar 35 #!/usr/bin/perl use strict; use warnings; # read values from @ARGV foreach my $myFile (@ARGV) { # read each file die "Failed to open $myFilenn" unless (open (FILEREAD, "<", $myFile)); flock (FILEREAD,1); print "====Start $myFile====nn"; undef $/; # undefine the file record separator my $myLines = <FILEREAD>; close(FILEREAD); print "$myLinesn"; } exit(0); 03_ReadFilesIntoScalarEx01.pl perl 03_ReadFilesIntoScalarEx01.pl data01.dat data02.dat Please see the result screen.
  • 36. Danairat T. Read File as Paragraph • To read file paragraph by paragraph, you need to assign “” the empty string to the file record separator $/ for perl to detect the blank line as a break of elements until match with the next line content 36 $/ = ""; # assign empty string to the file record separator my @myParagraphs = <FILEREAD>;
  • 37. Danairat T. Read File as Paragraph 37 #!/usr/bin/perl use strict; use warnings; # read values from @ARGV foreach my $myFile (@ARGV) { # read each file die "Failed to open $myFilenn" unless (open (FILEREAD, "<", $myFile)); flock (FILEREAD,1); print "====Start $myFile====nn"; $/ = ""; # assign empty string to the file record separator my @myParagraphs = <FILEREAD>; close(FILEREAD); my $paragraphNo = 1; foreach my $myParagraph (@myParagraphs ) { chomp($myParagraph); print "====Paragraph " . $paragraphNo++ . "====n" . $myParagraph . "n"; } } exit(0); 04_ReadFilesParagraphEx01.pl perl 04_ReadFilesParagraphEx01.pl data01.dat data02.dat Please see the result screen.
  • 38. Danairat T. Read many files in the Directory • Perl allow you to open the directory using:- – opendir (MYDIR, “/mydirectory”); • To read the item from directory, using:- – readdir (MYDIR); • To close the directory, using:- – closedir (MYDIR, “/mydirectory”); 38 #!/usr/bin/perl –w opendir(DIR, '.') or die "Couldn't open directory, $!"; foreach (sort grep(/^.*.txt$/,readdir(DIR))) { print "$_n"; } closedir (DIR);
  • 39. Danairat T. Read Files in the Directory 39 #!/usr/bin/perl -w use strict; my $dir = '.'; opendir(DIR, $dir) or die $!; while (my $file = readdir(DIR)) { # We only want files next unless (-f "$dir/$file"); # Use a regular expression to find files ending with .txt next unless ($file =~ /.txt$/); print "$filen"; # you can open the file here } closedir(DIR); exit(0); 05_ReadFilesFromDirectory.pl Please see the result screen.
  • 40. Danairat T. Read SubDir in the Directory 40 #!/usr/bin/perl -w use strict; my $dir = '../'; opendir(DIR, $dir) or die $!; while (my $subDirName = readdir(DIR)) { next unless (-d "$dir/$subDirName"); print "$subDirName"; } closedir(DIR); exit 0; 06_ReadSubDirFromDirectory.pl Please see the result screen.
  • 41. Danairat T. Read File Recursively in Directory 1. The Cwd module for you to find the current working directory. 2. The chdir() function is to use when you want to change “perl program working directory” to the another directory. 3. The recursive programming provides the repeat of the same action with keeping your history variables in the program stack and automatically pop it up to complete the program 41
  • 42. Danairat T. Read File Recursively in Directory 42 #!/usr/bin/perl -w use strict; use Cwd; # module for finding the current working directory my $myDirectory = '../../'; ScanDirectory($myDirectory); exit 0; sub ScanDirectory { my $workdir = shift; # same as shift(@_) my $startdir = cwd; # keep track of where we began chdir $workdir or die "Unable to enter dir $workdir: $!n"; opendir (DIR, '.') or die "Unable to open $workdir: $!n"; my @names = readdir (DIR) or die "Unable to read $workdir: $!n"; closedir (DIR); foreach my $name (@names) { next if ( $name eq '.' ); next if ( $name eq '..' ); if ( -d $name ) { # is this a directory? ScanDirectory($name); next; } if ( $name =~ /.txt$/i ) { # is this a file named "core"? print "$startdir/$workdir/$name n"; } } chdir $startdir or die "Unable to change to dir $startdir: $!n"; } 07_ReadFilesFromDirectoryRecursive_Ex01.pl Please see the result screen.
  • 43. Danairat T. Line ID: Danairat FB: Danairat Thanabodithammachari +668-1559-1446 Thank you