Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Seekers of Perl Wisdom

( #479=superdoc: print w/replies, xml ) Need Help??

If you have a question on how to do something in Perl, or you need a Perl solution to an actual real-life problem, or you're unsure why something you've tried just isn't working... then this section is the place to ask. Post a new question!

However, you might consider asking in the chatterbox first (if you're a registered user). The response time tends to be quicker, and if it turns out that the problem/solutions are too much for the cb to handle, the kind monks will be sure to direct you here.

User Questions
using json post in mysql stored procedure
1 direct reply — Read more / Contribute
by mhanna2755
on Jan 23, 2017 at 18:28

    I am trying to use json data as parameters in a mysql store procedure. I am getting a 502 error when i send my post to my perl script. If I manually enter the parameters in my script it works, so i have narrowed down my issue to $sth->execute($input->{to}, $input->{from}, $input->{id}, $input->{body});

    use strict; use warnings; # # need CGI, JSON, # MIME::LITE for email, # and DBI/DBD::mysql for DB operations. # use CGI (); use JSON (); use DBI; use DBD::mysql; use MIME::Lite; # # Declare variables # my ($status, $email, $input); my $q = CGI->new; my $json = JSON->new->utf8; if ($q->param('POSTDATA')) { $input = $json->decode( $q->param('POSTDATA') ); } else { &SimpleJSONResponse("error","no JSON data found"); exit(0); } # # test that all sms values exist # if not, return error # unless (($input->{to}) && ($input->{from}) && ($input->{body}) && ($in +put->{id})) { # # if we don't get the required fields from flowroute, # return an error # &SimpleJSONResponse("error","required fields not present"); } else { # # we received the required fields from flowroute # connect to database # my $data_source = "DBI:mysql:database=flowroute;host=localhost"; my $user = "user"; my $password = "password"; my $dbh = DBI->connect($data_source, $user, $password) or die &Simp +leJSONResponse("error","cannot connect to db"); # # run stored procedure add_sms # stores sms to database and returns the email address # my $sql = "call add_sms(?, ?, ?, ?)"; my $sth = $dbh->prepare($sql) or die &SimpleJSONResponse("error","c +annot prepare sql"); $sth->execute($input->{to}, $input->{from}, $input->{id}, $input->{ +body}); } # # Subroutines # sub SimpleJSONResponse() { my($key, $value) = @_; print $q->header(-type => "application/json", -charset => "utf-8"); print $json->encode({ $key => $value }); }
NET::LDAP and objectGUID
No replies — Read more | Post response
by ksublondie
on Jan 23, 2017 at 17:06
    Hi monks!

    I need to use a read-only unique identifier field for ldap objects. My research has taken me to "objectGUID". However, if I take a query result set and turn right back around to use the objectGUID values for my filter values, I only get hits on about 90% of the original objects. From my tests so far, it appears to be the same objects missing each time.

    my $users=$ldap_session->search( base=>$CONF{ldapuserbase}, filter=>$filter, attrs=> ['distinguishedName','sAMAccountName', +'homeDirectory','displayName','objectSid','objectGUID'] ) or die "err +or searching tree:$@\n"; my @entries = $users->sorted('displayName'); print "done.\nFound ".(scalar @entries)." entries.\n"; my $notfound=0; foreach my $e (@entries){ my $guid=$e->get_value('objectGUID'); my $name=$e->get_value('displayName'); my $thisuser=$ldap_session->search( base=>$CONF{ldapuserbase}, filter=>"(objectGUID=$guid)", attrs=> ['distinguishedName','sAMAccountName', +'homeDirectory','displayName','objectSid','objectGUID'] ) or die "err +or searching tree:$@\n"; my @user=$thisuser->sorted('displayName'); if(@user){ #do nothing }else{ print "can't find $name\n"; ++$notfound; } } print "not found=$notfound\n"; $ldap_session->unbind;
    I should be able to grab the value and turn right back around and query for it, right? Does anyone have any insight as to what's going on?

    ETA: I converted the "not found" objectGUID values to a readable hex string and displayed the values. All the objects have the value of "4" at the start of the 3rd subset. aka, XXXXXXXX-XXXX-4XXX-XXXX-XXXXXXXXXXXX


OT: Storing encryption keys securely
1 direct reply — Read more / Contribute
by Beatnik
on Jan 23, 2017 at 09:42
    Slightly OT but here goes.

    I'm writing some glue code that will store an encrypted password in a database. I'm looking at different approaches on making all this as safe as possible. Hashing the password (for verification) is not really an option as I will need access to the clear-text (to pass it on to another class). I'll be taking some steps to avoid breaking the encrypted password easily but what about storing the key used to encrypt? In an ideal world, the key to encrypt won't be accessible by anyone but how can I make sure? In some way, the key must be stored somewhere.. Even with a keychain of some kind, I will still need to store the keychain key.


    ... I'm belgian but I don't play one on TV.
Is it possible to retrieve the coding sequence of a gene from NCBI GenBank database using perl ?
4 direct replies — Read more / Contribute
by supriyoch_2008
on Jan 23, 2017 at 09:28

    Hi Perlmonks,

    I am interested in retrieving the coding sequence (cds) of a gene using accession number in a perl script. I searched for a script in the web and found a script that retrieves the entire sequence in FASTA format (not the cds directly). In fact, there is a hyperlink at the word CDS in the particular GenBank page of NCBI and when the link is clicked it shows the cds of the gene. Is it possible to get the cds sequence directly using a perl script and internet connectivity? I welcome suggestions from the perlmonks. Here goes the script:

    ######################################################### # Perl code to get complete sequence in FASTA format from GenBank page ######################################################### #!/usr/bin/perl use warnings; use strict; use Bio::DB::GenBank; use Bio::SeqIO; my $gb =new Bio::DB::GenBank; my $acc="NM_021817"; my $seq1=$gb->get_Seq_by_acc($acc); my $sequence=$seq1->seq(); my $description=$seq1->desc(); print "\n $description $sequence\n"; exit;

    The script produces the following output in cmd screen:


    I would like to get the cds sequence as given below:

Properly testing self-compiled character-encodings
2 direct replies — Read more / Contribute
by yulivee07
on Jan 23, 2017 at 07:15
    Hi Perlmonks, I am searching for a proper way to test various character-encodings if they works as expected on my platform.

    I have some special IBM-character codepages I need to include for my Platfrom (AIX 7.2). I have built these according to the manual delivered with enc2xs

    However, I do not trust them. I took a look with the strings utility whether there are characters in the produced binary file:
    $ strings $
    strings just returns nothing. Using another C-Compiler solved this problem and produced binary files that contain characters.

    So now I want to create a test to see if the encoding is working properly. I have an older machine where all those character-encodings are working and installed, so I could generate files containing correct information there and copy them over to the new machine for testing.

    I am not entirely shure about a good testing strategy. I thought of testing characters beyond the 128th character (as below theyy would be all equal as it is ASCII). Does this seem reasonable?

    I looked into the Encode distribution in search for unit-tests for encodings but didn't find much. Are there best practices for cases like this?

    Regards, Yulivee
Converting XML to array of array.
3 direct replies — Read more / Contribute
by ravi179
on Jan 23, 2017 at 04:36

    How can i convert XML to an array of array.

    <?xml version="1.0" encoding="UTF-8"?> <books> <!-- Programming Perl 3ed --> <book id="1"> <title>Programming Perl</title> <edition>3</edition> <authors> <author> <firstname>Larry</firstname> <lastname>Wall</lastname> </author> </authors> <isbn>0-596-00027-8</isbn> </book> <!-- Perl & LWP --> <book id="2"> <title>Perl &amp; </title> <edition >1</edition> <authors> <author> <firstname>Sean</firstname> <lastname>Burke</lastname> </author> </authors> <isbn>0-596-00178-9</isbn> </book> <book id="3"> <!-- Anonymous Perl --> <title>Anonymous Perl</title> <edition>1</edition> <authors /> <isbn>0-555-00178-0</isbn> </book> </books>

    I can use only XML::Simple,XML::Parser only,while using XML::Simple I was unable to print the data

    my $xl=XMLin('C:/perlpractice/dec8.xml',ForceArray=>1); for$key(keys %$xl) { for $s(keys %{$xl{$key}}) { print $s; } }

    Can anyone please help me how to access hashes of hashes with array reference

Perl and MySql - checking tables for column names
2 direct replies — Read more / Contribute
by bizactuator
on Jan 23, 2017 at 03:11
    I had to take over code from someone that died.

    They had this in the code:

    my @_mb = $dbh->tables; my $_tablesAffected = 0; foreach $_k (@_mb) { # Ran code in here... }

    It appears the $dbh->tables is no longer working.

    They were running through the tables in that foreach, pulling the tables in:

    my $_sql = "select * from $_k LIMIT 0"; my $sth2 = $dbh->prepare($_sql); $sth2->execute(); my @_mb2 = @{$sth2->{NAME}}; foreach $_k2 (@_mb2) { # then looking for column names with username: if($_k2 =~ /username/i) { # do what they are looking for } next; }

    So since that is no longer working, how can they run through the database like that, looking for the tables with the column they are looking for?

    It is for people who want to change their username to something else, the programming programmed everything together by the username, because they said they would never allow changes, but they had so many requests, they changed their mind, so now, they have to be changed, so need to look through the tons and tons of tables, in case it is somewhere.

    The server was upgraded and it quit working, I think it is because that $dbh->tables no longer works.

    Can someone point to me on how to use the new catalog in that to make it work again?

is @inc on windows ALWAYS in env vars?
1 direct reply — Read more / Contribute
by enrgyxprt
on Jan 22, 2017 at 11:04

    A series of uninstal, new install (ActiveState) and then Strawberry to try Padre, has left me hunting down
    why after using PPM to install something, my package still isn't found...

    I see in ther error @INC is C:\perl64... But Perl-v (or -V, I don't remember now) showed @INC C:\perl...

    Going to system properties, Advanced, Env Var's
    PATH only has C:\perl... paths Not one mention of C:\perl64 in it.
    So how is @INC looking in C:\perl64... ?

Unable to flush stdout: Invalid argument
3 direct replies — Read more / Contribute
by bakiperl
on Jan 22, 2017 at 08:27

    I have recently upgraded perl for windows from version 5.16 to version 5.24 and suddenly two of my cgi scripts started to produce the following error message:

    Unable to flush stdout: Invalid argument?

    I really have no idea where to look. Any help is appreciated.

    Thank you.
Readonly references, replicating data structures
3 direct replies — Read more / Contribute
by nikmit
on Jan 22, 2017 at 06:37

    When I initially hit this it seemed incomprehensible... then I figured out why it's happening but not yet how to fix it - so here I am humbly asking for your wisdom.

    I expected the below code to create a fresh copy of @arr every time gimme() is executed. Instead it creates a new reference for @arr but reuses all of the nested references. The result is that gimme() returns the last box with its content, rather than a fresh empty box which is what I want...

    I considered using Readonly or a configuration file on disk to ensure I get an empty box every time, but both seem clunky and slow. The actual data structure is nested to 5-6 levels and while it is not huge, it will be executed often.

    I suspect turning the data structure into an object may be the right path, but I'm yet to pick that side of perl up... What would be your advice?

    #!/usr/bin/perl -w use strict; use Data::Dumper; BEGIN { my @arr = ( { box => { attr => { this => 'that', foo => 'bar', }, content => {}, }, }, ); sub gimme { my @result = @arr; return \@result; } } my $box1_ref = gimme(); $box1_ref->[0]->{white_box}->{content}->{apples} = 5; print "tracing: $box1_ref -> $box1_ref->[0] -> $box1_ref->[0]->{white_ +box} -> $box1_ref->[0]->{white_box}->{content}\n"; print Dumper $box1_ref->[0]->{white_box}->{content}; my $box2_ref = gimme(); print "tracing: $box2_ref -> $box2_ref->[0] -> $box2_ref->[0]->{white_ +box} -> $box2_ref->[0]->{white_box}->{content}\n"; print Dumper $box2_ref->[0]->{white_box}->{content};

    Result of that code is:

    tracing: ARRAY(0x20d9e70) -> HASH(0x1fcf638) -> HASH(0x20d9db0) -> HAS +H(0x20d9de0) $VAR1 = { 'apples' => 5 }; tracing: ARRAY(0x1fab0f0) -> HASH(0x1fcf638) -> HASH(0x20d9db0) -> HAS +H(0x20d9de0) $VAR1 = { 'apples' => 5 };

Add your question
Your question:
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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (5)
    As of 2017-01-24 03:07 GMT
    Find Nodes?
      Voting Booth?
      Do you watch meteor showers?

      Results (200 votes). Check out past polls.