Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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
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.

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
    #!/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 , 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:
    ./ | ./
    Nothing happens.
    So I test the ./ and realized than I can`t grab the output.
    For example from cli, I do:
    ./ | 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?
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";
    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:

    ==("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

    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?
DBIx::Class Looping thru resultset
3 direct replies — Read more / Contribute
by phildeman
on Apr 29, 2016 at 16:04

    Hi All,

    Can variables be used in place of column names when looping through DBIx::Class array of objects.
    For example:

    my @db_colnames = ('lastname','firstname','middlename'); my @rows = $schema->resultset( 'myTable' )->search({}, {order_by => [qw +/ lastname firstname middlename /] } ); foreach my $row (@rows) { foreach my $col (@db_colnames) { print $row->$col ."\t"; } print "\n"; }

    No value printed. However, when I used the actual column name to retrieve the data, it worked fine. For example:

    . . print $row->lastname . "\t" . $row->firstname . "\t" . $row->middlenam +e . "\t"; print "\n"; . .

    Thanks for your help.

Parse multiline logfile
2 direct replies — Read more / Contribute
by chris2013
on Apr 29, 2016 at 15:04
    Dear All,

    I'd like to parse a Charon (StrongSwan) logfile.

    Somewhere it logs a failed login. If this is found, I'd like to lookup the username and IP address, that are mentioned some lines above.

    What's the best approach? Is there any framework that can do that? Where I only have to fill-in the regex?

    Should I read the whole logfile to an array and then go back? Or is there something like grep? I've tried to use a shell script and let perl one-liners do the regex stuff. Unfortunately I couldn't escape the single quotes in front and after the e-mail address. They have to be quoted because of the shell and the regex. Tried to write them to an evironment variable but that didn't work. Probably, there are completely different and better solutions.

    export MYP="identity '([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,})'" EMAIL=`grep -B15 "$DATE" $LOGFILE | grep "$CONID" | grep identity | pe +rl -nle 'print "$1" if /$ENV{MYP}/'`


    Apr 29 10:01:10 charon: 07[NET] <con1|33> received pac +ket: from[55842] to[4500] (80 bytes) Apr 29 10:01:10 charon: 14[IKE] <con1|33> received EAP + identity '' Apr 29 10:01:11 charon: 14[IKE] <con1|33> EAP-MS-CHAPv +2 verification failed, retry (1)

    - Chris

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?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (10)
As of 2016-05-06 18:55 GMT
Find Nodes?
    Voting Booth?