Other Added
#1 in Business Subscribe Email Print

You are here: Home > Internet and Businesses Online > Web Development > Building An Application Framwork In Mod_Perl Using Cpan Modules - Part 2

Tags

  • technology
  • standard
  • function expects
  • persistence issue
  • function checks

  • Links

  • Turn Words Into Traffic
  • Here Come the Holidays: Keeping Sane During a Busy Time
  • Causes of Depreesion - Abuse
  • Other Added - Building An Application Framwork In Mod_Perl Using Cpan Modules - Part 2

    Online Resume Formats
    There are several types of online resume formats that can be used when contacting potential employers. When you search for job openings online, some companies will have on their websites which online resume format is acceptable.If you don’t know which format is acceptable to a certain company, call them and ask. The worst thing you can do is send an online resume in the wrong format and have it ignored.PLAIN TEXT RESUMEThis format is also referred to as ASCII. Many companies used to accept this type of resume a few years ago when searching for a job on the internet was still new. When this format is used, the resume is written in plain text w
    >$rows = $sth->fetchall_arrayref();

    $sth->finish();

    $self->debug(1, "Database: $query_handle failed" ) unless ( $rows );

    return $rows; }

    Ok, so at first glance it may look complex but I'll simplify. Firstly, the function expects the first argument to be a database statement, which is read from a file (in this case a simple text file in the format 'statement = select * from mytable where col1 = ? and col2 = ?'), followed by an optional database name preceded with the word SELECT (e.g. SELECTmydb where mydb is the database to use), and then a list of parameters to fill in the statement, i.e. the value for col1 and col2. This function checks for existence of database to use (checking an argument with prefix SELECT) and also the existence of any parameters to pass to statement. Depending on whether or not any of these are present th

    Affiliate Marketing Online: Does It Make or Take Your Money?
    One of the biggest questions I see all over the internet these days is: "Is Affiliate Marketing an Online Money Marketing Scam?" If you have spent any time looking for ways to generate extra money from home, I am sure you have seen this many times. Before I try to answer this question, let's step back and explain what affiliate marketing really is.If you are new to the internet or have not paid much attention to what is going on, you may not realize the amount of advertising that exists in ether-space. These days, on almost any search engine, article, blog, myspace, email account, etc that you visit, there is probably some sort of advertising somewhere (if not all over the place). On
    Introduction

    In the previous article I discussed the basics of rolling your very own application framework covering the structure, setup and layout of such a system and also covered templating. In this article we will add juicy features such as database handling, interacting with remote web sites via SOAP and email functionality. Database handling The secret to handling databases in a web environment is to ensure strict clean code, e.g. ensuring database and statement handles and data structures are opened and closed properly. It is very common for database and statement handles to left open by developers when using the age old copy and paste technique of building their application. How often have you written a script and forgot to call close() on a method? Of course persistence is important when dealing with databases for the web, thankfully mod_perl allows for this quite nicely by extending the standard DBI class with Apache::DBI. You may have noted in Part 1 that a global var was declared to hold the database handle, this solves the persistence issue. Here we will describe a system that deals with a single database and/or other/multiple databases. In our Base.pm class the constructor opens a connection to the database, here it is: sub new {

    my $self = [];

    bless($self);

    my $db_name = Website::Constants::DB_NAME;

    my $db_user = Website::Constants::DB_USER;

    my $db_pass = Website::Constants::DB_PASS;

    $dbh = DBI->connect('DBI:mysql:'. $db_name, $db_user, $db_pass);

    $self->[DB_ERROR] = '';

    unless ( defined($dbh) && $dbh ) {

    $self->debug( 1, 'Unable to connect to MASTER DB!' );

    $self->[DB_ERROR] = 'Unable to connect to DB!';

    }

    return $self; }

    This provides basic error checking and logging of the attempt to connect to the database. Taking this to next step, in this class we will define the functions used to interact with the database, i.e. selects, updates and inserts. Here follows our db_select function for retrieving rows from the database in a nice clean fashion: sub db_select {

    my $self = shift;

    my $query_handle = shift;

    my $args = shift;

    my $sql = $self->get_query( $query_handle );

    unless ( $sql ) {

    $self->[DB_ERROR] = "No such database query: $query_handle";

    return -1;

    }

    my $has_args = @{$args};

    my $db = undef;

    $db = pop(@{$args}) if ( $has_args > 0);

    my $valid_db = 0;

    if ( $db =~ /SELECT/ ) {

    $db =~ s/SELECT//;

    $valid_db = 1;

    }

    push(@{$args}, $db) unless ( $has_args == 0 || $valid_db == 1 );

    my $this_dbh = undef;

    my $sth = undef;

    if ( $db && $valid_db ) {

    $this_dbh = DBI->connect('DBI:mysql:tracker_' . $db, 'root', '5exy');

    unless ( defined($this_dbh) && $this_dbh ) {

    $self->debug( 1, 'Unable to connect to CUSTOM DB: ' . $db );

    }

    $sth = $this_dbh->prepare($sql);

    }

    else {

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

    }

    if ( $args && $has_args > 0 ) {

    if ( @{$args} > 0 ) {

    unless ( $sth->execute( @{$args} ) ) {

    $self->debug( 1, $query_handle . ": " . $dbh->errstr);

    $self->[DB_ERROR] = $dbh->errstr;

    return -1;

    }

    }

    }

    else {

    unless ( $sth->execute() ) {

    $self->debug( 1, $query_handle . ": " . $dbh->errstr);

    $self->[DB_ERROR] = $dbh->errstr;

    return -1;

    }

    }

    $rows = $sth->fetchall_arrayref();

    $sth->finish();

    $self->debug(1, "Database: $query_handle failed" ) unless ( $rows );

    return $rows; }

    Ok, so at first glance it may look complex but I'll simplify. Firstly, the function expects the first argument to be a database statement, which is read from a file (in this case a simple text file in the format 'statement = select * from mytable where col1 = ? and col2 = ?'), followed by an optional database name preceded with the word SELECT (e.g. SELECTmydb where mydb is the database to use), and then a list of parameters to fill in the statement, i.e. the value for col1 and col2. This function checks for existence of database to use (checking an argument with prefix SELECT) and also the existence of any parameters to pass to statement. Depending on whether or not any of these are present the

    Small Business Networks: Getting Past Small Business Networking Myopia
    Many small businesses mistakenly think that they’re too small to cost-justify a "real" client/server small business network. However, because small businesses want, and in most cases, need, the same technology tools as their larger competitors, deploying a peer-to-peer network doesn’t usually make small business sense (except for the tiniest small offices).Don’t Let Clients Underestimate Technology RequirementsWhen purchase decisions are based solely on the initial price, small businesses tend to underestimate their technology requirements. If your client or prospect requires a secure, reliable, scalable and flexible technology backbone, the client or prospect needs a "real",
    ly mod_perl allows for this quite nicely by extending the standard DBI class with Apache::DBI. You may have noted in Part 1 that a global var was declared to hold the database handle, this solves the persistence issue. Here we will describe a system that deals with a single database and/or other/multiple databases. In our Base.pm class the constructor opens a connection to the database, here it is: sub new {

    my $self = [];

    bless($self);

    my $db_name = Website::Constants::DB_NAME;

    my $db_user = Website::Constants::DB_USER;

    my $db_pass = Website::Constants::DB_PASS;

    $dbh = DBI->connect('DBI:mysql:'. $db_name, $db_user, $db_pass);

    $self->[DB_ERROR] = '';

    unless ( defined($dbh) && $dbh ) {

    $self->debug( 1, 'Unable to connect to MASTER DB!' );

    $self->[DB_ERROR] = 'Unable to connect to DB!';

    }

    return $self; }

    This provides basic error checking and logging of the attempt to connect to the database. Taking this to next step, in this class we will define the functions used to interact with the database, i.e. selects, updates and inserts. Here follows our db_select function for retrieving rows from the database in a nice clean fashion: sub db_select {

    my $self = shift;

    my $query_handle = shift;

    my $args = shift;

    my $sql = $self->get_query( $query_handle );

    unless ( $sql ) {

    $self->[DB_ERROR] = "No such database query: $query_handle";

    return -1;

    }

    my $has_args = @{$args};

    my $db = undef;

    $db = pop(@{$args}) if ( $has_args > 0);

    my $valid_db = 0;

    if ( $db =~ /SELECT/ ) {

    $db =~ s/SELECT//;

    $valid_db = 1;

    }

    push(@{$args}, $db) unless ( $has_args == 0 || $valid_db == 1 );

    my $this_dbh = undef;

    my $sth = undef;

    if ( $db && $valid_db ) {

    $this_dbh = DBI->connect('DBI:mysql:tracker_' . $db, 'root', '5exy');

    unless ( defined($this_dbh) && $this_dbh ) {

    $self->debug( 1, 'Unable to connect to CUSTOM DB: ' . $db );

    }

    $sth = $this_dbh->prepare($sql);

    }

    else {

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

    }

    if ( $args && $has_args > 0 ) {

    if ( @{$args} > 0 ) {

    unless ( $sth->execute( @{$args} ) ) {

    $self->debug( 1, $query_handle . ": " . $dbh->errstr);

    $self->[DB_ERROR] = $dbh->errstr;

    return -1;

    }

    }

    }

    else {

    unless ( $sth->execute() ) {

    $self->debug( 1, $query_handle . ": " . $dbh->errstr);

    $self->[DB_ERROR] = $dbh->errstr;

    return -1;

    }

    }

    $rows = $sth->fetchall_arrayref();

    $sth->finish();

    $self->debug(1, "Database: $query_handle failed" ) unless ( $rows );

    return $rows; }

    Ok, so at first glance it may look complex but I'll simplify. Firstly, the function expects the first argument to be a database statement, which is read from a file (in this case a simple text file in the format 'statement = select * from mytable where col1 = ? and col2 = ?'), followed by an optional database name preceded with the word SELECT (e.g. SELECTmydb where mydb is the database to use), and then a list of parameters to fill in the statement, i.e. the value for col1 and col2. This function checks for existence of database to use (checking an argument with prefix SELECT) and also the existence of any parameters to pass to statement. Depending on whether or not any of these are present th

    7 Simple Tips For Building Trust
    Building trust between you and your potential client is a very important step that needs to occur first or else they won't buy from you. In fact, building trust is a prerequisite to selling. So how do you go about building this trust? Following are 7 tips.Tip #1 When having a sales conversation, explore whether you can help the person get what they want. Forget about selling because as soon as someone feels you're trying to sell something, they'll instinctively not trust you. That's just human nature. If, however, someone feels you are genuinely trying to help them, then they'll be more likely to trust you and buy from you.Tip #2 Ask questions - be sincere. When you sin

    return $self; }

    This provides basic error checking and logging of the attempt to connect to the database. Taking this to next step, in this class we will define the functions used to interact with the database, i.e. selects, updates and inserts. Here follows our db_select function for retrieving rows from the database in a nice clean fashion: sub db_select {

    my $self = shift;

    my $query_handle = shift;

    my $args = shift;

    my $sql = $self->get_query( $query_handle );

    unless ( $sql ) {

    $self->[DB_ERROR] = "No such database query: $query_handle";

    return -1;

    }

    my $has_args = @{$args};

    my $db = undef;

    $db = pop(@{$args}) if ( $has_args > 0);

    my $valid_db = 0;

    if ( $db =~ /SELECT/ ) {

    $db =~ s/SELECT//;

    $valid_db = 1;

    }

    push(@{$args}, $db) unless ( $has_args == 0 || $valid_db == 1 );

    my $this_dbh = undef;

    my $sth = undef;

    if ( $db && $valid_db ) {

    $this_dbh = DBI->connect('DBI:mysql:tracker_' . $db, 'root', '5exy');

    unless ( defined($this_dbh) && $this_dbh ) {

    $self->debug( 1, 'Unable to connect to CUSTOM DB: ' . $db );

    }

    $sth = $this_dbh->prepare($sql);

    }

    else {

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

    }

    if ( $args && $has_args > 0 ) {

    if ( @{$args} > 0 ) {

    unless ( $sth->execute( @{$args} ) ) {

    $self->debug( 1, $query_handle . ": " . $dbh->errstr);

    $self->[DB_ERROR] = $dbh->errstr;

    return -1;

    }

    }

    }

    else {

    unless ( $sth->execute() ) {

    $self->debug( 1, $query_handle . ": " . $dbh->errstr);

    $self->[DB_ERROR] = $dbh->errstr;

    return -1;

    }

    }

    $rows = $sth->fetchall_arrayref();

    $sth->finish();

    $self->debug(1, "Database: $query_handle failed" ) unless ( $rows );

    return $rows; }

    Ok, so at first glance it may look complex but I'll simplify. Firstly, the function expects the first argument to be a database statement, which is read from a file (in this case a simple text file in the format 'statement = select * from mytable where col1 = ? and col2 = ?'), followed by an optional database name preceded with the word SELECT (e.g. SELECTmydb where mydb is the database to use), and then a list of parameters to fill in the statement, i.e. the value for col1 and col2. This function checks for existence of database to use (checking an argument with prefix SELECT) and also the existence of any parameters to pass to statement. Depending on whether or not any of these are present th

    The Gender Blenders—How Successful Men and Women Mix-It-Up in Negotiation
    Men and women have been talking to each other, past each other and at each other ever since Adam became separated from his rib and the first gender gap was opened.Our early ancestors settled on a division of labor, dictated largely by biological necessity: The women bore the children and carried within their bosoms their infants' first food supply. Hence, Mama stayed home with the kids while Papa went hunting Mastodons and fighting bad guys from other tribes.Mama dug up roots and picked berries to go with the meaty victuals Papa brought home, but outside the Clan of the Cave Bear, she was an observer, not a participant in the hunt.From early history, boys and girls grew
    ( $has_args == 0 || $valid_db == 1 );

    my $this_dbh = undef;

    my $sth = undef;

    if ( $db && $valid_db ) {

    $this_dbh = DBI->connect('DBI:mysql:tracker_' . $db, 'root', '5exy');

    unless ( defined($this_dbh) && $this_dbh ) {

    $self->debug( 1, 'Unable to connect to CUSTOM DB: ' . $db );

    }

    $sth = $this_dbh->prepare($sql);

    }

    else {

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

    }

    if ( $args && $has_args > 0 ) {

    if ( @{$args} > 0 ) {

    unless ( $sth->execute( @{$args} ) ) {

    $self->debug( 1, $query_handle . ": " . $dbh->errstr);

    $self->[DB_ERROR] = $dbh->errstr;

    return -1;

    }

    }

    }

    else {

    unless ( $sth->execute() ) {

    $self->debug( 1, $query_handle . ": " . $dbh->errstr);

    $self->[DB_ERROR] = $dbh->errstr;

    return -1;

    }

    }

    $rows = $sth->fetchall_arrayref();

    $sth->finish();

    $self->debug(1, "Database: $query_handle failed" ) unless ( $rows );

    return $rows; }

    Ok, so at first glance it may look complex but I'll simplify. Firstly, the function expects the first argument to be a database statement, which is read from a file (in this case a simple text file in the format 'statement = select * from mytable where col1 = ? and col2 = ?'), followed by an optional database name preceded with the word SELECT (e.g. SELECTmydb where mydb is the database to use), and then a list of parameters to fill in the statement, i.e. the value for col1 and col2. This function checks for existence of database to use (checking an argument with prefix SELECT) and also the existence of any parameters to pass to statement. Depending on whether or not any of these are present th

    Top 5 Tips For Effective E-mail Marketing
    Over the past few years, research has continued to prove the benefits of e-mail marketing for business: low costs, high conversion rates and detailed tracking are all notable features. But e-mail marketing is becoming much more than just a tool for spammers and e-businesses. Consumers are becoming increasingly savvy on the differences between spam and permission-based emails, and more and more of them are accepting permission-based e-mail marketing as a positive replacement for direct mail.The best news is that the majority of people who receive permission-based e-mails open, on average, 78% of them.Jupiter Research reports effective email marketing campaigns can produc
    >$rows = $sth->fetchall_arrayref();

    $sth->finish();

    $self->debug(1, "Database: $query_handle failed" ) unless ( $rows );

    return $rows; }

    Ok, so at first glance it may look complex but I'll simplify. Firstly, the function expects the first argument to be a database statement, which is read from a file (in this case a simple text file in the format 'statement = select * from mytable where col1 = ? and col2 = ?'), followed by an optional database name preceded with the word SELECT (e.g. SELECTmydb where mydb is the database to use), and then a list of parameters to fill in the statement, i.e. the value for col1 and col2. This function checks for existence of database to use (checking an argument with prefix SELECT) and also the existence of any parameters to pass to statement. Depending on whether or not any of these are present the correct action is taken to read from the database, in the case that there are no parameters to be passed to the statement, DBI execute is called as such, in the case a custom database is supplied this database is used to supply results. Once the database and statement parameters have been taken care off the resulting dataset is slurped into an array ref to be returned after closing the statement handle. Note that the database handle is left open, this is intentional to provide a persistent connection. There is also some logging done in the event of an error or no data being returned, this could be modified to explicitly state which is the case. This function is modified quite easily to provide seperate functions to handle SQL update/deletes and inserts, the same principles apply just needing modification for specifics of a db_update function for update/deletes and a db_insert function, the above example should be enough to get you started on these. To call this in your application using orders_db, then with standard db as loaded on startup: my $rows = $self->db_select('get_orders', ['SELECTorders_db', $order_id]);

    for $order (@{$rows}) {

    print "Product ID: $order->[0]n"; }

    my $rows2 = $self->db_select('get_orders', [$order_id]);

    HTTP = HTML link (for blogs, profiles,phorums):
    <a href="http://www.otheradded.com/article/86482/otheradded-Building-An-Application-Framwork-In-ModPerl-Using-Cpan-Modules--Part-2.html">Building An Application Framwork In Mod_Perl Using Cpan Modules - Part 2</a>

    BB link (for phorums):
    [url=http://www.otheradded.com/article/86482/otheradded-Building-An-Application-Framwork-In-ModPerl-Using-Cpan-Modules--Part-2.html]Building An Application Framwork In Mod_Perl Using Cpan Modules - Part 2[/url]

    Related Articles:

    Corporate Gifts with Logo is Mileage for Money

    Customer Service and Conservative Political Conversations

    Winning Is Not The Only Thing: There Is Always The Need For Customer Service

    Bookmark it: del.icio.us digg.com reddit.com netvouz.com google.com yahoo.com technorati.com furl.net bloglines.com socialdust.com ma.gnolia.com newsvine.com slashdot.org simpy.com shadows.com blinklist.com