I need to a perl script

rajeshkumar created the topic: i need to a perl script
Hi ,

There is a file called test.txt what are all the different ways

BEGIN:
test 1
test 2 %%%%%%%%%% TEST TEST TEST
test 3
END:

need to grab all contents in between BEGIN and END:
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

rajeshkumar replied the topic: Re:i need to a perl script

Solution could be …..

#!/usr/bin/perl -w
$file=open(f, "test.txt");
$str="";
while() {$str.=$_;}
close(f);
$str =~ s/BEGIN:(.*)END:/$1/egs;
print $str

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

venu.madhav48@gmail.com replied the topic: Re:i need to a perl script

Please answer the below questions

1.What standard Perl modules have you made use of, and why did you use them instead of the equivalent built-in functions?

2.Write a statement that checks to make sure there are at least two command line arguments and exits with an error if not.

3.How do you find the number of elements in an array?

4.When an array is defined, PERL automatically numbers each element in the array
beginning with zero

venu.madhav48@gmail.com replied the topic: Re:i need to a perl script
Dear Rajesh,

Can u please help in answering the above questions

rajeshkumar replied the topic: Re:i need to a perl script

Hi Venu,

i will post this solution today evening…
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

venu.madhav48@gmail.com replied the topic: Re:i need to a perl script
Waiting for your post….

Tagged :

Create a flat file database using perl

scmuser created the topic: Create a flat file database using perl

How to create a flat file database as shown below

s.no name age city phone
0 hema 22 Calcutta 4312542
1 hema 21 Bangalore 2344345
2 ganesh 25 delhi 2445454
3 kartik 45 pune 4312121
4 santosh 25 Hyderabad 2254231
5 kumar 25 mysore 2344567
6 gita 34 mangalore 6532123
7 gita 32 pune 2213456

Q1.print the details of the person who r from bangalore

Q2.Replace the city name managlore to pune

Q3.prints no of person having name gita and hema

Q4.print how many are of age 25

Tagged :

Use Vs require in perl

scmuser created the topic: use Vs require in perl

Hi,

Whats difference between “use” and “require” in perl?

rajeshkumar replied the topic: Re: use Vs require in perl

use is done at ‘compile-time’ and require is done at ‘run-time’ (ie can conditionally load modules)

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

rajeshkumar replied the topic: Re: use Vs require in perl
More…

use is a pre-built in function in perl to Imports some semantics into the current package from the named module, generally by aliasing certain subroutine or variable names into your package such as..

* use Module VERSION LIST

* use Module VERSION
* use Module LIST
* use Module
* use VERSION

More info.. perldoc.perl.org/functions/use.html

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :

Spit out warnings on uninitialized variables Perl

scmuser created the topic: Spit out warnings on uninitialized variables Perl

How to Spit out warnings on uninitialized variables in Perl?

rajeshkumar replied the topic: Re: Spit out warnings on uninitialized variables Perl

#!/usr/local/bin/perl -w

This will help you (really, it will force you) to write better, cleaner code. Adding the -w switch to the perl interpreter will cause it to spit out warnings on uninitialized variables – potential bugs.

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :

Installing Perl modules

rajeshkumar created the topic: Installing Perl modules

Perl modules may be installed using the CPAN module or from source.
CPAN method

perl -MCPAN -e shell (to get an interactive CPAN shell)
perl -MCPAN -e ‘install Time::JulianDay’ (if you know the name of the module, you can install it directly without interacting with the CPAN shell)

Within the CPAN shell:
i /expression/ will search for a Perl module containing expression, and
install module will install the module.

Example:
perl -MCPAN -e shell
i /JulianDay/
install Time::JulianDay

Note: if you are behind a firewall, you may wish to use passive FTP with Perl’s Net::FTP module. Set the environment variable FTP_PASSIVE 1 (or any non-zero value) to use passive FTP when downloading Perl modules through CPAN.

Manual installation

To manually install a Perl module:

1. Download the Perl module from CPAN or other site.
2. Extract the tarball.
3. Run perl Makefile.PL
4. Run make
5. Run make test
6. Run make install

Note: you should use the same compiler to build Perl modules that you used to build Perl. For example, if you are building Perl modules with gcc and are using a version of Perl that was supplied with your distribution (ex. Solaris 8 includes Perl 5.005_03), you may run into errors.

Checking for existence of a Perl module
An easy way to check for the existence of a Perl module on your system (technically, in Perl’s @INC array, a list of directories Perl searches when attempting to load modules) is to run perl -e ‘use module;’

Example:

perl -e ‘use HTML::Parser;’

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :

Perl -MCPAN -e “install DBD::mysql”

rajeshkumar created the topic: perl -MCPAN -e “install DBD::mysql”

I am getting following issues while running following command in Windows Machine…

perl -MCPAN -e "install DBD::mysql"

Error

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :

Connect to my MySQL database using Perl?

rajeshkumar created the topic: connect to my MySQL database using Perl?

#!/usr/bin/perl
use DBI;

$database = “DBNAME”;
$hostname = “DBSERVER”;
$port = “3306”;
$username = “DBUSERNAME”;
$password = ‘DBPASSWORD’;

$dsn = “DBI:mysql:database=$database;host=$hostname;port=$port”;

$dbh = DBI->connect($dsn, $username, $password) or die(“Could not connect!”);

$sql = “SELECT * FROM mytable”;

$sth = $dbh->prepare($sql);
$sth->execute;

while(($column1, $column2) = $sth->fetchrow_array)
{
print “C1 = $column1, C2 = $column2n”;
}

$dbh->disconnect;

where DBNAME, DBUSERNAME, and DBPASSWORD are your database name, database username and database password, and where DBSERVER is your database server.

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :

Why we use ‘use strict’ in perl

scmuser created the topic: Why we use ‘use strict’ in perl

Hi,

Why you should ‘use strict’? what is the significant of “strict” in out module?

rajeshkumar replied the topic: Re: Why we use ‘use strict’ in perl

Whenever your program gets over a few lines long, definitely when you can’t view the whole program on one page, or sometimes when you just can’t figure out what else could be wrong.

To help you catch typos so you can quickly get on to finding more significant problems (and so we don’t have to catch the typos for you either), among other reasons.
Its difficult to spot ‘$recieve_date’ when on the previous page you’ve been calling it ‘$receive_date’. Also, to give your variables as small a scope as possible so that you don’t have to worry about what they’re doing to other parts of your program (although that’s the function of my, it forces you to use my which when properly used helps achieve this goal).

Put this line at the top of your script (after the shebang, e.g., ‘#!/usr/bin/perl’ line):

use strict;
# Change this:
$string = "hello world";
@array = qw(ABC DEF);
%hash = (A=>1, B=>2);

# To this:
my $string = "hello world";
my @array = qw(ABC DEF);
my %hash = (A=>1, B=>2);

# Change this:
# '$name' is global here
foreach $name (@names) {
print "Name: $name\n";
}

# To this:
foreach my $name (@names) {
# Now '$name' only exists in this block
print "Name: $name\n";
}

# Change this:
# Likewise, '$digit' is global here
foreach $digit (@digits) {
$number = 10*$number + $digit;
}
print "Number: $number\n";

# To this (variables used in an outer scope ('$number')
# will have to be declared in an outer scope):
my $number = 0;
foreach my $digit (@digits)
# Now '$digit' only exists in this block
$number = 10*$number + $digit;
}
print "Number: $number\n";

# Change this:
sub my_sub {
($arg1, $arg2) = @_;
print "Arg1: $arg1 Arg2: $arg2\n";
}

# To this:
sub my_sub {
my ($arg1, $arg2) = @_;
print "Arg1: $arg1 Arg2: $arg2\n";
}

# Using DBI? You can change this:
$sth->bind_columns(\$field1, \$field2);
while ($sth->fetch) {
print "F1: $field1 F2: $field2\n";
}

# To this (the '\' is distributed over a list of values):
$sth->bind_columns(\my ($field1, $field2));
while ($sth->fetch) {
print "F1: $field1 F2: $field2\n";
}

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

rajeshkumar replied the topic: Re: Why we use ‘use strict’ in perl

Some More Helpful info…

By default Perl allows you to use variables without declaring them. This may be convenient for short scripts and one-liners.But in a longer unit of code such as a module it is wise to declare your variables both to catch typos and to constrain their accessibility appropriately from outside the module. The strict pragmaforces you to declare your variables.

If no import list is supplied, all possible restrictions are assumed. (This is the safest mode to operate in, but is sometimes too strict for casual programming.) Currently, there are three possible things to be strict about: “subs”, “vars”, and “refs”.

strict refs
This generates a runtime error if you use symbolic references
1. use strict ‘refs’;
2. $ref = \$foo;
3. print $$ref; # ok
4. $ref = “foo”;
5. print $$ref; # runtime error; normally ok
6. $file = “STDOUT”;
7. print $file “Hi!”; # error; note: no comma after $file

strict vars

This generates a compile-time error if you access a variable that wasn’t declared via our or use vars , localized via my(), or wasn’t fully qualified. Because this is to avoid variable suicide problems and subtle dynamic scoping issues, a merely local() variable isn’t good enough.
1. use strict ‘vars’;
2. $X::foo = 1; # ok, fully qualified
3. my $foo = 10; # ok, my() var
4. local $foo = 9; # blows up
5.
6. package Cinna;
7. our $bar; # Declares $bar in current package
8. $bar = ‘HgS’; # ok, global declared via pragma

strict subs
This disables the poetry optimization, generating a compile-time error if you try to use a bareword identifier that’s not a subroutine, unless it is a simple identifier (no colons) and that it appears in curly braces or on the left hand side of the => symbol
1. use strict ‘subs’;
2. $SIG{PIPE} = Plumber; # blows up
3. $SIG{PIPE} = “Plumber”; # just fine: quoted string is always ok
4. $SIG{PIPE} = \&Plumber; # preferred form

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :