Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

The Monastery Gates

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

Donations gladly accepted

If you're new here please read PerlMonks FAQ
and Create a new user.

New Questions
Catalyst versionning
1 direct reply — Read more / Contribute
by QuillMeantTen
on May 06, 2016 at 10:27

    Greetings fellow monks,
    I'm currently working on my second project using catalyst and, as before, I do all my work under git. Thing is, I haven't the foggiest about what files (beside the model, views, controller) should really be pushed. Since one should not push to a repository anything that can be generated from what is already there I'd really like to know the current best practices.

Time::HiRes gettimeofday producing timestamps out of order
2 direct replies — Read more / Contribute
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.
Conditional creation of files - global or scoped filehandle
4 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.

pipe issue (linux)
4 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.
Remove double bracket and singe quotes
5 direct replies — Read more / Contribute
by lobs
on May 02, 2016 at 10:50
    So I am trying to remove double brackets and singe quotes. Here is example text
    'C-3PO' or 'See-Threepio' is a humanoid robot character from the [[Sta +r Wars]] universe who appears in the original ''Star Wars'' films, th +e prequel trilogy and the sequel trilogy.
    What I have done is
    $doc =~ s/\[\[//g; $doc =~ s/\]\]//g; $doc =~ s/\'//g;
    Does not work at all. Please help.
What am I not understanding about $,
3 direct replies — Read more / Contribute
by Anonymous Monk
on May 01, 2016 at 01:55
    Easy way around this, but I want to understand why it is not working as I expect.
    #!/usr/bin/perl -w use strict; my @array = ( 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx', 'yz ' ); print "@array[2,4]\n";
    Outputs:
    ghi mno
    I want to get rid of the space, so I added:
    local $, = undef;
    But there is still a space in the output. Why? And is there some clever way of getting rid of the space without the obvious method of combining the two elements in a temporary variable and printing that?
Device::SerialPorDevice::SerialPort;t Problem
2 direct replies — Read more / Contribute
by kurta
on Apr 30, 2016 at 14:25

    This is a weird one. I'm reading data from a GPS module. The following code gives the expected response:

    #! /opt/local/bin/perl -w use strict; require 5.000; use lib "/opt/local/lib/perl5/"; use Data::Dump qw(dump); use Device::SerialPort; my $port = Device::SerialPort->new("/dev/tty.usbserial"); $port->are_match("\r\n"); $port->baudrate(9600); $port->databits(8); $port->parity("none"); $port->stopbits(1); while (1) { my $s = $port->lookfor(); next if $s eq ''; print $s,"\n"; if ($s=~/.*GPGGA.*/){ my @a=$s=~/(\d+\.\d{2}),(\d+\.\d+),(N|S),(\d+\.\d+),(E|W)/; print "==",dump(@a); } }
    But if I comment out the "print $s,"\n";" statement, I get nothing. Here's some sample output:

    $GPRMC,108746.88,A,2048.37808,N,80113.44007,W,8.352,,388416,,,D*61
    $GPVTG,,T,,M,8.352,N,8.652,K,D*23
    $GPGGA,108746.88,2048.37808,N,80113.44007,W,2,89,1.82,26.8,M,-29.6,M,,8888*51
    ==("108746.88", "2048.37808", "N", "80113.44007", "W")$GPGSA,A,3,16,26,22,31,23,27,51,80,89,,,,1.78,1.82,1.36*83
    $GPGSV,4,1,14,83,15,210,80,84,11,840,13,87,13,381,,80,47,173,32*7F
    $GPGSV,4,2,14,89,42,311,31,11,83,103,,16,58,824,29,22,87,193,11*7E
    $GPGSV,4,3,14,23,78,208,20,26,26,844,19,27,63,117,26,31,18,897,17*7D

    I suspect there's something strange going on with buffering, but don't understand why printing a variable should make a difference. Any perl-ly wisdom would be appreciated

    === ARGH! Just added a "/n" to the print "==",dump(@a); line and now it works. Why doesn't the buffer flush on print? Is Device::SerialPort resetting something?
New Monk Discussion
unreaping , reversing reaped, resurrecting a node
3 direct replies — Read more / Contribute
by beech
on May 03, 2016 at 02:45
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 perusing the Monastery: (9)
As of 2016-05-06 20:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?