Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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
Handshake mechanism in perl
2 direct replies — Read more / Contribute
by t-rex
on Jun 24, 2016 at 01:51

    i am working for a project where i have implemented concurrent server , now i need to take back logs 9 in a hand shake mechanism without except module) the flow of my code is : { #try connecting to the server socket System(run or die “couldn’t run perl” If (fails) { Transfer files on server machine via scp Run Socket established Try connecting again to the server socket from client, (this time it w +ill connect) }
    My doubt now is how do I know whether everything is running , this code is a part of one big automation so I can’t use std libraries, maybe a log file , I know how to open and write to a file but a robust handshake mechanism is what I desire , anyone has any ideas for the same.

using File::Find to find recently-installed modules
2 direct replies — Read more / Contribute
by Datz_cozee75
on Jun 24, 2016 at 01:11

    Hello Monks,

    I'm working through the exercises in the alpaca book, to wit where the castaways have specified a local::lib and now seek to write a script that gives useful time data associated with these files. My needs are not exactly what the book calls for in any one exercise but range over several, so I used File::Find for the first time instead of relying on windows explorer to tell me where and what things are. (Uggh.)

    In the last couple weeks, I've torn out and replaced an overgrown perl install with this machine, so I wanted to pay some attention to make this install a bit more robust, and consequently have strawberry perl. I had a false start on local::lib, which I hope won't matter in the scheme of things, which I only state so that no one could say "why didn't you tell us you had a screwy, abortive, previous attempt at creating a local::lib?" It may matter, may not. That's part of what I'm learning. Here's what I have now:

    use strict; use warnings; use utf8; use 5.014; use File::Find; my @directories_to_search = ('.', 'C:\Users\Fred\Desktop'); my @files; find( \&find_module, @directories_to_search ); sub find_module { if ( $_ =~ m/.pm$/ ) { my $name = $File::Find::name; push( @files, $name ); } } for my $file (@files) { my $mtime = (stat $file)[9]; # mtime via slice my $when = localtime $mtime; print "$when: $file\n"; }


    ... Thu Apr 30 10:24:40 2015: ./alpaca/template_stuff/ Sun Jun 5 14:32:45 2016: ./alpaca/template_stuff/ Sun Jun 5 14:08:18 2016: ./alpaca/template_stuff/ Thu Apr 30 10:24:40 2015: ./alpaca/template_stuff/ Wed Jun 9 08:59:19 2010: ./perl5/lib/perl5/HTML/ Wed Jun 9 08:24:01 2010: ./perl5/lib/perl5/HTML/ Wed Jun 9 13:56:58 2010: ./perl5/lib/perl5/HTML/ Wed Jun 9 16:17:31 2010: ./perl5/lib/perl5/HTML/ Thu Feb 15 07:35:15 2007: ./perl5/lib/perl5/HTML/ Thu May 21 09:22:21 2015: ./perl5/lib/perl5/HTML/ Mon Nov 1 07:04:16 2010: ./perl5/lib/perl5/Prompt/ Wed Jun 24 04:07:26 2009: ./perl5/lib/perl5/WWW/Mechanize/

    It appears that cpan modules are getting stored in a place that seems to have 'perl5' in it twice. Hope that's okay. Scripts are able to find these so far. Otherwise, I see that, unlike the professor, mtime is not what I'm looking for, as the time I'm interested in is when they got saved to this locale. Is there a perl way to make sure I own these? (The dot is 'Documents', so I would think so indeed.)

    My quest for a "born-on" timestamp continued with reading up on stat, so I worked up versions of the same material using atime and ctime. I don't think I see the difference between the two, so I wonder which one works better as a "born-on" date in this context:

    use strict; use warnings; use utf8; use 5.014; use File::Find; my @directories_to_search = ('.', 'C:\Users\Fred\Desktop'); my @files; find( \&find_module, @directories_to_search ); my @time =localtime; say "local time is @time"; sub find_module { if ( $_ =~ m/.pm$/ ) { my $name = $File::Find::name; push( @files, $name ); } } for my $file (@files) { my $atime = (stat $file)[8]; # atime via slice my $when = localtime $atime; print "$when: $file\n"; }

    atime output:

    local time is 13 44 19 23 5 116 4 174 1 Mon Jun 13 19:12:19 2016: ./alpaca/template_stuff/ Mon Jun 13 19:12:19 2016: ./alpaca/template_stuff/ Mon Jun 13 19:12:19 2016: ./alpaca/template_stuff/ Mon Jun 13 19:12:19 2016: ./alpaca/template_stuff/ ... Wed Jun 22 22:09:13 2016: ./perl5/lib/perl5/HTML/ Wed Jun 22 22:09:13 2016: ./perl5/lib/perl5/HTML/ Wed Jun 22 22:09:13 2016: ./perl5/lib/perl5/HTML/ Wed Jun 22 22:09:13 2016: ./perl5/lib/perl5/HTML/ Wed Jun 22 22:07:17 2016: ./perl5/lib/perl5/HTML/ Wed Jun 22 22:08:24 2016: ./perl5/lib/perl5/HTML/ Wed Jun 22 22:12:21 2016: ./perl5/lib/perl5/Prompt/ Wed Jun 22 22:06:40 2016: ./perl5/lib/perl5/WWW/Mechanize/

    This has the useful information I need, and seems the same on inspection as the same script with the change of

     my $ctime = (stat $file)[10];  

    My intent is to make comparisons of time values part of the search criteria. I found many of these values astonishing and unexpected compared to the reading. Again my final question for moving forward is whether atime or ctime would serve as a better born-on date. Thank you for your comment.

Mocking a method defined in a Moo Role
2 direct replies — Read more / Contribute
by PopeFelix
on Jun 23, 2016 at 13:44

    I have a method, let's call it "foo" defined in a Moo Role.

    package 'MyRole'; use Moo::Role; sub foo { return 'blah'; }

    In a consuming class, I have some behavior implemented in an "around" modifier:

    package MyClass; use Moo; with 'MyRole'; around foo => sub { my ($self, $orig) = @_; if ($self->$orig eq 'baz') { return 'bak'; } return $self->$orig; }

    I would like to test this behavior, but I can't figure out how to mock MyRole::call. If I mock MyClass::call, the around() modifier isn't called. How do I test this around() behavior?

    Here's what I'm trying:

    use MyClass; use Test::Most; use Test::MockModule; my $mock = Test::MockModule->new('MyRole'); $mock->mock('foo' => sub { return 'baz' }); my $obj = MyClass->new; # Does not work is $obj->foo, 'bak', 'Foo is what it oughtta be';
Why do I (sometimes) get a REF ref and not a SCALAR ref?
4 direct replies — Read more / Contribute
by Anonymous Monk
on Jun 23, 2016 at 00:22

    Can anyone give an explanation of the following Perl behavior?

    $ perl -le 'my $a = "foo"; my $b = \$a; print $b'
    $ perl -le 'my $a = "foo"; $a = \$a; print $a'
    $ perl -le 'my $a = "foo"; my $b = $a; $a = \$b; print $a'
    $ perl -le 'my $a = "foo"; my $b = \$a; $a = $b; print $a'

    What I would expect is for all the above to produce a SCALAR. I'm tempted to call this a bug, but I have gotten this behavior with 5.25.2, 5.24.0, 5.22.2, 5.20.2, and 5.8.9, so you would think if it was a bug someone would have noticed before now. So what gives?

naming anonymous subroutines inner variables
6 direct replies — Read more / Contribute
by writch
on Jun 22, 2016 at 16:04
    I might be being a bit more clever than I should be, but it seemed such a good idea at the time.

    I have some heavily query driven code. I intended to use a hash of SQL queries to build out a number of prepared queries. That part is working fine.

    Then, I realized, rather than write functions to return the result sets, I maybe could, in the same loop which is doing the preparation of the queries, make an anonymous subroutine and assign it, rather than writing it by hand.

    The problem is in the sixth line. When I do my 'test' query, $self->{test} is the prepared query, and $self->{ftest}() is the code that would execute it.

    But the $_ that turned out fine in the earlier uses is useless in the code itself.

    The error is 'Can't call method "execute" on an undefined value' because (of course) the line is actaully $self->{}->execute(@_) instead of $self->{test}->execute(@_), as $_ isn't populated when it's being executed, just when it's being created.

    for (keys %{$self->{SQL}}) { $self->{$_} = $self->{DBO}->prepare($self->{SQL}->{$_}); $self->{"f$_"} = sub { my $self = shift; my @res; $self->{$_}->execute(@_); while (my $dbrow = $self->{$_}->fetchrow_h +ashref()) { push @res, \$dbrow; } return \@res; }; }
SSH daemon in Perl?
3 direct replies — Read more / Contribute
by robs87
on Jun 22, 2016 at 10:52


    I'm attempting to write an SSH daemon that listens on port 22 and connect to it with a standard SSH client. In the past, I've written some telnet daemons in Perl using basic sockets. SSH, however, needs encryption and is a more advanced protocol.

    I came across Net::SSH::Perl::Subsystem::Server on CPAN. Does anyone have experience with this module or a similar module?

    I also came across this blog post where someone wrote an SSH chat server in Go (and if it can be done in Go, it can undoubtedly be done in Perl):


check if conx to Net::RabbitMQ is still alive
1 direct reply — Read more / Contribute
by perl_help26
on Jun 22, 2016 at 10:04
    Hello, I have a rabbitmq server running on an external server node. My problem is that if I don't communicate with the rabbitmq server for a certain amount of time, the server will kill the connexion which will kill my perl script. Is there a method to know if the timeout has been reached (when requesting something from the rabbitmq) or a flag like 'is_connected'? That way I could reconnect everytime this happens.

    Also, if i set a param value for heartbeat for example heartbeat=>60, will this ping the server every 30 seconds and will my problem be solved? Thanks
    my $res = $rabbit_mq->publish(1, "my_exchange.lookup", $bin, {exchang +e=>'my_exchange'}, {reply_to => $rabbit_mq->{replyQ},}); #####in here if timeout has been reached reconnect #####or if the cnx is dead reconnect $res = $rabbit_mq->recv();
Multiline regex
4 direct replies — Read more / Contribute
by adrya407
on Jun 22, 2016 at 08:17
    I am trying to create a regex to extract the variable "my_variable" from a text format like this stored in $data:
    unwanted_line1=blabla unwanted_line2=blabla my_variable=important_content_section1 important_content_section2 important_content_section3 unwanted_line3=blabla
    unwanted_line1=blabla unwanted_line2=blabla my_variable=important_content_section1 important_content_section2 important_content_section3 [stepxyz#xxxx]
    I tryed the following regex:
    my ($getVariable) = $data =~ /(my_variable=.*\n(.+[^=]\n?)*)/;
    important_content_section1 is never empty, that's why i used my_variable=.*\n but it does get what i want only if i got this:
    unwanted_line1=blabla unwanted_line2=blabla my_variable=important_content_section1 important_content_section2 important_content_section3 unwanted_line3=
    What i want to get in $getVarible is:
    my_variable=important_content_section1 important_content_section2 important_content_section3
    Update: Based on anonymus monk response, i got the solution for first data format. This is the solution: my ($getVariable) = $data =~ /(my_variable=.*\n(?:[^=\n]*\n)*)/; How can i update the regex to stop also at the lines that match [step?
Peek a hash without breaking iterator
9 direct replies — Read more / Contribute
by hurricup
on Jun 19, 2016 at 02:33

    Got a problem with peeking hash elements without breaking iterator. According to perldocs, using keys or values on %hash variable resets the internal hash iterator, used by keys/values/each. But seems not only. Simple copying of hash via %other_hash = %hash resets iterator for %hash too.

    For example:

    my %hash = ('Hello' => 'World'); while ( my ( $key, $value ) = each %hash ) { my %otherhash = %hash; print "\n$key=$value"; }

    So question: how can I peek hash keys and values without breaking it?

    In case of "why, oh why do you need this?", here is the case: my debugger for IntelliJ IDEA sends current lexical variables to the IDE on each step in/over/etc. And in case of hashes, sends number of elements and elements (by request). But when I'm trying to do so - iterator resets and loop becames infinite. Haven't tried Storable yet, but it seems too expensive anyway.

Strawberryperl (Portable) impossible install module
4 direct replies — Read more / Contribute
by Anonymous Monk
on Jun 18, 2016 at 06:48

    Hi monks

    I am trying to install modules using cpan and cpanm on Straberryperl without success. I tried both the 32 as well as the 64 versions, both 5.24 and 5.22. The reason why I try to use the portable version is that I already have a ActiveState installation for productive tasks and I do not want to mess up with multiple installations on my PC (being the nice Perlbrew not available for Windows) and I do not loke complicated solutions to have parallel Perl install (at least I have not found any easy one). I tried with "force" too. The error I get is:

    Configuring Tk-804.033 ... OK Building and testing Tk-804.033 ... FAIL ! Installing Tk failed. Retry ith --force to force install it

    Any idea?

New Meditations
Azure remote desktop / Issues acessing network drives
1 direct reply — Read more / Contribute
by talexb
on Jun 23, 2016 at 10:10

    My work currently involves doing ETL (data-munging) using Perl. Because some of the data may have private information (PII), I'm doing my work on a remote desktop, which at this organization is Azure (Windows). It's not really my choice of platform -- but at least I'm able to use the excellent git-bash package to get (more or less) back to a Linux type command line.

    The issue I'm having is that transferring files from my volume to the volume where my output is carried on to the next step is unreliable, and I'm wondering if anyone else has come across the same challenge. I also want to check the existing files, as I append a version number to the end of my files so that new files don't overwrite existing files.

    • Copying files using File Explorer works, but sometimes a transfer will stall at 99%, at which point I may lose my connectivity to the remote desktop. That isn't a bad thing, although it irritatingly loses the network path in the destination folder, and I have to drill down through seven layers of directories to get back to where I was to check that my copy succeeded before disconnecting.
    • Copying via the command line (you knew that was coming next), while logical, also appears to be hilariously slow. A file that might get copied in 15-20 seconds using Explorer takes 15-20 minutes from the command line. I have no idea why, as it should be going from one network drive to another.

    The solution I've finally hit on (this is my work in progress) is to use chdir to go into the directory in question, and then glob to get a list of the files. It still takes ten seconds to get a list of files, but I guess that's the best I can do right now.

    Thoughts, comments, ideas are welcome. Thanks!

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

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 studying the Monastery: (4)
As of 2016-06-25 03:55 GMT
Find Nodes?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...

    Results (323 votes). Check out past polls.