Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
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
Time::HiRes gettimeofday producing timestamps out of order
No replies — Read more | Post response
by 1nickt
on May 06, 2016 at 09:30

    Hi all,

    I am using Time::HiRes to create timestamps in my program which I insert into the DB with the record, rather than using the DB's timestamp function, because of the unpredictability of when the record might actually be inserted.

    In my logger:

    use Time::HiRes qw/ gettimeofday /; sub log { my ( $self, $args ) = @_; my $data = { time => join('.', ( gettimeofday )), pid => $args->{'pid'}, action => $args->{'action'}, }; $self->write( $data ); }
    which is called, sequentially, three times, with actions 'finish', 'sleep', 'complete'.

    Most of the time the results are as expected, but sometimes the order is off. Example (note that the DB insert IDs follow the order of the program):

    mysql> select id, pid, action, from_unixtime(time) from queue_logger o +rder by id; +-------+-------+----------+----------------------------+ | id | pid | action | from_unixtime(time) | +-------+-------+----------+----------------------------+ | 83640 | 15218 | finish | 2016-05-06 12:38:49.834030 | | 83642 | 15218 | sleep | 2016-05-06 12:38:49.251690 | <- timestamp +ed first, but inserted second! | 83683 | 15218 | complete | 2016-05-06 12:39:19.707134 | +-------+-------+----------+----------------------------+
    Now I have read up on Time::HiRes and the various gotchas that occur related to the system clock. But I don't get this because (a) the code is doing the calls sequentially (b) the DB is inserting the rows in the same sequence, but (c) the timestamps are not in the same order.

    Any help to understand what's going on here would be greatly appreciated.


    The way forward always starts with a minimal test.
Change create file date
2 direct replies — Read more / Contribute
by artem78
on May 06, 2016 at 08:50
    utime function can change only access and modification date of file. How can I change create date in Windows?
Conditional creation of files - global or scoped filehandle
3 direct replies — Read more / Contribute
by Anonymous Monk
on May 06, 2016 at 06:05

    Wherever possible I try to utilise a three-argument open and avoid bareword filehandles, such as:

    open my $out, '>', "$outfile" or die "$!"

    However there are cases when the creation of a subset of files in my script is dependent upon arguments provided by the user:

    if ($ARGV[0] eq "Example") { open my $out, '>', "$outfile" or die "$!" }

    As a side note: i'm not sure that this is an appropriate means of conditionally opening files.

    This presents the problem that the filehandle is no longer global and thus inaccessible later on in the script. Is there a way around this or is this a situation where using a bareword filename is acceptable? Should the filename be declared outside of the loop first?

    Data being printed to $out is being created within a loop.

compare third column of one file with the first column of another
3 direct replies — Read more / Contribute
by Anonymous Monk
on May 06, 2016 at 04:39
    I have two files ptt.txt and mapped.txt :
    ptt.txt Location Strand Length PID Gene Synonym Code COG + Product 1..1524 + 5 +07 15607143 dnaA Rv0001 - COG0 +593L chromosomal replication initiato +r protein DnaA 2052..3260 + 402 15607144 dnaN Rv0002 - COG0592L + DNA + polymerase III subunit beta 3280..4437 + 385 15607145 recF Rv0003 - +COG1195L + DNA replication/repair protein RecF
    mapped.txt gi|50953765|ref|NC_002755.2|-587160 83 gi|448814763|ref|NC_00096 +2.3| + 1329307
    my code is
    $file="mapped.txt"; open (FH1, "$file"); open (FA, ">>allassignment.txt"); while ($seq1=<FH1>) { @seq1=split (/\t/, $seq1); $pos1=@seq1[3]; open (FH, "ptt.txt"); while ($seq=<FH>) { @seq=split (/\t/, $seq); $pos=@seq[0]; $pos8=@seq[8]; chomp $pos8; @pos=split(/\.\./, $pos); $pos_start=@pos[0]; $pos_end=@pos[1]; for ($i=$pos_start, $i<=$pos_end, $i++) {if ($i=~m/^$pos1$/) {$position1=@seq1[0]; $position8=@seq1[8]; $position9=@seq1[9]; print FA $i."\t".$pos1."\t".$pos."\t".$pos8."\t".$pos1."\t".$position1 +."\t".$position8."\t".$position9."\n" ; last;} } }}}
    I want to find out if the third column of mapped.txt lies within the range of first column of ptt.txt
compare two files to get a match
2 direct replies — Read more / Contribute
by Anonymous Monk
on May 06, 2016 at 03:38
    I have two files ptt.txt and mapped.txt :
    ptt.txt Location Strand Length PID Gene Synonym Code COG + Product 1..1524 + 507 15607143 dnaA Rv0001 - COG0 +593L chromosomal replication initiator protein DnaA 2052..3260 + 402 15607144 dnaN Rv0002 - COG0592L + DNA polymerase III subunit beta 3280..4437 + 385 15607145 recF Rv0003 - COG1195L + DNA replication/repair protein RecF
    mapped.txt gi|50953765|ref|NC_002755.2|-587160 83 gi|448814763|ref|NC_00096 +2.3| 1329307
    I want to find out if the third column of mapped.txt lies within the range of first column of ptt.txt. if it lies within that range then print the row of ptt.txt along with the third column value of mapped.txt please if anyone can help me with this
Setting CommandTimeout
2 direct replies — Read more / Contribute
by amitvalia
on May 05, 2016 at 16:45
    Hi All.

    I'm trying to set the connectionTimeout and CommandTimeout properties in ASP using PerlScript. I'm doing this because some of my queries are coming back with

    Query Timeout Expired error.

    Hence I try to set the commandTimeout parameter to 120. That's when I get another error.

    PerlScript Error error '80004005' (in cleanup) Can't modify non-lvalue subroutine call

    My code is

    $Conn = $Server->CreateObject("ADODB.Connection"); $Conn->Open( "Provider=SQLOLEDB;User ID=USER;Password=PASSWORD;Initial + Catalog=DB;Data Source=SERVER" ); $Conn->CommandTimeout = 120; $timeout = $Conn->CommandTimeout; $sql = "Select Col1,Col2 from Table1 "; $RS = $Conn->Execute( $sql,CommandTimeout=120); if($Conn->Errors->{Count} > 0) { %> Error is <% =$Conn->Errors(0)%> ...
perl 5.010 Can't locate object method "seek" via package "IO::Handle"
1 direct reply — Read more / Contribute
by jandrew
on May 05, 2016 at 16:08

    I confess that I never attempted to seek on an IO::Handle when I was writing for perl 5.10, but I am currently attempting to release a package that includes this action and I would like to verify support back to 5.10. I get the title error in my travis-ci tests (with the container based Debian). Any thoughts on how to get around this? I suspect some incremental support occurred from 5.012 onward and that there is a known way around it for perls 5.010 and earlier but I'm unable to find it with a search. I would appreciate any suggestions.

    The top level docs aren't complete on the GitHub page yet

pipe issue (linux)
3 direct replies — Read more / Contribute
by leostereo
on May 05, 2016 at 15:49
    Hi guys,
    I was trying to pipe two perl scripts
    I have a data provider and a data process scritp.
    First the data provider is
    dhcp_line_dispacher.pl
    #!/usr/bin/perl -w use strict; use File::Tail; my $file = File::Tail->new( name =>'/var/log/messages', interval => 1, maxinterval => 1, # resetafter=> 5, ); while (defined(my $line=$file->read)) { print $line; }
    and , line_process.pl is:
    #!/usr/bin/perl -w use strict; use Net::Ping; my $result; while (<>){ my $p = Net::Ping->new('icmp',3); if ($_ =~ /^(.*) dns02cor dhcpd: DHCPACK on ([0-9\.]+) + to ([[:xdigit:]:]+).* ([0-9\.]+)/) { for (my $i = 0; $i < 3; $i++){ print "attempt $i for $2 \n"; $result = $p->ping($2); if ($result){ print "############## +$2 is alive at attempt $i\n"; last; } $p->close(); } unless ($result){ print "*****$2 is dead*****\n +"; } } }
    When I run them individually they work ok, but when I try to pipe them doing:
    ./dhcp_line_dispacher.pl | ./line_process.pl
    Nothing happens.
    So I test the ./dhcp_line_dispacher.pl and realized than I can`t grab the output.
    For example from cli, I do:
    ./dhcp_line_dispacher.pl | grep ACK And can not get the output in real time.
    What should I do to get the output and then process it in other script?
    regards,
    Leo.
JSON encoding issues
1 direct reply — Read more / Contribute
by sedninja
on May 05, 2016 at 11:41
    Hello wise perl monks,

    I am hoping someone here can point me in the right direction. I have a message processing system written in perl using JSON as the primary transport encoding. The main process receives STOMP messages using AnyEvent::STOMP. Those messages are encoded in json by php and received by perl and decoded. The messages are JSONRPC messages with a "method" and "params". When I decode them, I then re-encode the params and use a AnyEvent::JSONRPC::TCP::Client to send them to the AnyEvent::JSONRPC::TCP::Servers listening on unix sockets. This is a service I have been running for 2+ years, and the bit of code responsible for processing the stomp messages has remained unchanged for most of the life of the project. I am running perl-5.18 in a Docker container based on ubuntu:14.04.

    I recently flushed all of my layer caches and built the entire container from scratch, and I am now having a problem with the JSON encoding which has me frazzled. I've spent 8+ hours trying to figure out specifically what might have changed, but I'm coming up with nothing. In my stomp client process I have always used JSON->new->allow_nonref as my encoder because some of the "params" that come through are an array once decoded and some are not. Using allow_nonref made this something I could handle without having to change anything based on how the message was received. However, after building the new image I found the following exception messages in my log, and the messages are not making it to the JSONRPC::Server any longer.

    unhandled callback exception on event (MESSAGE, AnyEvent::STOMP::Client=HASH(0x7432b18), HASH(0x1a9da520) {"method":"echo","params":[{"Deployment":{"id":"57276a74-8790-48aa-b7d6-42480a6e0303","status":268435970,"comment":"marathon| Creating new application for website 1088 in marathon","website_id":1088,"district":"devel"}}]}): hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this) at /usr/local/lib/perl/5.18.2/AnyEvent/Handle.pm line 1135

    The AnyEvent::Handle module line 1135 is using the encode method of JSON::XS->new->utf8. I tried to reproduce this issue by using the same encode method on the same data via perl cli and I am unable. I modified the module code to have it dump the data it was receiving as it's $ref input, and it is as I suspected a valid hashref. I used that dumped hashref, I tried changing from allow_nonref to encode/decode it in different ways before sending it to the client, but nothing I do will reproduce the issue in my tests.

    use JSON::XS; use Data::Dumper; my $json = JSON::XS->new->utf8; my $input = '{"method":"echo","params":[{"Deployment":{"id":"57276a74- +8790-48aa-b7d6-42480a6e0303","status":268435970,"comment":"marathon| +Creating new application for website 1088 in marathon","website_id":1 +088,"district":"devel"}}]}'; print Dumper { input => $input, output => $json->decode($input), re_en +coded => $json->encode($json->decode($input)) }; $VAR1 = { 'input' => '{"method":"echo","params":[{"Deployment":{"id":" +57276a74-8790-48aa-b7d6-42480a6e0303","status":268435970,"comment":"m +arathon| Creating new application for website 1088 in marathon","webs +ite_id":1088,"district":"devel"}}]}', 're_encoded' => '{"params":[{"Deployment":{"status":26843597 +0,"district":"devel","comment":"marathon| Creating new application fo +r website 1088 in marathon","id":"57276a74-8790-48aa-b7d6-42480a6e030 +3","website_id":1088}}],"method":"echo"}', 'output' => { 'params' => [ { 'Deployment' => { 'id' => '572 +76a74-8790-48aa-b7d6-42480a6e0303', 'website_id' + => 1088, 'comment' => + 'marathon| Creating new application for website 1088 in marathon', 'district' = +> 'devel', 'status' => +268435970 } } ], 'method' => 'echo' } };
Segmentation fault with threads and binmode utf-8
3 direct replies — Read more / Contribute
by Anonymous Monk
on May 05, 2016 at 04:32

    The following program produces a segmentation fault with perl 5.10.1, 5.20.3, and 5.22.2:

    #!/usr/bin/perl use strict; use warnings; use threads; binmode STDOUT, ':encoding(utf-8)'; for my $i (1..4) { threads->create(sub {})->join(); }

    Is this a known bug?


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!
  • 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?
    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 rifling through the Monastery: (10)
    As of 2016-05-06 13:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?