Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calling one perl cgi from another 1

Status
Not open for further replies.

theEclipse

Programmer
Dec 27, 1999
1,190
US
Hello, I have set up a perl based template system for my website.

The template program takes a page parameter (site.pl?page=index).

I am also developing other scripts for my site. Is there a way to have my other pers scripts use the site.pl template? (eg. call the site.pl script from the other-pl-script.pl?)

thanks
Robert

Robert Carpenter
/b{2}|!b{2}/ - that is the question...
robert@convertingchaos.com
 
Yesm you can do this using LWP but its horribly inefficient.

The proper course is to refactor your template engine as a library and write CGI wrappers around that library.

Then, a cgi can call any parts of the library to generate any required templates.

This results in MUCH MUCH faster code that is amazingly easy to maintain and expand.

 
A library is just a bunch of procedures put together. Here is an example

Code:
package SAA::TourEngine::Utility ;

require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(get_dbh get_entity_class);


sub get_dbh {
        use DBI ;

        my $Host = $SAA::TourEngine::Config->{DBHost} ;
        my $Database = $SAA::TourEngine::Config->{DBName} ;
        my $User = $SAA::TourEngine::Config->{DBUser} ;
        my $Pass = $SAA::TourEngine::Config->{DBPass} ;

        my $dbh = DBI->connect( "dbi:mysql:$Database:$Host" , $User,$Pass ) ;
        return $dbh ;
}

sub get_entity_class {
        my $dbh = get_dbh() ;
        my %ARGS = @_ ;
        my $request = "SELECT NAME FROM ENTITY_TYPES WHERE ID=".$dbh->quote( $ARGS{ TYPE_ID } );
        my $sth = $dbh->prepare( $request  );
        $sth->execute() ;
        return $sth->fetchrow_array ;
}

1 ;

__END__

=head1 NAME

Utility - Various utility functions for use across all classes and software

=head1 SYNOPSIS

  use TourEngine::Utility;



=head1 DESCRIPTION

        get_dbh returns a valid DBH using variables from TourEngine::Config
        usually dbh is populated into $self->{dbh}

        get_entity_class(TYPE_ID) : Takes an ID of an entity type and returns its human readable name which
                                                                also happens to be its CLASS name

and here is how you would use it in your application

Code:
use SAA::TourEngine::Utility ;

 my $dbh = get_dbh( Config=>$ARGS{ Config } ) ;

See, I created a library with a function, I export the function and then other code can include the library and call the function.

So in your context you can create your template generation functions, put them in a library like this, export them and then include the library in your application and call the code where required.

This is a quick and dirty library, the way i like to do it (the above code is leveraged across some large applications), for a 'full' tutorial go here. Warning, its complex if you don't understand the guts of perl.

 
Oh, and its called a 'Package' in perl speak, you can google it or search the FAQ's here for more info.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top