perlidiot123 has asked for the wisdom of the Perl Monks concerning the following question:

My latest question is kinda basic (Always learning here). I need to pass resultant vars from an sql query to the object methods directly, if it's possible. For instance:
# First Step my $query=qq~select name,text from pages where uid='1'~; # Second Step my($name,$text)=&sthqf($query); # Sub that handles all single row quer +ies # Third Step $Tobj->name($name); $Tobj->text($text); undef ($name,$text,$query); &display_data($Tobj);
This is a very simple version of my current code... How to eliminate the second step with declared vars... and third step, passing the sql return values directly to the $Tobj, instead of using declared variables first? Thanks folks! PS: I've been on an intense learning curve with OO perl, as well as developing all new code using my own developed modules, (basically taking a bunch of variables and keeping them in their own name space, passing the object handle from sub to sub)... been away from development for a few years since my last post here https://perlmonks.org/?node_id=1129157 <- the response I got was amazing. Thanks to all that helped me out, and gave me very important guidance on how to proceed at the time in 2015. The folks here are fantastic! They need to add a tip jar here as I would love to donate a few bucks to everyone that responded then.

Replies are listed 'Best First'.
Re: Simple OO question...
by holli (Abbot) on Apr 30, 2019 at 02:28 UTC
    You want an Object Relational Mapper of some sort. One on CPAN is conveniently names ORM but there are others as well.


    holli

    You can lead your users to water, but alas, you cannot drown them.
      Wow. Thanks man. What an eye opener!

        DBIx::Class is widely considered the best ORM in Perl though there are a few others. It has a *steep* learning curve. I feel it pays off in extensibility and ease of maintenance. It does expect good DB design and can be punishing to poorly design schemata.

Re: Simple OO question...
by LanX (Saint) on Apr 30, 2019 at 10:44 UTC
    As a side note:

    your style is very Perl4, you don't need to undef $variables or call functions() with &ampersand

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      You're right. Understanding prototypes re Perl was never my strong suit. Old habits are hard to break.
        passing the object handle from sub to sub)
        &display_data($Tobj);

        alternatively call methods on the object

        $Tobj->display_data()

        For example

        #!/usr/bin/perl use strict; use lib './my_app'; use my_app::db; use my_app::page; my $dbh = my_app::db->connect(); my $page = my_app::page->new(); # create a page object for my $uid (1..3){ $page->init($dbh,$uid); # query database $page->display; }
        poj
Re: Simple OO question...
by trippledubs (Deacon) on May 01, 2019 at 03:20 UTC

    ORMs require a lot of investment, so do databases. There are contrarian opinions that say use neither. I'm not sure I could/would abandon SQL, definitely not for unstructured databases like Mongo or whatever else. I think that would be worse because you never really know what you are going to get from a query. Adding columns or whatever they're called without SQL seems like a real joy when some rows have it and some don't. I'm also not about to put the data in some long list of files, don't see that working out, trying to make an API correct without SQL. As the requirements change over time, yuck. BUT I think worth reading about anyways.

    Uncle Bob's (Author of Code Complete) NODB

    Seems like you would want to instantiate your objects with the data from the database (or wherever), so you can make those objects explode correctly if that data can't be reached. Probably already obvious. ORMs are great for the simple cases, but probably don't stay that way for long. Maybe other languages have great ORMs and I just haven't been exposed to them, but the top answer at SO for best Java ORM has "None" as preferred answer!! I mean, if your database accesses improve over time through the normal course of development, seems like you would lose a lot by not getting your hands dirty, trusting the ORM to get the best query for you. I've always grown frustrated trying to learn an ORM to do basic junk and given up. SQL only has 4 commands to learn. If you even count update since delete + insert.

    https://stackoverflow.com/questions/452385/what-java-orm-do-you-prefer-and-why

    Indexing a file by hand, very cool reading too. Takes the magic away from database indexes

    Index a file with pack for fast access

    Using indexing for faster lookup in large file

Re: Simple OO question...
by shmem (Chancellor) on Apr 30, 2019 at 15:56 UTC

    For simple stuff, I really like Class::Tables. It has some rough edges and presumes a convention for foreign keys, e.g. if you have a table users with an id column and a table groups with a foreign key which references the id column in users, that column in groups must be named users_id. It is quite small, but though old and discontinued, still a gem on CPAN imho. It is worth studying and nicely hackable.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'