Hi 2 all. I'm trying to wrap into OOP form some chunks of my code. I mean to make a module that will be able to perform different complex operations MySQL DB (based on custom logics) and return data in a simple form (hash or scalar). For example something like:

1. FetchDataset($label_id,$role_id,'dataset') should return a hash where key\value pares will be created from table column names\values returned by query.

2. GetValue('log_file') will check if "dataset" exists and in case of success will return this value.

So this where I got stuck. I'm not sure if I understand how to store data returned from a DB without pre defined keys in "$self" reference like $self = {label_id => shift, role_id => shift,....} which is not to good for me because I'm trying to make this module to be able to retrieve data from different tables.
#!/usr/bin/perl -w package Conf; use DBI; my $dbh = DBI->connect("DBI:mysql:database=framework;host=192.168.1.1; +port=3306;", "test", "test", {'RaiseError' => 1}); sub new { $class = shift; my $self = {}; die "ERROR: Connection to DB failed.Dying." if !$dbh; bless $self, $class; return $self; } sub FetchDataset { my ( $self,$label_id,$role_id,$data_type ) = @_; print "$self\n"; print "$label_id\n"; print "$role_id\n"; print "$data_type\n"; if ($data_type eq "dataset") { $sth = $dbh->prepare(qq{ SELECT `cfg_role_attrs`.`attr +_key` , `cfg_datasets`.`attr_value` FROM `cfg_datasets` LEFT JOIN `c +fg_role_attrs` ON `cfg_datasets`.`attr_id` = `cfg_role_attrs`.`attr_i +d` WHERE `cfg_datasets`.`role_id` = $role_id}); $rv = $sth->execute(); if (!$rv) { die "Building 'global' dataset for: |$label_i +d - $role_id : FAILED\n"; } $self = $sth->fetchrow_hashref(); return $self; } } sub GetValue { my ($key) = @_; return $self->{$key}; } 1;

And this is example how I'm planning to use it:

#!/usr/bin/perl -w use warnings; use strict; use Conf; my $object = Conf->new(); my $d_set = $object->FetchDataset('1','1','dataset'); my $value = $object->GetValue('log_file'); print "Coresponding value is : $value\n";
Thanks in advance for any help.

In reply to OOP Data extractor by kazak

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.