Perl Debug is simple tool to easily debug perl scripts.
Few people like me find it difficult using Debug, but to be frank this is quite simple and straight forward. It makes the life of programmer easy to trace the bugs in a fast mode.
Without this, programmer needs to write unnecessary Print statements inside code and check where exactly things going wrong.
If you invoke Perl with the -d switch, your script runs under the Perl source debugger.
This works like an interactive Perl environment, prompting for debugger commands that let you examine source code, set breakpoints, dynamically pass the values of variables, etc.
This is so convenient that you often fire up the debugger all by itself just to test out Perl constructs interactively.
Debug Interactively:
Andrew E. Page, written an useful CPAN module Devel::Ptkdb, using this we can debug interactively.
This makes life much easier, no need to use <debug> prompt instead one can use the interactive mode to set the break points and pass the values to arguments dynamically.
How to Debug Perl Script Interactively using above module
perl -d:ptkdb myscript.pl (for graphical representation of debugging)
Normal Debugger
perl debugger works using -d switch
Once you execute script using -d switch, it will change to debug prompt as mentioned below :
DB<1> ....
DB<2> ....
Some commonly used commands in Debug mode:
1) "s" (Stepping through line by line execution)
Keep pressing "s" will execute line by line
2) "l" (List line)
e.g.,
l 20 #Lists line 20
l 20-25 #Lists 20 to 25 lines of script
l <subroutine> #Lists a sub-routine
3) "b" (Setting break point on subroutine)
"b" <subroutine>
#This will allow you to set a break point (set a break point on first line of subroutine)
4) "c" <subrutine>
#This will take you directly to that break point (set one-time bkpt at subname and continue)
5) "q"
#It will quit the debugger
PySpark, BigData, SQL, Hive, AWS, Python, Unix/Linux, Shortcuts, Examples, Scripts, Perl
Showing posts with label cpan modules. Show all posts
Showing posts with label cpan modules. Show all posts
Jul 2, 2013
Perl Debug
Labels:
cpan modules,
debug,
Devel::Ptkdb,
perl,
perl_modules
Apr 27, 2013
How to create excel report in Perl
We can generate Microsoft Excel (XLS) report using Perl
Note:
Please install module Spreadsheet::WriteExcel before running the program
We can create worksheets
We can set styles like color/font/size etc for the cells
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use Spreadsheet::WriteExcel;
my ($xls_label, $workbook, $worksheet, $format_header, $format_row);
$xls_label = "test.xls";
$workbook = Spreadsheet::WriteExcel->new($xls_label);
$worksheet = $workbook->add_worksheet();
$format_header = $workbook->add_format(); # Add a format
$format_header->set_bold();
$format_header->set_color('purple');
$format_header->set_align('center');
$format_header->set_size(12);
$format_row = $workbook->add_format(); # Add a format
$format_header->set_bold();
$format_row->set_align('center');
my $row = 0;
my $column = 0;
$worksheet->write($row, 0, 'S.No', $format_header);
$worksheet->write($row, 1, 'Humanists', $format_header);
my @humanists_arr = ("Mother Teresa", "Mahatma Gandhi", "Abraham Lincoln", "Winston Churchil", "Alfred Nobel");
foreach my $each_humanist (@humanists_arr) {
$row++;
$worksheet->write($row, 0, $row, $format_row);
$worksheet->write($row, 1, $each_humanist, $format_row);
}
1;
Output :
S.No Humanists 1 Mother Teresa 2 Mahatma Gandhi 3 Abraham Lincoln 4 Winston Churchil 5 Alfred Nobel
Please refer to other topics on Unix like :
Unix Delete Duplicated Lines in a File
Unix Unique Lines in a File
Unix Grep Examples
Unix Cut Command Examples
Search a Directory in Unix
Unix For Loop
pushd & popd in Unix
Find Size of Directory
Word Count
Please refer to other topics on AWK like :
Awk Examples
Print First Two Columns of File
Print Last Two Columns of File
Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python
Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension
Please refer to other topics on File Concepts like :
Print File Content in Python
Print File in Reverse Order in Python
Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl
You might also wish to read other topics like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton
Labels:
advanced perl,
cpan modules,
perl_script,
Spreadsheet::WriteExcel,
xls
Sep 13, 2012
Mod Perl Notes
mod-perl:
##########
http://www.perl.com/pub/2002/02/26/whatismodperl.html
http://www.perl.com/pub/2002/03/22/modperl.html
Having the Perl interpreter embedded in the server saves the very considerable overhead of starting an external interpreter for any HTTP request that needs to run Perl code.
At least as important is code caching: the modules and scripts are loaded and compiled only once, when the server is first started. Then for the rest of the server's life the scripts are served from the cache, so the server only has to run the pre-compiled code. In many cases, this is as fast as running compiled C programs.
The primary advantages of mod_perl are power and speed
You have full access to the inner workings of the Web server and you can intervene at any stage of HTTP request processing
There are big savings in startup and compilation times.
Difference between Apache::Registry, Apache::PerlRun ?
################################################################
Speed Wise :
Apache::Registry > Apache::PerlRun > Perl CGI
The Apache::PerlRun Class
###########################
The Apache::PerlRun handler is intended for Perl CGI scripts that depend strongly on the traditional
one-process-per-execution CGI model and cannot deal with being invoked repeatedly in the same process.
For example,
a script that depends on a lot of global variables being uninitialized when it starts up is unlikely to work properly under Apache::Registry.
Like Apache::Registry, Apache::PerlRun manages a directory of CGI scripts, launching them when they are requested.
However, unlike Apache::Registry, Apache::PerlRun module does not cache compiled scripts between runs. A script is loaded and compiled freshly each time it is requested.
However, Apache::PerlRun still avoids the overhead of starting a new Perl interpreter for each CGI script,
so it's faster than traditional Perl CGI scripting but slower than Apache::Registry or vanilla Apache API modules.
It offers a possible upgrade path for CGI scripts: move the script to Apache::PerlRun initially to get a modest performance bump.
This gives you time to rework the script to make it globally clean so that it can run under Apache::Registry for the full performance benefit.
The configuration section for running Apache::PerlRun is similar to Apache::Registry:
Alias /perl-run/ /home/www/perl-run/
<Location /perl>
SetHandler perl-script
PerlHandler Apache::PerlRun
Options +ExecCGI
# optional
PerlSendHeader On
</Location>
The Apache::PerlRun handler is only a small part of the picture. The rest of the Apache::PerlRun class provides subclassable methods that implement the functionality of Apache::Registry.
The Apache::PerlRun handler simply uses a subset of these methods; other modules may override certain methods to implement the Apache::Registry enviroment with a few twists. However, these Apache::PerlRun class methods were not fully defined when this book was going to press.
The Apache::Registry Class
##############################
The Apache::Registry class is essentially a CGI environment emulator that allows many CGI scripts to run without modification under mod_perl. Because there are many differences between CGI and the Apache API, Apache::Registry has to do a great deal of work to accomplish this sleight of hand.
It loads the scripts in its designated directory, compiles them, and stores them persistently in a memory structure. Before Apache::Registry runs a script, mod_perl will set up the various CGI environment variables, provided PerlSetupEnv is configured to On, which is the default.
When the PerlSendHeader directive is On, mod_perl monitors the text printed by the script, intercepts the HTTP header, and passes it through send_cgi_header(). It also arranges for STDIN to be read from the request object when the script attempts to process POST data.
Apache::Registry also monitors the modification dates of the script files it is responsible for and reloads them if their timestamp indicates they have been changed more recently than when they were last compiled.
Despite its complexity, Apache::Registry is easy to set up. The standard configuration consists of an Alias directive and a <Location> section:
Alias /perl/ /home/www/perl
<Location /perl>
SetHandler perl-script
PerlHandler Apache::Registry
Options +ExecCGI
# optional
PerlSendHeader On
</Location>
After restarting the server, you can place any (well, almost any) Perl CGI script into /home/www/perl (or the location of your choice) and make it executable. It runs just like an ordinary CGI script but will load much faster. The behavior of Apache::Registry can be tuned with the following directives:
PerlTaintCheck
When set to On, mod_perl will activate Perl taint checks on all the scripts under its control. Taint checks cause Perl to die with a fatal error if unchecked user-provided data (such as the values of CGI variables) is passed to a potentially dangerous function, such as exec(), eval(), or system().
PerlSendHeader
When set to On, mod_perl will scan for script output that looks like an HTTP header and automatically call send_http_header(). Scripts that send header information using CGI. pm's header() function do not need to activate PerlSendHeader. While scripts that use CGI.pm's header() will still function properly with PerlSendHeader On, turning it Off will save a few CPU cycles.
PerlFreshRestart
If PerlFreshRestart is set to On, mod_perl will flush its cache and reload all scripts when the server is restarted. This is very useful during module development to immediately see the changes to the source code take effect.
PerlWarn
If the script mentions the -w switch on its #! line, Apache::Registry will turn Perl warnings on by setting the $^W global to a nonzero value. The Perl-Warn directive can be configured to On to turn on warnings for all code inside the server.
Apache::Registry has several debug levels which write various informational messages to the server error log.
Apache::Registry scripts can change the debug level by importing Apache::Debug with its level pragma:
use Apache::Debug level => $level;
The debug level is a bit mask generated by ORing together some combination of the following values:
1 Make a note in the error log whenever the module is recompiled
2 Call Apache::Debug::dump() on errors
4 Turn on verbose tracing
The current value of the debug level can be found in the package global $Apache::Registry::Debug. You should not set this value directly, however. See Chapter 2, A First Module, for more hints on debugging Apache::Registry scripts.
===============================================================================================================
##########
http://www.perl.com/pub/2002/02/26/whatismodperl.html
http://www.perl.com/pub/2002/03/22/modperl.html
Having the Perl interpreter embedded in the server saves the very considerable overhead of starting an external interpreter for any HTTP request that needs to run Perl code.
At least as important is code caching: the modules and scripts are loaded and compiled only once, when the server is first started. Then for the rest of the server's life the scripts are served from the cache, so the server only has to run the pre-compiled code. In many cases, this is as fast as running compiled C programs.
The primary advantages of mod_perl are power and speed
You have full access to the inner workings of the Web server and you can intervene at any stage of HTTP request processing
There are big savings in startup and compilation times.
Difference between Apache::Registry, Apache::PerlRun ?
################################################################
Speed Wise :
Apache::Registry > Apache::PerlRun > Perl CGI
The Apache::PerlRun Class
###########################
The Apache::PerlRun handler is intended for Perl CGI scripts that depend strongly on the traditional
one-process-per-execution CGI model and cannot deal with being invoked repeatedly in the same process.
For example,
a script that depends on a lot of global variables being uninitialized when it starts up is unlikely to work properly under Apache::Registry.
Like Apache::Registry, Apache::PerlRun manages a directory of CGI scripts, launching them when they are requested.
However, unlike Apache::Registry, Apache::PerlRun module does not cache compiled scripts between runs. A script is loaded and compiled freshly each time it is requested.
However, Apache::PerlRun still avoids the overhead of starting a new Perl interpreter for each CGI script,
so it's faster than traditional Perl CGI scripting but slower than Apache::Registry or vanilla Apache API modules.
It offers a possible upgrade path for CGI scripts: move the script to Apache::PerlRun initially to get a modest performance bump.
This gives you time to rework the script to make it globally clean so that it can run under Apache::Registry for the full performance benefit.
The configuration section for running Apache::PerlRun is similar to Apache::Registry:
Alias /perl-run/ /home/www/perl-run/
<Location /perl>
SetHandler perl-script
PerlHandler Apache::PerlRun
Options +ExecCGI
# optional
PerlSendHeader On
</Location>
The Apache::PerlRun handler is only a small part of the picture. The rest of the Apache::PerlRun class provides subclassable methods that implement the functionality of Apache::Registry.
The Apache::PerlRun handler simply uses a subset of these methods; other modules may override certain methods to implement the Apache::Registry enviroment with a few twists. However, these Apache::PerlRun class methods were not fully defined when this book was going to press.
The Apache::Registry Class
##############################
The Apache::Registry class is essentially a CGI environment emulator that allows many CGI scripts to run without modification under mod_perl. Because there are many differences between CGI and the Apache API, Apache::Registry has to do a great deal of work to accomplish this sleight of hand.
It loads the scripts in its designated directory, compiles them, and stores them persistently in a memory structure. Before Apache::Registry runs a script, mod_perl will set up the various CGI environment variables, provided PerlSetupEnv is configured to On, which is the default.
When the PerlSendHeader directive is On, mod_perl monitors the text printed by the script, intercepts the HTTP header, and passes it through send_cgi_header(). It also arranges for STDIN to be read from the request object when the script attempts to process POST data.
Apache::Registry also monitors the modification dates of the script files it is responsible for and reloads them if their timestamp indicates they have been changed more recently than when they were last compiled.
Despite its complexity, Apache::Registry is easy to set up. The standard configuration consists of an Alias directive and a <Location> section:
Alias /perl/ /home/www/perl
<Location /perl>
SetHandler perl-script
PerlHandler Apache::Registry
Options +ExecCGI
# optional
PerlSendHeader On
</Location>
After restarting the server, you can place any (well, almost any) Perl CGI script into /home/www/perl (or the location of your choice) and make it executable. It runs just like an ordinary CGI script but will load much faster. The behavior of Apache::Registry can be tuned with the following directives:
PerlTaintCheck
When set to On, mod_perl will activate Perl taint checks on all the scripts under its control. Taint checks cause Perl to die with a fatal error if unchecked user-provided data (such as the values of CGI variables) is passed to a potentially dangerous function, such as exec(), eval(), or system().
PerlSendHeader
When set to On, mod_perl will scan for script output that looks like an HTTP header and automatically call send_http_header(). Scripts that send header information using CGI. pm's header() function do not need to activate PerlSendHeader. While scripts that use CGI.pm's header() will still function properly with PerlSendHeader On, turning it Off will save a few CPU cycles.
PerlFreshRestart
If PerlFreshRestart is set to On, mod_perl will flush its cache and reload all scripts when the server is restarted. This is very useful during module development to immediately see the changes to the source code take effect.
PerlWarn
If the script mentions the -w switch on its #! line, Apache::Registry will turn Perl warnings on by setting the $^W global to a nonzero value. The Perl-Warn directive can be configured to On to turn on warnings for all code inside the server.
Apache::Registry has several debug levels which write various informational messages to the server error log.
Apache::Registry scripts can change the debug level by importing Apache::Debug with its level pragma:
use Apache::Debug level => $level;
The debug level is a bit mask generated by ORing together some combination of the following values:
1 Make a note in the error log whenever the module is recompiled
2 Call Apache::Debug::dump() on errors
4 Turn on verbose tracing
The current value of the debug level can be found in the package global $Apache::Registry::Debug. You should not set this value directly, however. See Chapter 2, A First Module, for more hints on debugging Apache::Registry scripts.
===============================================================================================================
Labels:
Apache::PerlRun,
Apache::Registry,
cpan modules,
mod-perl,
modules,
perl vs mod-perl,
perl_modules
HTML::Template using tmpl_loop
template2.cgi
############
#!c:/perl/bin/perl
use CGI qw(:all);
my $q = CGI->new;
print $q->header();
my @languages = (
{
language_name => 'Perl',
description => 'Practical Extraction and Report Language'
},
{
language_name => 'PHP',
description => 'Hypertext Preprocessor'
},
{
language_name => 'ASP',
description => 'Active Server Pages'
},
);
my $template = HTML::Template->new( filename => 'template2.tmpl' );
$template->param( language => \@languages ); # Array ref You have to pass => [ {a=>10, b=>20}, {a=>30, b=>40}, {a=>50, b=>60} ]
print $template->output();
template2.html
################
<head>
<title>Template 2</title>
</head>
<body>
<table>
<tr>
<th>Language</th>
<th>Description</th>
</tr>
<tmpl_loop name="language">
<tr>
<td><tmpl_var name="language_name"></td>
<td><tmpl_var name="description"></td>
</tr>
</tmpl_loop>
</table>
</body>
</html>
############
#!c:/perl/bin/perl
use CGI qw(:all);
my $q = CGI->new;
print $q->header();
my @languages = (
{
language_name => 'Perl',
description => 'Practical Extraction and Report Language'
},
{
language_name => 'PHP',
description => 'Hypertext Preprocessor'
},
{
language_name => 'ASP',
description => 'Active Server Pages'
},
);
my $template = HTML::Template->new( filename => 'template2.tmpl' );
$template->param( language => \@languages ); # Array ref You have to pass => [ {a=>10, b=>20}, {a=>30, b=>40}, {a=>50, b=>60} ]
print $template->output();
template2.html
################
<head>
<title>Template 2</title>
</head>
<body>
<table>
<tr>
<th>Language</th>
<th>Description</th>
</tr>
<tmpl_loop name="language">
<tr>
<td><tmpl_var name="language_name"></td>
<td><tmpl_var name="description"></td>
</tr>
</tmpl_loop>
</table>
</body>
</html>
Labels:
cgi,
cpan modules,
html::template,
modules,
mvc,
perl_modules
Perl File Operations Read, Write
Reading from File:
#########################
#!/usr/local/bin/perl
my $filename = '/path/to/your/data.txt';
unless (-e $filename) {
print "File Doesn't Exist!";
}
open (MYFILE, $filename);
while (<MYFILE>) {
chomp;
print "$_\n";
}
close (MYFILE);
Writing to File
######################
#!/usr/local/bin/perl
my $filename = '/path/to/your/data.txt';
if (-e $filename) {
print "File Exists!";
}
unless (-e $filename) {
print "File Doesn't Exist!";
}
open (MYFILE, ">>$filename");
print MYFILE "Bob\n";
close (MYFILE);
use the > single greater than symbol to tell the open function that you want a fresh file each time.
use the >> to append to the file data.txt
File::Basename for type of file
##################################
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
use File::Basename;
#my($filename, $directories, $suffix) = fileparse($path);
#my($filename, $directories, $suffix) = fileparse($path, @suffixes);
#my $filename = fileparse($path, @suffixes);
#fileparse("/foo/bar/baz"); - On Unix returns ("baz", "/foo/bar/", "")
#fileparse('C:\foo\bar\baz'); - On Windows returns ("baz", 'C:\foo\bar\', "")
#fileparse("/foo/bar/baz/"); - On Unix returns ("", "/foo/bar/baz/", "")
my @exts = qw(.txt .zip);
while (my $file = <DATA>) {
chomp $file;
my ($dir, $name, $ext) = fileparse($file, @exts);
given ($ext) {
when ('.txt') {
say "$file is a text file";
}
when ('.zip') {
say "$file is a zip file";
}
default {
say "$file is an unknown file type";
}
}
}
__DATA__
file.txt
file.zip
file.pl
File::Type (mime_type)
##########################
use strict;
use warnings;
use File::Type;
my $file = '/path/to/file.ext';
my $ft = File::Type->new();
my $file_type = $ft->mime_type($file);
if ( $file_type eq 'application/octet-stream' ) {
# possibly a text file
}
elsif ( $file_type eq 'application/zip' ) {
# file is a zip archive
}
#########################
#!/usr/local/bin/perl
my $filename = '/path/to/your/data.txt';
unless (-e $filename) {
print "File Doesn't Exist!";
}
open (MYFILE, $filename);
while (<MYFILE>) {
chomp;
print "$_\n";
}
close (MYFILE);
Writing to File
######################
#!/usr/local/bin/perl
my $filename = '/path/to/your/data.txt';
if (-e $filename) {
print "File Exists!";
}
unless (-e $filename) {
print "File Doesn't Exist!";
}
open (MYFILE, ">>$filename");
print MYFILE "Bob\n";
close (MYFILE);
use the > single greater than symbol to tell the open function that you want a fresh file each time.
use the >> to append to the file data.txt
File::Basename for type of file
##################################
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
use File::Basename;
#my($filename, $directories, $suffix) = fileparse($path);
#my($filename, $directories, $suffix) = fileparse($path, @suffixes);
#my $filename = fileparse($path, @suffixes);
#fileparse("/foo/bar/baz"); - On Unix returns ("baz", "/foo/bar/", "")
#fileparse('C:\foo\bar\baz'); - On Windows returns ("baz", 'C:\foo\bar\', "")
#fileparse("/foo/bar/baz/"); - On Unix returns ("", "/foo/bar/baz/", "")
my @exts = qw(.txt .zip);
while (my $file = <DATA>) {
chomp $file;
my ($dir, $name, $ext) = fileparse($file, @exts);
given ($ext) {
when ('.txt') {
say "$file is a text file";
}
when ('.zip') {
say "$file is a zip file";
}
default {
say "$file is an unknown file type";
}
}
}
__DATA__
file.txt
file.zip
file.pl
File::Type (mime_type)
##########################
use strict;
use warnings;
use File::Type;
my $file = '/path/to/file.ext';
my $ft = File::Type->new();
my $file_type = $ft->mime_type($file);
if ( $file_type eq 'application/octet-stream' ) {
# possibly a text file
}
elsif ( $file_type eq 'application/zip' ) {
# file is a zip archive
}
Labels:
append,
cpan modules,
file,
File::Basename,
File::Type,
modules,
perl_modules,
read,
write
May 15, 2012
send email in perl
There are 3 ways to send mail from Perl
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use MIME::Lite;
my $msg = MIME::Lite->new(
From =>'admin@example.com',
To =>'user@example.com',
Subject =>'testing a mail with attachments',
Type =>'multipart/mixed'
) die "Error creating multipart container: $!\n";
### Add the text message part
$msg->attach (
Type => 'TEXT',
Data => "Here is the attachment file(s) you wanted"
) or die "Error adding the text message part: $!\n";
### Add the GIF file
$msg->attach (
Type => 'image/gif',
Path => my_file.gif,
Filename => your_file.gif,
Disposition => 'attachment'
) or die "Error adding $file_gif: $!\n";
### Add the ZIP file
$msg->attach (
Type => 'application/zip',
Path => my_file.zip,
Filename => your_file.zip,
Disposition => 'attachment'
) or die "Error adding $file_zip: $!\n";
$msg->send;
1;
- shelling out to /usr/sbin/sendmail
- using Net::SMTP directly when the application did not need to send attachments
- using MIME::Lite when you did need to include attachments
Out of these MIME::Lite is best as it handles attachments and performance perspective.
Let us explain sending email using MIME::Lite
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use MIME::Lite;
my $msg = MIME::Lite->new(
From =>'admin@example.com',
To =>'user@example.com',
Subject =>'testing a mail with attachments',
Type =>'multipart/mixed'
) die "Error creating multipart container: $!\n";
### Add the text message part
$msg->attach (
Type => 'TEXT',
Data => "Here is the attachment file(s) you wanted"
) or die "Error adding the text message part: $!\n";
### Add the GIF file
$msg->attach (
Type => 'image/gif',
Path => my_file.gif,
Filename => your_file.gif,
Disposition => 'attachment'
) or die "Error adding $file_gif: $!\n";
### Add the ZIP file
$msg->attach (
Type => 'application/zip',
Path => my_file.zip,
Filename => your_file.zip,
Disposition => 'attachment'
) or die "Error adding $file_zip: $!\n";
$msg->send;
1;
Jun 22, 2008
Slurp
- Slurp means reading or writing a file at one shot, instead of
reading or writing line by line.
- Generally slurp if very fast than normal reading a file line by line
- But slurp uses more memory, as it needs to keep th whole file
in a scalar (or) an array, but now a days as everybody is
having enormous amount of hard disk space and RAM, its not
a problem with Slurp.
- But those people where memeory and space concerns are there,
don't go for Slurp
- Some Cpan modules on Slurp are:
1) Slurp # Allows you to read multiple files at a time
2) File::Slurp # Good module for Slurp
3) Perl6::Slurp # Recent module on Slurp with lot more features
The Program will read a folder and read all the files and slurp them into an array and writes to output file
-------------------------------
#!F:\Perl\bin\perl -w
use Slurp;
use File::Slurp;
use Data::Dumper;
use strict;
my $dir='F:\Documents and Settings\Administrator\Desktop\sample_programs';
opendir DIR, $dir or die "cannot open dir $dir: $!";
my @file= readdir DIR;
closedir DIR;
my @final_files;
print "\n All file names before:", Dumper(\@file);
for (@file) {
next if($_ =~/^\.+|\.swp$|\~$/ig);
push (@final_files, $_);
}
my @zx = slurp(@final_files);
write_file('output.txt', @zx);
my @out = File::Slurp::read_file('output.txt');
print "\n output:" , Dumper(\@out);
reading or writing line by line.
- Generally slurp if very fast than normal reading a file line by line
- But slurp uses more memory, as it needs to keep th whole file
in a scalar (or) an array, but now a days as everybody is
having enormous amount of hard disk space and RAM, its not
a problem with Slurp.
- But those people where memeory and space concerns are there,
don't go for Slurp
- Some Cpan modules on Slurp are:
1) Slurp # Allows you to read multiple files at a time
2) File::Slurp # Good module for Slurp
3) Perl6::Slurp # Recent module on Slurp with lot more features
The Program will read a folder and read all the files and slurp them into an array and writes to output file
-------------------------------
#!F:\Perl\bin\perl -w
use Slurp;
use File::Slurp;
use Data::Dumper;
use strict;
my $dir='F:\Documents and Settings\Administrator\Desktop\sample_programs';
opendir DIR, $dir or die "cannot open dir $dir: $!";
my @file= readdir DIR;
closedir DIR;
my @final_files;
print "\n All file names before:", Dumper(\@file);
for (@file) {
next if($_ =~/^\.+|\.swp$|\~$/ig);
push (@final_files, $_);
}
my @zx = slurp(@final_files);
write_file('output.txt', @zx);
my @out = File::Slurp::read_file('output.txt');
print "\n output:" , Dumper(\@out);
Labels:
cpan modules,
File::Slurp,
perl_modules,
perl_script,
Slurp
How to use Ciel and Floor Functions
#!F:\Perl\bin\perl -w
use strict;
use POSIX; #ciel and floor available in POSIX module
$a = ceil(3.45);
print "\n $a"; #gives o/p as '4'
$b = floor(3.45);
print "\n $b"; #gives o/p as '3'
printf("\n%.3f", 3.1415926535); #gives output as 3.142, rounds of to 3 digits
#For Rounding use printf and sprintf
use strict;
use POSIX; #ciel and floor available in POSIX module
$a = ceil(3.45);
print "\n $a"; #gives o/p as '4'
$b = floor(3.45);
print "\n $b"; #gives o/p as '3'
printf("\n%.3f", 3.1415926535); #gives output as 3.142, rounds of to 3 digits
#For Rounding use printf and sprintf
Subscribe to:
Posts (Atom)