IT Community - Software Programming, Web Development and Technical Support

Getting started with PERL

This is a discussion on Getting started with PERL within the Perl forums, part of the Software Development category; Hi, Can someone guide me to start using MYSQL in PERL?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Perl

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 08-16-2007, 04:58 AM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 385
Murali is on a distinguished road
Send a message via AIM to Murali
Default Getting started with PERL

Hi,

Can someone guide me to start using MYSQL in PERL?
__________________
-Murali..

Last edited by Murali : 08-16-2007 at 05:02 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 08-17-2007, 09:03 PM
Venkat Venkat is offline
D-Web Master
 
Join Date: Mar 2007
Posts: 347
Venkat is on a distinguished road
Thumbs up Re: Getting started with PERL

hey,

I can give you a nice introduction to a database interface with MySQL and Perl.

Despite the power, the stability, and the flexibility of MySQL, it's still only a database engine and not an application, per se. Although it can be accessed with the mysql client, the client is not suitable for most non-technical users. Therefore, developers must use a programming language to build user interfaces. A few of the popular languages provide application program interfaces (APIs) to interact with MySQL: Perl, PHP, Python, and Java. The basics of building a MySQL interface with Perl.

Let's start by connecting to the database:

use DBI;

my $dsn = 'DBI:mysql:my_database:localhost';
my $db_user_name = 'admin';
my $db_password = 'secret';
my ($id, $password);
my $dbh = DBI->connect($dsn, $db_user_name, $db_password);

Let's assume we've received as form input a nickname and password from a login screen. So right now,

$input_nickname = 'Cowlick' and $input_password = 'udder'

We want to verify that the entered password matches what we have in our database.

my $sth = $dbh->prepare(qq{
select id, password from users
where nickname = $input_nickname
});
$sth->execute();

Notice there is no command-terminating semi-colon.

How do we get the results? Since we only expect one row,

($id, $password) = $sth->fetchrow_array();
$sth->finish(): # we're done with this query
if ($input_password eq $password) # case-sensitive
{
... # login successful
}

What if our result is more than one row? Successive calls to

$sth->fetchrow_array()

will return the rest of the result set.

my $sth = $dbh->prepare(qq{
select nickname, favorite_number from users
});
$sth->execute();
while (my ($nickname, $favorite_number) =
$sth->fetchrow_array()) # keep fetching until
# there's nothing left
{
print "$nickname, $favorite_number\n";
}
$sth->finish();

If we want to save the entire result set first for processing later,

my (@matrix) = ();
while (my @ary = $sth->fetchrow_array())
{
push(@matrix, [@ary]); # [@ary] is a reference
}
$sth->finish();

A reference, for C programers, can be thought of as a pointer. The Matrix is now an array of array references, or a two-dimensional array.

You can access row $i with:

@{matrix[$i]}

Or, you can access a specific row and column ($i, $j) in the table with:

$matrix[$i][$j]

For MySQL operations that don't return a result you can use the do method instead of prepare then execute.

$dbh->do("insert into message_votes
(message_id, user_id, vote) values (1, 3, 'good')");

When you're done with the database:

$dbh->disconnect();

MySQL That should be enough to get you started. You can see that using Perl DBI is a matter of calling a method with the MySQL command as a string.
__________________
Venkat
knowledge is Power
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 08-20-2007, 08:03 AM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 385
Murali is on a distinguished road
Send a message via AIM to Murali
Default Re: Getting started with PERL

Hi Venkat,

Now i can create a perl file for connecting database, then how to execute that?


Shall i need to execute in the command prompt? else shall i execute it via Internet Browser?
__________________
-Murali..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4  
Old 10-04-2008, 10:37 PM
danica danica is offline
D-Web Trainee
 
Join Date: Sep 2008
Posts: 12
danica is on a distinguished road
Default Re: Getting started with PERL

The Perl language includes a specialized syntax for writing regular expressions (RE, or regexes), and the interpreter contains an engine for matching strings to regular expressions. The regular expression engine uses a backtracking algorithm, extending its capabilities from simple pattern matching to string capture and substitution.

_________________________

Keyword Research Georgia Health Insurance
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5  
Old 02-11-2009, 02:16 AM
Qiuene06 Qiuene06 is offline
D-Web Trainee
 
Join Date: Feb 2009
Posts: 1
Qiuene06 is on a distinguished road
Smile our daily decisions

I’ve always thought of life as a spiraling staircase, gracefully wrapping around time and achievement, as opposed to the blunt vertical found on a ladder. Comparing life’s ascension to a set of stairs is often apt, but there are moments when life as ladder is far more fitting, such as maple story mesos when we find ourselves stuck between rungs, mired in the middle of old pattern and new performance. This is when the decisive climb from one rung to the next must begin so that we may climb toward our tomorrow, while leaving yesterday behind.Steps may be clambered in wow power leveling tandem (my wife and I often fall into reflexive harmony when we find ourselves on stairs together), but climbing a ladder is a solitary endeavor. We begin at the bottom, then spend our lifetimes reaching for the top, each rung pulling us closer to dusk and further from dawn. Destinations are determined by our daily decisions, as is our grasp and the speed of our climb.Each of us finds ourselves wow power leveling at some point stuck between the rungs, pinned at an impasse. Perhaps success has halted, and our growth is no longer happening at the rate it once was. We can wrap our hands around the rung above, but we do not have the strength to pull ourselves upward. Often, it is the rung just archlord gold below that is holding us hostage with insubordinate habit. It is then when we must lower our heel, shatter the habits to bits, and then continue to climb without looking behind.It is never too late to learn a new habit; never to early to shed the old like dead, useless skin. Bad habits are formed by the slow and steady accumulation of mindless minutes. As a million years of buy archlord gold rainfall will smooth the slope of a mountain summit, so do a million misplaced moments warp our good intentions.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
what is perl? inder Perl 2 01-07-2009 09:17 PM
Help getting started dchips13 J2ME 3 03-21-2008 04:10 AM
How to set Perl Interpreter within perl script sivaramakrishnan Perl 1 07-19-2007 05:45 AM
Perl? swoosh Perl 2 03-19-2007 03:31 AM
Java:Tutorial - Getting Started pranky Java Programming 0 02-23-2007 11:48 PM


All times are GMT -7. The time now is 10:28 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0