Hi,

I am struggling to figure out how to use threads::shared variables, in this particular case Hash, amongst multiple threads. To make things as simple as I can right now I am not trying to run in threads but just code what I hope is the simplest of verifications. In this particular case I get a SQLite reference back from a connect but cannot figure out the correct reference/dereference syntax. In this particular example in the 3rd section of the code the SQLite prepare fails because the prior connect reference isn't the same, in my large use this would have a shared Hash of SQLite 'open' DB's (the connect references) which could be cycled through doing queries.

From the documentation on threads::shared I am 'sure' my lack of understanding is related to the comment "Shared variables can only store scalars, refs of shared variables, or refs of shared data (discussed in next section):" but I simply do not understand enough to figure out what to do.

Any help is appreciated.

Doug

the code example;

[root@itx2 ~]# cat x7.pl use DBI; use threads; use threads::shared; my $driver = 'SQLite'; my $database = ''; my $dsn = ''; my $userid = ''; my $password = ''; my $dbh; my $dsn; my $stmt; my $sth; my $rv; my @row; my %hash; my %hsh :shared; my $int :shared; my $String; my $Sdatabase; $Sdatabase = $ARGV[0]; $dsn = "DBI:$driver:dbname=$Sdatabase"; $dbh = DBI->connect($dsn, $userid, $password, { PrintError => 1, Ra +iseError => 1, AutoCommit => 0, ReadOnly => 1 }); $stmt = qq(select count ( * ) from asnipv4 ); $sth = $dbh->prepare( $stmt ); $rv = $sth->execute(); while( @row = $sth->fetchrow_array()) { $Iknt++; $String = join ',', @row; print "$Iknt -> $String \n"; } $dbh->disconnect(); print 'one' . "\n"; $dbh = DBI->connect($dsn, $userid, $password, { PrintError => 1, Ra +iseError => 1, AutoCommit => 0, ReadOnly => 1 }); $hash{$Sdatabase} = $dbh; $stmt = qq(select count ( * ) from asnipv4 ); $sth = $hash{$Sdatabase}->prepare( $stmt ); $rv = $sth->execute(); while( @row = $sth->fetchrow_array()) { $Iknt++; $String = join ',', @row; print "$Iknt -> $String \n"; } $hash{$Sdatabase}->disconnect(); delete $hash{$Sdatabase}; print 'two' . "\n"; $dbh = DBI->connect($dsn, $userid, $password, { PrintError => 1, Ra +iseError => 1, AutoCommit => 0, ReadOnly => 1 }); $hsh{"$Sdatabase"} = $dbh; $stmt = qq(select count ( * ) from asnipv4 ); $sth = $hsh{"$Sdatabase"}->prepare( $stmt ); $rv = $sth->execute(); while( @row = $sth->fetchrow_array()) { $Iknt++; $String = join ',', @row; print "$Iknt -> $String \n"; } $hsh{$Sdatabase}->disconnect(); delete $hsh{$Sdatabase}; print 'tre' . "\n"; exit; [root@itx2 ~]#

the execution failure;

[root@itx2 ~]# perl x7.pl /opt/asd/sql/dbi-indexed-GeoLite2.db-sqlite 1 -> 512468 one 2 -> 512468 two Invalid value for shared scalar at x7.pl line 60. [root@itx2 ~]#

In reply to threads::shared referencing confusion by Anonymous Monk

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.