Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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
question about variables from new perl user
5 direct replies — Read more / Contribute
by rst
on Nov 30, 2015 at 17:33
    Hello, I'm new to perl, but not programing. I'm playing with fetching data from web pages. I have a simple program:
    use warnings; use LWP::Simple; my $url = ''; my ($t1, $t2, $t3, $t4, $t5)=head($url) or die 'Unable to get page +'; my $temp = head($url) or die 'Unable to get page'; print Dumper($temp,$t1, $t2, $t3, $t4, $t5); print "done\n"; exit 0;
    Note, this question is about the particular code snippet, but even more it is about understanding variables in Perl. I used $t1 - $t5 because the documentation said that head() returns a list. $temp contains a lot of information, $t1, - $t5 contain only a very little. How would I get something like the return code (_rc) or the etag header, or the user agent? My first instinct (which seems to be wrong) would be to treat this like an associative array and try
    $temp['headers']['etag'] and $temp['_rc'] and $temp['_request']['_headers']['user-agent']
    I would like to understand this. Any help would be appreciated.
Dynamic interpolation in a string with a scalar variable?
2 direct replies — Read more / Contribute
by nysus
on Nov 30, 2015 at 15:54
    I was trying to figure out a way if it was possible to modify a string by changing the value of an interpolated scalar variable inside the string after the string is declared. The following bit of code doesn't work but illustrates what I'm trying to achieve:
    my $variable = 3; my $string = "Variable: ${\$variable}\n"; print $string; #results in "Variable: 3" $variable = 4; print $string; #still results in "Variable: 3" but I'd like it to result in "Variable +: 4"

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon";
    $nysus = $PM . $MCF;
    Click here if you love Perl Monks

Merging Data into one array
4 direct replies — Read more / Contribute
by Anonymous Monk
on Nov 30, 2015 at 15:10
    Hi there Monks!

    I am trying to merge these two arrays only if the acc matches, can any one tell me what could be the best way to do this?
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $data1 = [ { 'NAME' => '1PAUL DY', 'DATE' => '12009-05-05', 'NUMBER' => '100001', 'ACC' => '1A1A', }, { 'NAME' => '2PAUL DY', 'DATE' => '2011-01-05', 'NUMBER' => '200331', 'ACC' => '2A3B', }, { 'NAME' => '4PAUL DY', 'DATE' => '42011-01-05', 'NUMBER' => '4200331', 'ACC' => '6A4B', }, ]; my $data2 = [ { 'EXT1' => '1b', 'EXT2' => '12b', 'EXT3' => '13b', 'EXT4' => '14d', 'ACC' => '1A1A', }, { 'EXT1' => '2b', 'EXT2' => '2b', 'EXT3' => '2b', 'EXT4' => '2d', 'ACC' => '2A3B', }, { 'EXT1X' => '4b', 'EXT2X' => '4b', 'EXT3X' => '4b', 'EXT4X' => '4d', 'ACC' => '4A4B', }, ]; # Merge all the data my @all_data; foreach my $one_data ( @{ $data1 } ) { my $sec_data = shift @{ $data2}; push @all_data, { %{ $one_data }, %{ $sec_data } }; } print Dumper \@all_data;

    Thanks for the help!
Bioinformatics program writing question
2 direct replies — Read more / Contribute
by Theworldarriving
on Nov 30, 2015 at 14:52
    This is the approach I would like to take. I have 5 mitochondrial dna sequences. I would like to run a loop (I'm not sure how to do this) to see which one is most similar to the mtdna of It will print this result. Tympanuchus cupido cupido is the bird I'm trying to compare the other mtdna to. Then, I would like to swap the mtdna of the Heath Hen to the closest relative. #!/usr/bin/perl use strict; use warnings; # we are going to run a loop the mtdn subspecies of the prairie hen to Tympanuchus cupido cupido to see which one is most similar percentwise. Then we will select the closest relative and the nucelotides with heath hen mtdna. # What do I do next?
perl to run awk and sed
4 direct replies — Read more / Contribute
by deelinux
on Nov 30, 2015 at 12:17

    Hello, I want to use awk and sed in a perl script as Im not sure how to do it in a perl script as yet

    I want to run this command, awk 'BEGIN {FS="\t"}; {print $1}' | sed 's/.$//' but Im getting the below error

    Use of uninitialized value $1 in concatenation (.) or string at ./ line 50, <stdin> line 1. sed: -e expression #1, char 3: unterminated `s' command

    Any pointers or how I can run this in a perl script, or how to do this via perl code, Im still new to perl and programming

Convert string into plain text
3 direct replies — Read more / Contribute
by ultranerds
on Nov 30, 2015 at 11:33

    This is probably super simple for most of you - but I can't work it out =)

    I have the following string:

    Segments[4][0] = '\u0069\u006d\u0070\u006f\u0072\u0074\u0061\u006e\u0074';

    How would I convert that into a text string? The program I'm working in, encodes it like that (to hide it from the front end users) ... but we are now moving over to a new system, so need to "reverse engineer" it, so work out what the phrases actually say =)


I2C help (from python)
3 direct replies — Read more / Contribute
by packetstormer
on Nov 30, 2015 at 10:30

    Hello monks, the following is a long-shot!

    I have a small raspberry pi relay board that I am trying to use. The manufacturer has provided some sample Python code to open and close the relay. However, as the rest of the project is written in perl I'd like to get it working all in one module

    Could anyone take a shot at explaining what the python lines below do and how they might translate to perl?

    I can manage to open all ports (at the same time!), using Device::SMBus on the relay board in perl but I can't figure out what "&=" and "|&" means in the source script

    class Relay(): global bus def __init__(self): self.DEVICE_ADDRESS = 0x20 #7 bit address (will be left shift +ed to add the read write bit) self.DEVICE_REG_MODE1 = 0x06 self.DEVICE_REG_DATA = 0xff bus.write_byte_data(self.DEVICE_ADDRESS, self.DEVICE_REG_MODE1, se +lf.DEVICE_REG_DATA) def ON_1(self): print 'ON_1...' self.DEVICE_REG_DATA &= ~(0x1<<0) bus.write_byte_data(self.DEVICE_ADDRESS, self.DEVICE_REG_MODE1 +, self.DEVICE_REG_DATA)

    And my Perl Code

    #!/usr/bin/perl use strict; use Device::SMBus; use Data::Dumper; my $dev = Device::SMBus->new( I2CBusDevicePath => '/dev/i2c-1', I2CDeviceAddress => 0x20, ); $dev->writeByteData(0x06,0x1<<0); print Dumper $dev

    With the perl code all the relays open (or close). So it's really this line self.DEVICE_REG_DATA &= ~(0x1<<0) that's causing my trouble.

    Like I said, I know this is a long shot but I'd appreciate some input

Activeperl 5.16.1 Ipv6 support
1 direct reply — Read more / Contribute
by rama133101
on Nov 30, 2015 at 06:42

    Hi, I am trying to create an application that support Dual stack IP. I am using the IO::Socket::IP which allows both IPv4 and IPv6. Below is the piece of code I took from Socket::IP cpan page.

    use Net::SSH2; use IO::Socket::IP; use Socket qw(:addrinfo SOCK_RAW); use Socket6; my $ipv6 = "2620:0:170:2531:60:165d:4f14:1068"; my $ipv4 = ""; my $sock = IO::Socket::IP->new( PeerHost => $ipv6, PeerPort => "22", Timeout => 3 ) or die "Cannot construct socket - $@";

    The above code is giving "Cannot construct socket - nodename nor servname provided" error. Initially I was using perl 5.20.2 and everything was working fine. Now,for some reason I had to move to 5.16.1 and this same piece of code is giving error.

    I went through couple of posts discussing similar issues in ipv6 support. One suggestion I see is the usage of getaddrinfo. But, that doesn't help me in any way. Another post says, Socket doesn't support Ipv6. Another post suggests using socket6. I have tried all options and nothing helps

    kindly provide suggestions. I believe I am missing out something in Perl 5.16.

insert data into data structure
3 direct replies — Read more / Contribute
by Genmai
on Nov 30, 2015 at 04:43
    Hi Monks! I'm trying to read data from a file and insert the data into a complex data structure. I have a file like this:
    .. and so forth..
    I really can't find a way to do it,i want some multidimensional structure so that given paragraph5 i can access all the keys and change the values or add a line or delete it. I tried using array of array of hashes and other mix but no luck. Thank you for your help
Problem with 'Distribution::Guess::BuildSystem' module
1 direct reply — Read more / Contribute
by Krzysztofc
on Nov 30, 2015 at 03:36


    I'm new to the '' site and not wanting to violate the existing rules here, but I would like to ask at which section - if any of course - could I present a problem with which I struggle ?

    I want to install the module 'Module::Starter::AddModule'. However, during installation the 'Distribution::Guess::BuildSystem' module the error occurs. The 'Distribution::Guess::BuildSystem' is a required component.

    Testing of Distribution::Guess::BuildSystem does not pass.

    [root@pars-k-018 ~/tt1/Distribution-Guess-BuildSystem-0.12]# make test PERL_DL_NONLAZY=1 "/usr/local/bin/perl5" "-MExtUtils::Command::MM" "-M +Test::Harness" "-e" "undef *Test::Harness::Switches; test_harness(0, +'blib/lib', 'blib/arch')" t/*.t t/_setting.t .................... ok t/file_has_string.t ............. ok t/just_give_me_a_hash.t ......... ok t/load.t ........................ ok t/makemaker-build-either.t ...... ok t/makemaker-build-wrapped.t ..... ok t/makemaker-true.t .............. 1/? # Failed test 'makemaker_version returns true (good)' # at t/makemaker-true.t line 94. # Looks like you failed 1 test of 30. t/makemaker-true.t .............. Dubious, test returned 1 (wstat 256, + 0x100) Failed 1/30 subtests t/module-build-compat.t ......... ok t/module-build.t ................ ok t/module-install-autoinstall.t .. ok t/module-install.t .............. ok t/pod.t ......................... skipped: Test::Pod 1.00 required for + testing POD t/pod_coverage.t ................ skipped: Test::Pod::Coverage 1.00 re +quired for testing POD coverage t/preferred.t ................... ok Test Summary Report ------------------- t/makemaker-true.t (Wstat: 256 Tests: 30 Failed: 1) Failed test: 20 Non-zero exit status: 1 Files=14, Tests=261, 6 wallclock secs ( 0.22 usr 0.07 sys + 3.91 cu +sr 0.65 csys = 4.84 CPU) Result: FAIL Failed 1/14 test programs. 1/261 subtests failed. *** Error code 255 Stop. make: stopped in /root/tt1/Distribution-Guess-BuildSystem-0.12

    Would You be so kind to help me with this or point me, where can I get a help to resolve this problem.

    I'm running it on the following system:

    # uname -a FreeBSD pars-k-018 10.0-RELEASE-p18 FreeBSD 10.0-RELEASE-p18 #0: Wed F +eb 25 01:08:00 UTC 2015 +bj/usr/src/sys/GENERIC amd64

    Thank you in advance for your help

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 about the Monastery: (7)
    As of 2015-12-01 05:04 GMT
    Find Nodes?
      Voting Booth?

      What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

      Results (794 votes), past polls