質問

I often save a lot of data into a hash var, or get data according to the conditions. It is not convenient, so i want a module with which accessing data with SQL as a NoSQL. I found DBD::RAM, but is there a smaller module?

For example: a hash data like a MySQL table:

{
   "table": "company",
   "rows" : [
       {
         "name": "baidu",
         "location": "China"
       },
       {
         "name": "taobao",
         "location": "China"
       }
    ]
}

In general, inserting a record like this:

my %new_row = (name=>xxx, location=>yyy);
push (@{$hash->{rows}}, \%new_row);

If I do that, there will be a lot of hash variable, so I want to do it more like this:

$handle->insert('insert into company values("xxx", "yyy")');

my ($name, $location) = $handle->select_unqiue_record(<<"_EOC_";
    select name, location from company where name="baidu"
_EOC_);
役に立ちましたか?

解決

I recommend https://metacpan.org/module/DBIx::DataModel.

Once you setup a Schema describing your target table - and you can do this automatically by reverse engineering - you can insert your hash directly like this:

my $table = $schema->table($table_name);
my $id = $table->insert($hash_ref);

In fact you can pass DBIx::DataModel an array of hash_refs (as per your question) and it will insert each of these for you. See the documentation at: https://metacpan.org/module/DBIx::DataModel#Insert

他のヒント

If I understand you correctly, you're storing data in complex structures that you find hard to manipulate. The reason you're asking about NoSQL type of stuff is that you want an easy way to store and manipulate your data.

It's then time to roll up the sleeves and learn Object Oriented Perl.

Creating Perl objects is a good way to handle your complex data structure, and it's really not all that hard to learn. I normally write classes on the fly and just declare them at the end of my program.

This is the data structure you had in your initial post as a Company class:

package Company;

sub new {
    my $class    = shift;
    my $name     = shift;
    my $location = shift;

    my $self = {};
    bless $self, $class;

    $self->Name($name);
    $self->Location($location);
    return $self;
}

sub Name {
    my $self     = shift;
    my $name     = shift;

    if ( defined $name ) {
        $self->{NAME} = $name;
    }
    return $self->{NAME};
}

sub Location {
    my $self      = shift;
    my $location  = shift;

    if ( defined $location ) {
        $self->{LOCATION} = $location;
    }
    return $self->{$LOCATION};
}

That's all there is to it. Now I can easily create and manipulate my companies without worrying about manipulating hashes of hashes, or trying to remember exactly how I structured my data:

# Read in all of the companies from $company_file into @company_list

open my $company_fh, "<", $company_file;
my @company_list;
while ( my $line = <$company_fh> ) {
    chomp $line;
    my ($name, $location) = split /:/, $line;
    my $company = Company->new( $name, $location );
    push @company_list, $company;
}
close $company_fh;

Later on, I can manipulate my companies like this:

#Print out all Chinese Companies
for my $company ( @company_list ) {
    if ( $company->Location eq "China" ) {
        say $company->Name . " is located in China.";
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top