Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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
Parameters not in order by using conifg::tiny
4 direct replies — Read more / Contribute
by bhushanQA
on Jul 06, 2015 at 09:03
    I have below code to read the parameters and section from my .ini file, but output is not in order. code:
    my $config = Config::Tiny->read('SQLdata.ini'); foreach my $section (keys %$config) { print "[$section]\n"; foreach my $parameter (keys %{$config->{$section}}) { print "$parameter = $config->{$section}->{$par +ameter}\n"; } #return $parameter; }
    SQLdata.ini has below sections and parameters:
    [insert] SQL1 = insert into table <table1> SQL2 = insert into table <table2> SQL3 = insert into table <table3> SQL4 = insert into table <table4> [Truncate] SQL1= truncate table <tablename1> SQL2= truncate table <tablename2> [update] SQL1 = update into <table1> SQL2 = update into <table2> output is: [Truncate] SQL2= truncate table <tablename2> SQL1= truncate table <tablename1> [insert] SQL4 = insert into table <table4> SQL2 = insert into table <table2> SQL3 = insert into table <table3> SQL1 = insert into table <table1> [update] SQL2 = update into <table2> SQL1 = update into <table1>
    how can I achieve output in order??
DBIx::Class - Count with prefetch creates nested query
2 direct replies — Read more / Contribute
by stepamil
on Jul 06, 2015 at 07:18

    Hi PerlMonks,

    I'm using DBIx::Class 0.082820 on Perl 5.10.1 and I have a situation when I'm trying to do count on table which is prefetched to another. Here's an example:

    my $count = Core::Models->resultset('Shipment')->search( undef, { prefetch => 'rma' } )->count();

    This creates this query:

    SELECT COUNT( * ) FROM (SELECT me.id FROM shipment me GROUP BY me.id) me

    And logically it should be this:

    SELECT COUNT( * ) FROM shipment me GROUP BY me.id

    Maybe I'm missing something. The real usecase is very complicated, but I've simplified it.

    Can you please help?

    Update (I've added the real models):

    package Models::Result::Shipment; use strict; use base 'DBIx::Class::Core'; __PACKAGE__->table("shipment"); __PACKAGE__->add_columns( "id", { accesor => 'id', data_type => "integer", extra => { unsigned => 1 }, is_nullable => 0, is_auto_increment => 1 }, "ref", { accesor => 'reference', data_type => "varchar", is_nullable => 1, size => 255 }, "api_merchant_id", { data_type => "integer", extra => { unsigned => 1 }, is_nullable => 0, default_value => 1 }, # ... lot of other fields bellow ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->has_many( "rma", "Models::Result::RMA", { "foreign.shipment_reference" => "self.ref", "foreign.merchant_id" => "self.merchant_id", }, { cascade_delete => 0 } ); 1; package Models::Result::RMA; use strict; use base 'DBIx::Class::Core'; __PACKAGE__->table('api_rma'); __PACKAGE__->add_columns( 'id' => { data_type => 'integer', is_nullable => +0, extra => { unsigned => 1 } }, 'shipment_reference' => { data_type => 'varchar', is_nullable => +0, size => 255 }, 'merchant_id' => { data_type => "integer", extra => { unsigned + => 1 }, is_nullable => 0 }, # ... lot of other fields bellow ); __PACKAGE__->set_primary_key('id'); 1;

    UPDATE 2: Here's what I have for now

    $count = Core::Models->resultset('Shipment')->search( undef, { prefetch => 'rma', select => { count => 'DISTINCT(me.id)' }, as => [ 'total_count' ], } )->get_column('total_count')->first();
XSub won't write to memory file
1 direct reply — Read more / Contribute
by syphilis
on Jul 06, 2015 at 04:39
    Hi,
    I have (for demonstration purposes) an XS sub that prints "hello" to a filehandle.
    It will print to STDOUT, STDERR and to any real filehandle (opened for writing) that gets passed to it.
    But, if I pass it a filehandle to a memory file, then nothing gets written to the referenced scalar.

    Here's the demo script:
    use warnings; use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'EOC'; void to_FH(FILE * stream) { fprintf(stream, "hello"); fflush(stream); } EOC my ($got1, $got2); $got1 = get_string_1(); chomp($got1); # just in case ... print "OK 1\n" if $got1 eq "hello"; $got2 = get_string_2(); chomp($got2); # just in case ... print "OK 2\n" if $got2 eq "hello"; sub get_string_1 { # Write to a temporary file open TEMPFILE, '+>', undef or die $!; to_FH(*TEMPFILE); seek TEMPFILE, 0, 0; my $ret = <TEMPFILE>; close TEMPFILE; return $ret; } sub get_string_2 { # Write to a memory file my $out; open MEM, '+>', \$out or die $!; to_FH(*MEM); #close MEM; # Makes no difference return $out; # returns undef ... why ? } __END__ For me, outputs: OK 1 Use of uninitialized value $got2 in scalar chomp at try.pl line 24. Use of uninitialized value $got2 in string eq at try.pl line 25.
    So ... sub get_string_1 works fine and passes the string to the temporary file associated with the filehandle, from which I successfully retrieve what was written.

    With sub get_string_2, my expectation is that the string "hello" will be written to the sub's $out - but that's not happening, and $out remains uninitialized.

    What needs to be done here, to get it working as I expect ?

    I also tried an alternative version of the XSub:
    void to_FH(PerlIO * stream) { FILE * stdio_stream = PerlIO_exportFILE(stream, NULL); fprintf(stdio_stream, "hello"); fflush(stdio_stream); PerlIO_releaseFILE(stream, stdio_stream); }
    But this made no discernible difference to the behaviour.

    Cheers,
    Rob
Connecting to WSDL
2 direct replies — Read more / Contribute
by sumeetgrover
on Jul 06, 2015 at 03:06

    Hello Monks,

    I have received the joyful task of writing Perl code to connect to a WSDL web service, something I haven't done before.

    I tried connecting to the web service using SOAP::Lite using the examples demonstrated on its CPAN page and other Perl forums. That code didn't work.

    I have, however, connected to the WSDL service using SOAP::WSDL but the CPAN site recommends not to use this module. This leaves me to my question:
    How can I connect to a WSDL web service reliably in Perl?

Return a value
4 direct replies — Read more / Contribute
by bhushanQA
on Jul 06, 2015 at 03:00
    I have written below code and wana add in the module
    sub Read_INI_files_get_initialData{ my ( %ini_file, $ini_sect ); tie %ini_file, 'IniFiles', ( -file => "/home/testtool/config/InitialData.ini" ); foreach $ini_sect ( keys %ini_file ) { %$ini_sect = %{ $ini_file{$ini_sect} }; }
    Return a above subroutine,which is working perfectly, but I want to return ini files' section in to hash, but it is giving error "Read_INI_files.pm did not return a true value" My Ini file contains below configuration:
    [section1] SQL1 = select * from .... SQL2 = Insert into table <tablename> and so on.
    what and how should I return value to hash.
using Safe.pm
2 direct replies — Read more / Contribute
by Datz_cozee75
on Jul 06, 2015 at 01:23

    Hello Monks,

    I had read up on the new nodes, so I went to the tutorials to select something that I thought might be interesting and improve my perl game, settling on 181977, which has a good example of Tk that a beginner can replicate. I downloaded and executed the script, finding that it produces a window that shows the contents of a directory, has another for a listing of a selected file, and a final one for the output if it's a perl script. It's billed as an "obfu decoder ring," as one can, allegedly, see the output of a script or with pasted-in text without giving it full-reign.

    At the end of the thread, someone posted an obfuscated script, which I turned into a .pl file to see what it might reveal, getting no output. What's more, the poster claimed you would be more or less brainless to run it without the use of Safe.pm.

    My question is: how do I use Safe.pm effectively? Assume that I have an obfu that I can make neither heads nor tails of. I won't list the one at the end of this node, because I don't know whether it is pernicious or not. What I have so far is this:

    #!/usr/bin/perl use Safe; use 5.010; say "safe";

    Thanks for your comment,

Failed test of Net::SNTP::Client module
3 direct replies — Read more / Contribute
by thanos1983
on Jul 05, 2015 at 18:53

    Dear Monks,

    I am trying to write my first Perl module (Net::SNTP::Client). I have post several questions over and over trying to get some help from the experts. So unfortunately now I got stack again, with the testing and verification of the module.

    So first thing first. The module code if someone want to test it can be found here RFC: Net::SNTP::Client v1.

    So as a next step I should explain what I am doing.

    I was following the instructions from How to make a CPAN Module Distribution where I completed all steps even the testing part. Now I am trying to verify my module before I uploaded on CPAN and I got stack.

    I am trying to install the module manually by applying the following commands:

    perl Makefile.PL make make test sudo make install

    Everything seems to be fine up to the point of make test. I am getting the following error:

    The strange part is that I am executing the test manually inside the /t directory through the command: perl Net-SNTP-Client.t everything compiles and executes just fine.

    Sample of output:

    So the question is why is it failing, when manually is not? What I am missing?

    Bellow is the test cases code in case that someone would like to replicate the error.

    Although the test cases that are failing are 2 out of 15 I tried to fix them because the error is really simple. I have commended the fix, but still the error is not stopping to appear. Where I am going so wrong?

    Thank you in advance for your time and effort to assist me.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
AUTOLOAD block
1 direct reply — Read more / Contribute
by hurricup
on Jul 05, 2015 at 15:30

    Hello Monks!

    In the SelfLoader.pm I found AUTOLOAD block name. Syntax is familiar with BEGIN/CHECK/etc. but autoloading being done using AUTOLOAD sub as far as i know. What is it?

Perl backticks not returning output
2 direct replies — Read more / Contribute
by dannyjmh
on Jul 05, 2015 at 13:49

    Hey there good monks! I'm writing a small perl script to create 1000 files ("test_set_$i" here) with 400 gene IDs each (one per line). Then I must search for them in 4 other files (stored in @results). I'd rather use unix grep -f instead of doing a lot of Perl open/close and hash creating and accessing, which would be way too slower. The problem comes after iteration #204, in which the backticks call won't get the output in $tot_length and $? gets -1. Any idea why, please? I'm using Perl v5.14.2 in 64-bit Ubuntu 14.04. Here's that part of the code only:

    for my $i(0..999){ + + my $tot_length = `grep -f test_set_$i $ARGV[2] | awk '{cnt += \$2}END{printf "%d", +cnt}'`; `grep -f test_set_$i $results[0] | awk '{print \$1}' | sed 's/>>//'| sort | uniq -c | awk '{print \$1*1000/$tot_length,\$2}' > out`; tie @{$files{'first'}[$i]},'Tie::File', "out"; `grep -f test_set_$i $results[1] | awk -F" : " '{print \$2}' | sort | uniq -c| awk '{print \$1*1000/$tot_length,\$2}' > out`; tie @{$files{'second'}[$i]},'Tie::File', "out"; `grep -f test_set_$i $results[2] | sed 's/ targets sites//' | sed 's/.*>//' | awk -F": " '{cnt[\$1]+=\$2} END{for (x in cnt){print cnt[x]*1000/$tot_length,x}}' > out`; tie @{$files{'third'}[$i]},'Tie::File',"out"; `grep -f test_set_$i $results[3] | awk '{print \$2}' | sort | uniq -c| awk '{print \$1*1000/$tot_length,\$2}' > out`; tie @{$files{'fourth'}[$i]},'Tie::File',"out"; `grep -f test_set_$i $results[4] | awk '{print \$2}' | sort | uniq -c| awk '{print \$1*1000/$tot_length,\$2}' > out`; tie @{$files{'fifth'}[$i]},'Tie::File',"out"; }

    Thanks a lot!

DBIx::Dump Title
1 direct reply — Read more / Contribute
by ScottM
on Jul 05, 2015 at 12:58
    Hi Monks, is there a way to add a Title to my worksheet created with DBIx::Dump? TIA

Add your question
Title:
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!
  • 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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (11)
    As of 2015-07-07 09:11 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (88 votes), past polls