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

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
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?
DBD::mysql::st execute failed: You have an error in your SQL syntax;
3 direct replies — Read more / Contribute
by Anonymous Monk
on Jun 22, 2016 at 07:55

    the below code is giving following error: DBD::mysql::st execute failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 at /var/www/cgi-bin/tt.cgi line 35.

    #!/usr/bin/perl use warnings FATAL => 'all'; use CGI; use CGI::Carp qw(fatalsToBrowser); use DBI; use DBD::mysql; my $cgi = CGI->new; print $cgi->header, $cgi->start_html; my $title = $cgi->param('TITLE'); my $feature = $cgi->param('FEATURE'); my $type = $cgi->param('TYPE'); my $description = $cgi->param('DESCRIPTION'); my $status = $cgi->param('STATUS'); my $path = $cgi->param('PATH'); my $priority = $cgi->param('priority'); my $database = "test_case"; my $host = "localhost"; my $user = "chiragk"; my $pw = ""; my $dsn = "dbi:mysql:$database:localhost"; my $dbh = DBI->connect($dsn, $user, $pw, { RaiseError => 1 }) or die "una +ble to connect:$DBI::errstr\n"; my $query = "insert into test(title,feature_name,type,description, +status,test_case_path,priority) values(?,?,?,?,?,?,?,)"; my $sth = $dbh->prepare($query); $sth->execute("$title","$feature","$type","$description","$status" +,"$path","$priority"); print $cgi->p("[$title],[$feature],[$type],[$description],[$status +],[$path],[$priority]"); print $cgi->end_html;
Help with compiling glib
2 direct replies — Read more / Contribute
by frazap
on Jun 22, 2016 at 06:28

    I have copied the *.dll.a files on *.a files in my c:/prog/gtk+/lib folder and in the sub folders.

    I run
    perl INC="-IC:\prog\gtk+\include\glib-2.0 -IC:\prog\Gtk+\l +ib\glib-2.0\include" LIBS="-LC:/prog/gtk+/lib"
    dmake fails with
    "C:\strawberry\perl\bin\perl.exe" -MExtUtils::Command::MM -e cp_nonemp +ty -- blib\arch\auto\Glib\ 644 [ LD blib\arch\auto\Glib\Glib.xs.dll ] Glib.o:Glib.c:(.text+0x85): undefined reference to `g_filename_display +_basename' Glib.o:Glib.c:(.text+0xb6): undefined reference to `g_free' Glib.o:Glib.c:(.text+0x195): undefined reference to `g_filename_displa +y_name' ...

    Thanks for any suggestion !

Bug in latest File::Util?
2 direct replies — Read more / Contribute
by martell
on Jun 21, 2016 at 06:07

    Dear Monks,

    I think I found a bug in the File::Util (File-Util-4.161200) package. $f->list_dir() doesn't work when no files are present in the directory. Can someone have a look if they see this too and this isn't an error on my part?


    Step 1: Locate on your system a directory that contains no files, but can contain subdirectories. Example "/home/my_user/foo", containing "/home/my_user/foo/bar" and /home/my_user/foo/bar2".

    Step 2: Try to list the directories with:

    perl -e 'use strict; use File::Util;my $f = File::Util->new(); my @d = $f->list_dir("/home/my_user/foo/"); print join " ", @d, "\n";'

    This results in following error:

    Can't use an undefined value as an ARRAY reference at /usr/local/share/perl/5.14.2/File/ line 452.

    Step 3: Create an file in "/home/my_user/foo/". Example "test.txt"

    Step 4: Run the same perl snippet again. This results in the correct expected result:

    . .. bar bar2 test.txt

    Module: File-Util-4.161200
    Perl version:v5.14.2
    System: Linux hostname 3.4.90 #2 SMP PREEMPT Tue Aug 5 14:11:40 CST 2014 armv7l GNU/Linux

    I would appreciate that someone can confirm this before I report this as a bug to the maintainer of the package.

    I tried the same code on a different system with File-Util-4.132140 and there it worked as expected

    Kind regards


    Update: All, thanks for the quick reactions and the verifying. I posted this as a bug report on cpan:

Issue in SFTP connection using Net::SFTP::Foreign-
1 direct reply — Read more / Contribute
by iit2011101
on Jun 21, 2016 at 05:55

    Hi I am using SFTP to connect to a server using private-public key authentication . However I am not able to connect. I am able to connect using bash command

    here is the code
    Net::SFTP::Foreign->new($ftp_server, port => $ftp_port , user => $ftp_ +username, password => $ftp_password , key_path => $private_key , more + => [ '-v', -o => 'PreferredAuthentications publickey']); here is verbose log debug1: Reading configuration data /home/fds/apfh/.ssh/config debug1: Reading configuration data /etc/ssh/ssh_config debug1: Applying options for * debug1: Connecting to [] port 6010. debug1: Connection established. debug1: identity file /home/fds/apfh/FDSnse_fh/etc/id_rsa type 1 debug1: identity file /home/fds/apfh/FDSnse_fh/etc/id_rsa-cert type -1 debug1: Remote protocol version 2.0, remote software version OpenSSH_5 +.3 debug1: match: OpenSSH_5.3 pat OpenSSH* debug1: Enabling compatibility mode for protocol 2.0 debug1: Local version string SSH-2.0-OpenSSH_5.3 debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: server->client aes128-ctr hmac-md5 none debug1: kex: client->server aes128-ctr hmac-md5 none debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP debug1: SSH2_MSG_KEX_DH_GEX_INIT sent debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY debug1: Host '[]:6010' is known and matches the RSA host k +ey. debug1: Found key in /home/fds/apfh/.ssh/known_hosts:1 debug1: ssh_rsa_verify: signature correct debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug1: SSH2_MSG_NEWKEYS received debug1: SSH2_MSG_SERVICE_REQUEST sent debug1: SSH2_MSG_SERVICE_ACCEPT received "NOTICE TO USERS" "The system is to be used for AUTHORIZED business purpose +only. All activities on this system are being monitored. Unauthorize +d access to this system may be subject to legal action,and/or pros +ecution" debug1: Authentications that can continue: publickey debug1: Next authentication method: publickey debug1: Offering public key: /home/fds/apfh/FDSnse_fh/etc/id_rsa debug1: Server accepts key: pkalg ssh-rsa blen 277 debug1: read PEM private key done: type RSA debug1: Authentication succeeded (publickey). debug1: channel 0: new [client-session] debug1: Requesting debug1: Entering interactive session. debug1: Sending environment. debug1: Sending subsystem: sftp debug1: channel 0: free: client-session, nchannels 1 debug1: fd 0 clearing O_NONBLOCK debug1: fd 1 clearing O_NONBLOCK debug1: fd 2 clearing O_NONBLOCK Connection to closed by remote host. Transferred: sent 2392, received 2488 bytes, in 30.4 seconds Bytes per second: sent 78.7, received 81.8 debug1: Exit status -1 ERROR[06/21 13:41:52] Cannot connect to [SF +TP] server, error message: SSH slave exited unexpectedly with error c +ode 255 WARN [06/21 13:41:52] [SFTP] conne +ction attempt to server[] failed, will retry in 60 second +s!
    Any help will be appreciated .
Postgres + Perl + Ubuntu
6 direct replies — Read more / Contribute
by SimonClinch
on Jun 21, 2016 at 03:26
    Hi, I have a puzzling issue when trying to migrate a database. The database was previously set up under Postgres 9.5.3 on Windows 10 using activeperl latest version with DBD:Pg.

    The target environment for the migration is Ubuntu 14.04. The latest version of Postgres for this appears to be 9.3. The Perl version it has is 5.18.2.

    After apt-get install of Postgres, defaulting to 9.3, PgAdminIII, cpan installation of DBI, DateTime and DBD:Pg, I created the databases with the same names, roles and passwords as the Windows installation, using pgsql. I can log on from pgsql, but neither the postgres user or the roles can log in from DBI, behaving as if the username and password are wrong, but I am sure they are not. I get the same kind of problem from PgAdminIII, so it isn't just Perl but PgAdminIII suffering the same problem. I checked the Postgres configuration files and there is nothing wrong there. It isn't a network problem because all access methods above were local. I checked for having more than one installed version of Postgres, which would explain it, but no, there is only one. So the problem appears to lie under the hood somewhere, not obviously in DBI otherwise pgadmin would work, but I am not sure where to look next. All suggestions are welcome! Thanks!

    One world, one people

curious regex result for perl 5.8.8
3 direct replies — Read more / Contribute
by erodrig
on Jun 20, 2016 at 17:56

    Consider the following program which just does a regex on two array entries:

    @files=("zzz.21.yy.ccc", "zzz.220.ccc" ); foreach $name (@files) { chomp $name; $match="no "; $match="yes" if ( $name =~ /(^[a-z]{3})\.(\d{2,3})\..*\.ccc/) ; print "$match, $name, match1: $1, match2: $2\n"; }

    When I run this on Linux perl 5.8.8 $2 does not seem correct for the second name:

    The second name should have the same values since the regex match fails the second time, but $2 mysteriously takes "21." from the first entry. Can this be a problem with perl 5.8.8?

    See below:

    perl yes, zzz.21.yy.ccc, match1: zzz, match2: 21 no , zzz.220.ccc, match1: zzz, match2: 21.

    With perl 5.10.1 this seems to run as I would expect it to:

    perl yes, zzz.21.yy.ccc, match1: zzz, match2: 21 no , zzz.220.ccc, match1: zzz, match2: 21

    Thanks for any comments.

Constant SQL Querys to send "signals"
4 direct replies — Read more / Contribute
by stewe
on Jun 20, 2016 at 16:13
    Dear Monks,

    i come in shame as i have to admit that i use perl combined with mysql in a way that cant be right. At least it feels so wrong.
    I've got an Application which is written in Perl embedded into Apache. I've got some fancy scripts which i call agents. Now : When there are certain things happening in Apache like i get an request from the outside world and i have the urge to tell it my mighty agent what i do is insert a string of JSON into a table which i call "do".

    My Agent does something like this :
    while(1){ foreach my $p (@{$dbh->selectall_arrayref(qq|SELECT id, do, ts FROM + do|, {Slice=>{} })}){ # Do what the JSON is telling me } sleep 1; }
    By now u know what i mean with it feels wrong in any possible way. So i started digging : Signals, Mysql Triggers, POE, Anyevent, IO:: and so forth. What would you do or maybe have done to implement something like that. Dont worry all i need is some direction which would be best and i will read from there but im overwhelmed by posibilitys without havein any Idea what would really work in production later which actually works ... not like Threads when i looked at them 7 Years ago ^^

    Any guidence would be appreciated
Request for Comments: More annotations for Perl plugin for IntelliJ
1 direct reply — Read more / Contribute
by hurricup
on Jun 20, 2016 at 15:05

    This is a real request for comments and suggestions. If you are using Perl plugin for IntelliJ, you could see that it uses annotations to help working with project and resolve methods, like #@deprecated, #@returns and #@method

    Currently I'd like to add two more annotations and thinking about the proper format.

    Variable Types

    Currently, variable type may be guessed by the plugin by explicit type definition, like my Foo::Bar $var; or by return value, like my $var = Foo::Bar->new();

    Perl explicit type has a flaw: you can't specify different types on multiple declarations, only one for all of them, like my Foo::Bar ($var1, $var2, $var3);, what is not really convenient for arguments unpacking, so you need to change compact format to larger and slower multiple shifts.

    So the idea here is to introduce #@type annotation and unpacking will look like:

    my ( $self, #@type Foo::Bar $foobar, #@type Foo::Baz $foobaz ) = @_;

    For arrayrefs and hashrefs i'd like to use Moose format from isa parameter of has statement: HashRef[Foo::Bar] and ArrayRef[Foo::Bar].

    Subs arguments

    To improve subs signatures on auto-completion and auto-completion itself, I'd like to add #@args annotation, which may provide additional information about sub argument, like type, optionality and named arguments. Example:

    #@returns Some::Class #@arg $arg1 Foo::Bar # some description #@arg $arg2 Foo::Baz #@arg $arg3 # just a description #@arg %args key1, key2 Foo::Zoo, key3 sub somesub{ my ($self, $arg1, $arg2, $arg3, %args) = @_; }

    This idea is pretty new, so I don't really know what is the best way to do this. Typing format is pretty obvious but other features - not.

    Optional vs mandatory:

    #@arg [$var1 HashRef[Foo::Bar]] # description or #@arg optional $var1 HashRef[Foo::Bar] # description or #@oarg $var1 HashRef[Foo::Bar] # description

    Named parameters looks pretty solid, aside it's not possible to add a description, but not sure how to use it yet anyway.

    I'm also going to make possible to move all subs annotations to a separate file, like Python stub file, it's necessary to work with external OOP modules, which are not annotated and won't ever be.

    Please, share your ideas and suggestions.

Add your question
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?

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

      Results (326 votes). Check out past polls.