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
"Cast" a wxString to Numeric?
2 direct replies — Read more / Contribute
by jmlynesjr
on May 02, 2015 at 09:08

    I have the following event handler dealing with a change in a Wx::TextCtrl:

    Wx::Event::EVT_TEXT($self, $IcqText, sub { my ($self, $event) = @_; $self->{AMP1}->Icq(($IcqText->GetValue) / 1000 +);});

    Where $IcqText->GetValue returns a wxString and the / 1000 throws the warning Argument "" isn't numeric in division (/) at ./CEAmp.pl line 163. The division is successfully completed.

    The following modification masks the warning:

    Wx::Event::EVT_TEXT($self, $IcqText, sub { my ($self, $event) = @_; { no warnings; $self->{AMP1}->Icq(($IcqText->GetValue) / 1000 +); } });

    Is there an alternate way to "cast" the wxString to numeric?


    There's never enough time to do it right, but always enough time to do it over...

How to debug HTTP::Server::Simple::PSGI
1 direct reply — Read more / Contribute
by Anonymous Monk
on May 02, 2015 at 03:12

    Hi monks
    I want to know what is the simplest way to debug an HTTP::Server::Simple::PSGI application
    When I used CGI instead of PSGI I debugged my application with

    use CGI::Carp 'fatalsToBrowser'

    But now it's PSGI, CGI::Carp doesn't work and when I have errors, It just prints Internal server error on browser screen and nothing to STDOUT.

Perl IO::Socket::SSL persistent TCP connection
1 direct reply — Read more / Contribute
by kamrul
on May 01, 2015 at 17:37
    I am writing a perl script for communicating with android GCM CCS. My script looks like the below:
    use IO::Socket::SSL; my $client = IO::Socket::SSL->new('gcm.googleapis.com:5236') or die "error=$!, ssl_error=$SSL_ERROR"; print $client '<stream:stream to="gcm.googleapis.com" version="1.0" #xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" +/>'; print <$client>; print $client '<auth mechanism="PLAIN" xmlns="urn:ietf:params:xml:ns:xmpp-sasl">MTI2MjAwMzQ3OTMzQHByb2plY3RzL +mdjbS5hb mFTeUIzcmNaTmtmbnFLZEZiOW1oekNCaVlwT1JEQTJKV1d0dw==</auth>'; if(!<$client>){ print "Auth error\n"; } else { print <$client>; }
    After writing the first request on the socket I get response as expected from google server. And I cant print the server response using print But, I dont get any response after writing the second request. It seems like the connection gets closed after the first request and response or something. How do I send multiple request through open TLS connection and receive server responses ?
RPN Question
7 direct replies — Read more / Contribute
by nat47
on May 01, 2015 at 16:22
    EDIT: Thank for some awesome advice and answers! I've learned a lot, and translated RPN strings into solved equations! Clarification for those that were wondering: I was asking how to take a RPN string that contained variables, and solve for that variable.
Net::SSH::Perl and getting User from config
3 direct replies — Read more / Contribute
by dayton
on May 01, 2015 at 14:58

    I'm trying to setup a script using Net::SSH::Perl to connect to several hosts and need some insight.. I'm able to connect either with password or rsa/dsa key as long as I specify the username - however on a few hosts the user name is different (and defined in my ~/.ssh/config) How can I get my script to grab the "User" value from the "Hostname" in my config file?

    foreach $host (@HOSTS) { $ssh = Net::SSH::Perl->new("$host",debug=>1,option +s => ["ConnectTimeout=5","StrictHostKeyChecking=no"]); $ssh->login($user); ($out, $err, $exit)=$ssh->cmd($exec_cmd); }

    per the debug, it's reading my config but not getting the User for a particular host:

    Host host2 hostname host2.mydomain User user1
    my_host: Reading configuration data /home/djones/.ssh/config my_host: Reading configuration data /etc/ssh_config my_host: Connecting to host2, port 22. my_host: Remote version string: SSH-2.0-OpenSSH_5.3p1 Debian-3ubuntu7. +1 my_host: Remote protocol version 2.0, remote software version OpenSSH_ +5.3p1 Debian-3ubuntu7.1 my_host: Net::SSH::Perl Version 1.36, protocol version 2.0. .y_host: No compat match: OpenSSH_5.3p1 Debian-3ubuntu7.1 my_host: Connection established. my_host: Sent key-exchange init (KEXINIT), wait response. my_host: Algorithms, c->s: 3des-cbc hmac-sha1 none my_host: Algorithms, s->c: 3des-cbc hmac-sha1 none my_host: Entering Diffie-Hellman Group 1 key exchange. my_host: Sent DH public key, waiting for reply. my_host: Received host key, type 'ssh-dss'. my_host: Host 'host2' is known and matches the host key. my_host: Computing shared secret key. my_host: Verifying server signature. my_host: Waiting for NEWKEYS message. my_host: Send NEWKEYS. my_host: Enabling encryption/MAC/compression. my_host: Sending request for user-authentication service. my_host: Service accepted: ssh-userauth. my_host: Trying empty user-authentication request. my_host: Authentication methods that can continue: publickey,password. my_host: Next method to try is publickey. my_host: Publickey: testing agent key '/home/djones/.ssh/id_dsa' my_host: Authentication methods that can continue: publickey,password. my_host: Next method to try is publickey. my_host: Next method to try is password. my_host: Trying password authentication. my_host: Will not query passphrase in batch mode. my_host: Authentication methods that can continue: publickey,password. my_host: Next method to try is publickey. my_host: Publickey: testing agent key '/home/djones/.ssh/id_dsa' my_host: Authentication methods that can continue: publickey,password. my_host: Next method to try is publickey. Received disconnect message: Too many authentication failures for djon +es at /usr/share/perl5/vendor_perl/Net/SSH/Perl/AuthMgr.pm line 143.
Separating different lexicals with B::Xref
1 direct reply — Read more / Contribute
by LanX
on May 01, 2015 at 12:49

    I thinks this question was asked before but maybe a new view might help:

    In the following code two different my $a are defined in separate scopes, but the output of B::Xref doesn't help separating them.

    I.e. the last use of $a in line 21 could belong to both.

    Any way to fix this?

    lanx@lanx-1005HA:~$ cat /tmp/tst_bxref.pl use strict; use warnings; my $a=1; print($a); sub bla { } { my $a=2; print($a); bla($a); } print($a); lanx@lanx-1005HA:~$ perl -MO=Xref,-d /tmp/tst_bxref.pl File /tmp/tst_bxref.pl Subroutine (main) Package (lexical) $a i5, i15, 8, 16, 17, 21 Package main &bla &17 /tmp/tst_bxref.pl syntax OK lanx@lanx-1005HA:~$ perl -MO=Xref,-d,-r /tmp/tst_bxref.pl /tmp/tst_bxref.pl (main) 5 (lexical) $ a + intro /tmp/tst_bxref.pl (main) 8 (lexical) $ a + used /tmp/tst_bxref.pl (main) 15 (lexical) $ a + intro /tmp/tst_bxref.pl (main) 16 (lexical) $ a + used /tmp/tst_bxref.pl (main) 17 (lexical) $ a + used /tmp/tst_bxref.pl (main) 17 main & bla + subused /tmp/tst_bxref.pl (main) 21 (lexical) $ a + used /tmp/tst_bxref.pl syntax OK

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Storable dumping core
1 direct reply — Read more / Contribute
by Random_Walk
on May 01, 2015 at 11:35

    Good Day Fair Monks,

    I am using Storable and every so often it all goes pear shaped.

    Magic number checking on storable file failed at /usr/localperl/lib/5.18.2/x86_64-linux-thread-multi/Storable.pm line 380 ...

    My code runs every five minutes, and will happily store about 5 samples, then it throws the above and dumps core. The store file is zero bytes after that event. When things are good I see these details.

    > file itm6_linux_server.memstore > itm6_linux_server.memstore: perl Storable (v0.7) data (major 2) (min +or 9) # from print Dumper Storable::file_magic 'hdrsize' => 19, 'ptrsize' => 8, 'byteorder' => '12345678', 'intsize' => 4, 'file' => 'itm6_linux_server.memstore', 'longsize' => 8, 'netorder' => 0, 'minor' => 9, 'version_nv' => '2.009', 'major' => 2, 'nvsize' => 8, 'version' => '2.9' > ls -l itm6_linux_server.memstore -rw-rw-rw- 1 root root 2514 May 1 16:25 itm6_linux_server.memstore >perl -v This is perl 5, version 18, subversion 2 (v5.18.2) built for x86_64-li +nux-thread-multi

    The amount of data is not massive, It currently has 5 records in there, and is 2026 Bytes. The Data I am storing is a hash/array structure, 5 levels at its deepest.

    I'm using lock_store and lock_retrieve to get at it, as the script runs on a schedule, as well as me running it every so often for development and debug. The script uses a file lock to guard against multiple runs anyway. I just added the lock_ versions when I hit this error. Did not help. It is only being accessed on the one machine, with the one version of Perl

    Any ideas what could be wrong. The code is long and complex. So building a small test case will be a rather long winded process. If there are any known gotchas with Storable I would love a pointer.


    All the code is massive. But here are my retrieve and store routines

    sub getMem { my $self = shift; my $confID = shift; $confID = $self->{_itm6_lib_gen}->{_CONFID} unless defined $confID +; my $mem = {}; # default to an empty one my $local_store = "local_$confID.memstore"; if (-e $local_store) { eval { $mem = lock_retrieve $local_store }; if ($@) { $self->{_logger}->trace( 1, "getMem: Reading store failed: + $@"); print "getMem: Reading store failed: $@\n"; } } else { # print "$local_store not found\n"; } push @{ $self->{_file_to_write} }, [$mem, $local_store]; # note we + need to write this return $mem; } # Saving is done in DESTROY sub DESTROY { my $self = shift; for (@{ $self->{_file_to_write} }) { my ($mem, $store) = @$_; lock_store ($mem, $store); } }


    Pereant, qui ante nos nostra dixerunt!
Efficiently create an IF/ELSE regex
3 direct replies — Read more / Contribute
by jcrush
on May 01, 2015 at 11:32

    Hello Wise Monks,
    I'm trying to find a more efficient solution, to correct interpolated data coming into my script.
    Since I cannot control the data coming in - only how its stored in the database.
    I'm storing data into a SQL database that is interpolating domain accounts into whitespace.

    Incoming data, 3 types of samples interpolated:

    "AD\thomas" -> "AD homas" # tab "MAIN\nancy" -> "MAIN ancy" # newline "nancy" -> "nancy" # no domain, no problem

    Here is what I came up with, but its a kludge, and not efficient when working with lots of database inserts.

    $domainID =~ (s/(AD|MAIN)\n/n/); # should not be $domainID =~ (s/(AD|MAIN)\r/r/); # checked 3 times $domainID =~ (s/(AD|MAIN)\t/t/); # before exiting

    - - - SOLUTION - - -

    Thanks to everyone's suggestions, I solved this domain interpolation issue:

    our %whitespace = ( "\f" => "f","\n" => "n","\r" => "r","\t" => "t" ); $netid =~ (s/(\s)/$whitespace{$1}/g); $netid =~ (s/(AD|MAIN)//);


    "AD\thomas" -> "thomas" "MAIN\floyd" -> "floyd" "AD\manny" -> "manny" # perl escapes normal char, no harm

    Thank you for all the help,

IP address validation when IP is picked dynamically
3 direct replies — Read more / Contribute
by rahulruns
on May 01, 2015 at 08:24

    I am picking up IP address from a ifcfg file (RHEL) and trying to validate IP. But I am not able to validate it. When I am using IP address in quotes (e.g. ipaddress = "x.x.x.x" to check) it works but when I am trying to take it dynamically it does not

    #!/usr/bin/perl use strict; use warnings; use Data::Validate::IP qw(is_ipv4); print "Enter the absolute directory location for ifcfg files\n"; my $directory = <>; chomp($directory); opendir(DIR, $directory) or die "couldn't open $directory: $!\n"; my @ifcfg_files = grep { /^ifcfg-bond/ } readdir(DIR); closedir DIR; foreach (@ifcfg_files) { my %hash = (); my $file = "$_"; open (my $fh, "<", $file) or die "Can't open the file $file: "; while (my $line =<$fh>) { next if $. < 2; my($key, $parameter_value) = split("=", $line); next if $key eq "BONDING_OPTS"; if (is_ipv4($parameter_value)) { print $parameter_value; } else{ print "Fail" } } } #Outer Foreach Contents of ifcfg file # Creating bond1 DEVICE=bond1 IPADDR= NETMASK= DNS1= MTU= BOOTPROTO=none ONBOOT=yes USERCTL=NO NM_CONTROLLED=no BONDING_OPTS='mode=1 miimon=100' Result [root@localhost bond]# perl ifcfgbondverification.pl Enter the absolute directory location for ifcfg files /root/office/ifcfgverification/bond FailFailFailFailFailFailFailFailFailFailFailFailFailFailFailFailFailFa +ilFailFailFailFailFailFailFailFailFailFailFailFailFailFailFailFailFai +lFailFailFailFailFailFailFailFailFailFailFailFailFailFailFailFailFail +FailFailFailFailFailFailFailFailFailFailFail[root@localhost bond]#
Move all files and folders older than 5 minutes
1 direct reply — Read more / Contribute
by omegaweaponZ
on May 01, 2015 at 07:18
    Hey there, I'm looking to move all files and folders from one directory to another, but I don't just want a full instant directory move. I'm looking to individually scan each file and folder from a parent directory and, keeping the sub-folder structure intact, only move them if they are older than 5 minutes of being modified to a new directory. So something like this:
    $dir = '/current/directory/'; $newdir = '/new/directory/'; find(\&movefiles, $dir); sub movefiles { $move_file = $File::Find::name; $move_curDir = $File::Find::dir; $move_curFile = $_; $move_basedir = dirname($move_curDir); $file_time = (stat($move_file))[9]; $current_time = time; $time_dif = $current_time - $file_time; if ($time_dif <= 300) { #Ignore } else { #Move my $new_file = $move_file; $new_file =~ s/old_folder/new_folder/; copy("$move_file","$new_file") or die "Copy failed: $!";
    After this, I'm looking to actually move the files and sub-folders of those files from the base current directory to the new current directory. Any thoughts? Thank you!

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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.