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 :

Access denied for user ‘ODBC’@’localhost

rajeshkumar created the topic: Access denied for user ‘ODBC’@’localhost

I am getting following error while running following command…any help

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

Error

D:\wamp\bin\mysql\MYSQL5~1.36\bin\MYSQLA~1.EXE: connect to server at 'localhost'
failed
error: 'Access denied for user 'ODBC'@'localhost' (using password: NO)'
Problem running D:\wamp\bin\mysql\MYSQL5~1.36\bin\MYSQLA~1.EXE - aborting ...

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 :

Use of use DBI;

scmuser created the topic: Use of use DBI;

Hi,

why do we use following syntax in perl program. can you please explain it?

use DBI;

rajeshkumar replied the topic: Re: Use of use DBI;

Perl’s coolest modules is the Perl database interface (DBI). This module offers a unified interface to different databases, providing a series of generic functions that are internally translated into native function calls. This makes it extremely easy to take advantage of a database and create dynamic Web pages using Perl.

Download and install

To begin, download and install the Perl DBI module and the MySQL DBD driver by running the following commands at your Perl prompt:

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

PERL – DBI Query

Queries must be prepared and then executed. Two lines of code are required for this, first the prepare() function and then the execute() function.

PERL – DBI Prepare()

Inside the prepare() function lies the actual SQL query. Essentially the prepare function acts precisely like the console of an SQL platform. If you’ve been following along, all we need to do is define a variable with a(n) SQL statement. Then create a query handle and run our $connect statement along with the prepare function as outlined below.

The only main difference is that we have to use PERL’s escaping characters and we probably have to use them more often.

PERL – DBI Execute
Once the query has been prepared, we must execute the command with the execute function. This is accomplished in one final line appended to the code above.

PERL – DBI Select Queries
Select queries fetch results and then return those results in the form of an array. Accessing the results of the array requires first that we bind the columns to variable names. Then we just need to set up a loop to loop through each row and print back the results to our browser.

#!/usr/bin/perl

# PERL MODULES WE WILL BE USING
use DBI;
use DBD::mysql;

# HTTP HEADER
print "Content-type: text/html \n\n";

# CONFIG VARIABLES
$platform = "mysql";
$database = "store";
$host = "localhost";
$port = "3306";
$tablename = "inventory";
$user = "username";
$pw = "password";

# DATA SOURCE NAME
$dsn = "dbi:mysql:$database:localhost:3306";

# PERL DBI CONNECT
$connect = DBI->connect($dsn, $user, $pw);

# PREPARE THE QUERY
$query = "SELECT * FROM inventory ORDER BY id";
$query_handle = $connect->prepare($query);

# EXECUTE THE QUERY
$query_handle->execute();

# BIND TABLE COLUMNS TO VARIABLES
$query_handle->bind_columns(undef, \$id, \$product, \$quantity);

# LOOP THROUGH RESULTS
while($query_handle->fetch()) {
print "$id, $product, $quantity
";
}

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

rajeshkumar replied the topic: Re: Use of use DBI;

One more example for connecting database using perl

You can connect to MySQL database from a Perl script using the Perl DBI module. DBI stands for “Database Interface” and is a database layer, with simple interface for SQL queries. Here is a Perl script connecting to MySQL database and printing FirstName and LastName columns from a table called Users:
#!/usr/local/bin/perl

use DBI;

print “Content-type:text/html\n\n”;

$dbh = DBI->connect(“dbi:mysql:database=; host=localhost; user=; password=”)
or die “Connecting from PHP to MySQL database failed: $DBI::errstr”;

$sSQL = “SELECT FirstName, LastName FROM Users”;
$st = $dbh->prepare($sSQL)
or die “Preparing MySQL query failed: $DBI::errstr
“;

$st->execute()
or die “The execution of the MySQL query failed: $DBI::errstr”;

while ($row = $st->fetchrow_hashref())
{
print ” $row->{FirstName} $row->{LastName}
“;
}

$dbh ->disconnect();

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

rajeshkumar replied the topic: Re: Use of use DBI;

One more example…

#!/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 :

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 :