How to store the Perl output - instead of printing?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
sayhello_to_the_world
Posts: 77
Joined: Mon 24 Dec 2012, 15:19

How to store the Perl output - instead of printing?

#1 Post by sayhello_to_the_world »

goo day

new to perl - want to store the output - instead of printing out

Code: Select all


  use Parse::CPAN::Authors;
 
      # must have downloaded
      my $p = Parse::CPAN::Authors->new("01mailrc.txt.gz");
      # either a filename as above or pass in the contents of the file
      my $p = Parse::CPAN::Authors->new($mailrc_contents);
 
      my $author = $p->author('LBROCARD');
      # $a is a Parse::CPAN::Authors::Author object
      # ... objects are returned by Parse::CPAN::Authors
      print $author->email, "\n";   # leon@astray.com
      print $author->name, "\n";    # Leon Brocard
      print $author->pauseid, "\n"; # LBROCARD
 
      # all the author objects
      my @authors = $p->authors;
      
      
      
      

what do i want to do: i want to store all the output in the mysql-db

how to put the output of the request to the db

have perl :: DBI installed
also the mysql-db is up and running


cf: https://metacpan.org/source/LBROCARD/Pa ... .27/README




regarding the db-things: well i am pretty new to perl-tasks.

but with the above mentioned module - i think i can learn alot - it is quite very simple. And with this i can play around - and try to find out how to store the data into a mysql db.

well could the results that i get be regarded as a perl object (reference to array of references) like the below:

Code: Select all

my $a = [ [$a, $ab, $c ], [$a, $b, $c] ] ;
and need to store it on the DB then retrieve it.

i look for a good mechanism to serialize it and then store it on the DB?


hmm - if i want to store:

Code: Select all

use Storable 
use DBI; 
 
# ... connect to database 
# Store 
my $data = [ [$a, $b, $c ], [ $a, $b, $c ] ]; 
my $bytestream = nfreeze $data; 
$dbh->do('insert into table (field) values(?)', undef, $bytestream);


by the way: What about Data::Dumper?

User avatar
GustavoYz
Posts: 883
Joined: Wed 07 Jul 2010, 05:11
Location: .ar

#2 Post by GustavoYz »

Hi, Im not understanding the question.
What do you mean by serialize it?

As long as you have the data retrieved, store it is easy as make a query, prepare and run it (use placeholders '?' wisely). If the structure is complex, you may need to find a procedure to retrieve it in order, but thats specific: it depends on the problem to solve.

Being a generic scenario, Ill post you a generic function (:P) I use sometimes. It will print nested data structures, hash and arrays. May be of use, maybe not.

Code: Select all

sub iterate {
    my $input = shift;
    unless (ref $input) {
        print "$input\n";
    } elsif (ref $input eq 'ARRAY') {
        iterate($_) for @$input;
    } elsif (ref $input eq 'HASH') {
        for (keys %$input) {
            print "$_\n";
            iterate($input->{$_});
        }
    } else {
        print ref $input,"\n";
    }
}

Post Reply