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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Who can tell me what this code wants to do ? 4

Status
Not open for further replies.

ppgoodboy

Programmer
Oct 9, 2006
8
JP
Code:
sub new : method # public static
{
    my ($class, $db_type, $conn_str, $encoding) = @_;
    
    my $self = {};
[blue]
    my $dbio_impl;
    # ????????
    my $impl_class = "GECC::JobRequest::DAO::${db_type}DBIOImpl";
    eval {
        require $impl_class;
        $dbio_impl = $impl_class->new($conn_str, $encoding);
        foreach (qw/ SYSDATE_FUNC STRTODATE_FUNC STRTOTIME_FUNC /) {
            $self->{$_} = eval("${impl_class}::$_");
        }
    };
    if ($@) {
        warn $@;
    } else {
        $self->{-impl} = $dbio_impl;
    }
[/blue]
    bless $self, $class;
}
 
Looks to me as though it's making a DB connection and running some stored procedures/functions and returning a hash of the results.

But I'm stabbing in the dark on this one :)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
It appears to be a class constructor which is a pseudo-wrapper for the constructors of the referenced DBIOImpl classes. If it was well designed it would become a child-class of the intercepted classes, but instead it seems to want to just contain the chosen class. Please note that I've no idea what these parent classes really do; I can't find them and am guessing they're also home-grown.

It seems to take as arguments the database type desired, a connection string and an encoding type. The first of these is used to choose which class to load; the latter pair are simply forwarded on to the class constructor of the chosen class. My guess is that the calls to SYSDATE_FUNC and so on in the chosen class return a string for that database type which would give that database's SQL function name for the generic function named. In any case, the returns from these functions, if any, are stored in the class instance variable, as is the instance of the chosen class.

Thus, if the process does not error, the returned $self hash probably contains:
Code:
 ->{-impl}          = (the class instance of the DBIOImpl class chosen)
 ->{SYSDATE_FUNC}   = (SQL function name for retrieving the system date)
 ->{STRTODATE_FUNC} = (SQL function name for converting a string into an year-month-day date type)
 ->{STRTOTIME_FUNC} = (SQL function name for converting a string into an hour-minute-second time type)
 
hmmm, I'm glad that makes sense to you :)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Yes to both of the above, the part of the code in blue

Code:
    my $dbio_impl;
    # ????????
    my $impl_class = "GECC::JobRequest::DAO::${db_type}DBIOImpl";
    eval {
        require $impl_class;
        $dbio_impl = $impl_class->new($conn_str, $encoding);
        foreach (qw/ SYSDATE_FUNC STRTODATE_FUNC STRTOTIME_FUNC /) {
            $self->{$_} = eval("${impl_class}::$_");
        }
    };
    if ($@) {
        warn $@;
    } else {
        $self->{-impl} = $dbio_impl;
    }

looks to be checking to see if the functions in the list (SYSDATE_FUNC STRTODATE_FUNC STRTOTIME_FUNC) are there. If not the script returns an error, if they are, it blesses the hash and returns the reference.



- Kevin, perl coder unexceptional!
 
The warn is triggered by any failure - but I feel it's unlikely that it was designed to fail on not getting the returns from the named functions. More likely is that it was for guarding against a failure by the package require statement and class instatiation, which can fail simply on bad strings being passed to the constructor and would kill something not error-guarded in some way.
 
MOrac,

You're correct. The warn is checking for more than just the existence/returns of the functions. When I first looked at the code that seemed to be the main purpose but it is also checking that the required file returns true and is probably also a general error/warning routine for things as you said: not error-guarded, in some way.

- Kevin, perl coder unexceptional!
 
I am agree with MOrac,he is wonderful, and make the right answer.and thanks to everybody who answered me.It creats different database instance with different $db_type,and return the database system time and so on with db instance.When it goes wrong , then it is wrong.
 
hey cool, not a bad stab in the dark from me, even if I do say so myself ;-)

stars all round :)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top