Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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
eq evaluates a variable as equal to two different strings
2 direct replies — Read more / Contribute
by landru27
on Jan 30, 2015 at 15:42

    I have encountered a very strange thing, and I'm wondering if anyone else has seen it, and if so what he or she might have done about it. The most frustrating aspect of this is that I cannot reproduce the problem with a short program that contains the same code.

    As background, allow me to say that I've been working with Perl for some 20 years. I'm not a newbie, and I've eliminated many possibilities already. I'm open to the possibility that I'm missing something fundamental, but I'm as certain as a person can be that it's nothing basic.

    The code is thus:

    ... $baz = $info{$key}; # $baz = unpack('H*', $baz); # $baz = pack('H*', $baz); print(STDOUT '[' . ($baz) . "]\n"); print(STDOUT '[' . (unpack('H*', $baz)) . "]\n" ); print(STDOUT '[' . ($baz eq 'true') . "]\n"); print(STDOUT '[' . ($baz eq 'false') . "]\n"); print(STDOUT '[' . ($baz eq 'default') . "]\n"); ...

    With those unpack() and pack() calls commented out and when the value of $info{$key} is 'false' (that is, the string 'false'), the code in-place in the larger program yields:

    [false] [66616c7365] [] [1] [1]

    When I remove the comment markers (thus calling unpack() and then pack() in succession on the value), it yields the much more expected:

    [false] [66616c7365] [] [1] []

    Again, if I take these lines and make them into a short program by themselves, the unpack() / pack() dance is not necessary; the three 'eq' comparisons yield the second block of output, as expected.

    So, we have a situation where (a) 'eq' is evaluating the same value to be equivalent to two different strings, and (b) an unpack() followed immediately by pack() is having an effect on the value, when that should not happen (at least, not with a template as simple as 'H*') since these two functions are mirrors of each other.

    Has anyone ever seen the same kind of unexpected funniness with 'eq'? Does the fact that unpack() + pack() affects the value here tell us anything about the nature of the underlying data that is salient to understanding the unexpected 'eq' results? And, related, is there something about the inner operation of unpack() / pack() that sheds some light on this oddity?

    Thanks in advance for any insight. I'm happy to provide additional detail that might help diagnose this.

Can I re-use modules compiled with a different build of Perl, but with the same version number on the same machine?
3 direct replies — Read more / Contribute
by neuty
on Jan 30, 2015 at 14:47

    My department's production servers (RHEL5) have no access to the Internet or any CPAN repository. Therefore, when we need to use a specific Perl module with our scripts, we download them to our terminals from the CPAN website, scp them over to our dev machines, untar them, and then build them locally. We then distribute them to a shared network file system so that other developers may use them, and so that they are available on the prod machines as well. In this way we build a library of Perl modules that slowly grows over time. Our current set of modules were built using my system Perl, which /usr/bin/perl -V lists as revision 5 version 10 subversion 1.

    However, we're interested in building a separate copy of Perl that we can also upload to the networked file system. This way we are not tied to using each of our machines' system Perl, which can change from machine to machine (and is frequently 5.8.8, which is outdated for a few necessary modules). A sort of "central" Perl that can be standard across each machine.

    Therefore, I needed to build a local copy of Perl in my home directory. I made sure to install the same version as my system Perl, perl-5.10.1. I did this using the following Configure followed by a simple make,make test, make install:

    ./Configure -des -Dprefix=/home/myuser/localperl -Duserelocatableinc

    -Dprefix installed it to the folder localperl in my home directory, while the -Duserelocatableinc flag made it so the @INC would change if the folder was moved (ie., uploaded to the networked file system).

    The issue comes when I try to test it out with one of my scripts that requires a module from our library of manually built Perl modules. I keep getting the following error:

    /home/myuser/localperl/bin/perl: symbol lookup error: .../auto/DBI/ undefined symbol: Perl_Istack_sp_ptr

    Obviously my script uses the shared DBI module but is running into errors. From doing research on the Internet, people have said that I would need to recompile the modules using the new build of Perl.

    However, the two Perls are the same version and were built on the same machine. I have tried to rebuild the local Perl using the Configure settings as close to the machine Perl as possible to try to see if that would work:


    So far this has not stopped the error. My system Perl actually used many more Configure options, but since I was so unfamiliar with them, I didn't throw them into the local Perl build for obvious precautionary reasons.

    First, can someone help me get a better understanding of what is preventing these modules from working with my locally built Perl? It makes sense that modules would not work under different versions of Perl, but it surprises me that it doesn't work with the same version built on the same machine. It also does not make sense to me that a module would lock itself info a specific Perl build like a "license" type scenario, so my intuition is that the module is incompatible with some unseen setting/library that the local Perl is built under.

    Second, is there a way to get my locally built Perl to work with the modules compiled with the system Perl, or will I have to recompile them all and redistribute a fresh set of modules?

    Thank you for any and all help! I have tried Googling the answer, but any issues dealing with this only bring up different Perl versions, which is not the situation I am currently facing.

extracting attribute values which might have multiple occurences from xml file
2 direct replies — Read more / Contribute
by perlDevsWorld
on Jan 30, 2015 at 14:01
    I need help in extracting some information from xml file using perl. 1) test.xml file How can I extract and key and tier and then put it in one file ( a text file ) using perl scripts
    <httpsRoutes> <httpsRoute hostname="" port="443" environment="Q +A" tier="dmz"> <key>6924</key> </httpsRoute> <httpsRoute hostname="" port="7416" environment=" +CAT" tier="dmz"> <key>9990068</key> </httpsRoute> </httpsRoutes>
Generate a # between 1 and 50 in groups of 5
5 direct replies — Read more / Contribute
by Hayest
on Jan 30, 2015 at 13:45

    Hello, as the title states, I'm trying to get a Perl program to generate a number between 1 and 50 in groups of 5. This is for a school assignment, and I'm wondering where I need to go next (I am a beginner with Perl and programming in general). Here is what I have so far:

    #!/usr/bin/perl use strict; use warnings; use List::Util 'shuffle'; my @numbers = (1 .. 50); print shuffle(@numbers);

    I was able to find the 'shuffle' utility which works to print my whole array (with numbers between 1 and 50). Now I know how to create a for loop which would go through each number in the array and print on a new line:

    for ($i = 0; $i < @numbers; ++$i) { ... }

    What I want to know is how would I get it to pick five of the numbers inside of it, and print them each on a new line (5 new lines with a number between 50 on each line), line break, print 5 more, line break, etc. I want the program to terminate after it does this for 5 cycles. Any thoughts? Any help would be much appreciated!!!

Using threads to process multiple files
4 direct replies — Read more / Contribute
by anli_
on Jan 30, 2015 at 11:36
    Hey. I'm trying to get the feel for how multithreading works with perl in an effort to try to speed up my scripts. My test case is that I want to parse two relatively large files and place all the lines from each file into separate hashes. Normally I would just go over them one by one with a while loop and populate my hashes that way. Like so.
    open IN, '<', $ARGV[0]; my %hash1; while (<IN>) { next unless ( $_ =~ m/^\@HWI/ ); my ($header) = split(/ /, $_); $hash1{$header} = 1; } close IN; open IN2, '<', $ARGV[1]; my %hash2; while (<IN2>) { next unless ( $_ =~ m/^\@HWI/ ); my ($header) = split(/ /, $_); $hash2{$header} = 1; } close IN;
    Instead I thought I'd try doing both files at once. Like so:
    my @threads = ("1","2"); # Loop through the array: foreach(@threads){ # Tell each thread to perform our 'parseLines()' subroutine. $_ = threads->create(\&parseLines, shift(@ARGV)); } #Tries to join the running threads #Some check implemented to avoid quitting the loop, before everything +is joined my @running = threads->list(threads::running); #Array of running threa +ds my @joinable = threads->list(threads::joinable); #Array of joinable th +reads my @catcher; while (scalar(@running) != 0 || scalar(@joinable) > 0) { #While as lon +g as there are running or joinable threads @running = threads->list(threads::running); #Repopulate running, n +ot sure if needed. foreach(@threads){ if ($_->is_joinable()) { push(@catcher, $_->join()); #Put's parsed file as hash-ref int +o array } } @joinable = threads->list(threads::joinable); @running = threads->list(threads::running); } sub parseLines{ open IN, '<', $_[0]; my %hash; while (<IN>) { next unless ( $_ =~ m/^\@HWI/ ); my ($header) = split(/ /, $_); $hash{$header} = 1; } close IN; return \%hash; }
    While the multithreaded code works, it's about 50% slower than the first one, so nothing is really accomplished there. It seems to me that maybe the problem is that the threads take a long time passing the created hashes back to the main script. So it's shuffling things around in memory, but I'm on really thin ice here I must admit. Any input is appreciated.
How to fork in PSGI/Dancer2
2 direct replies — Read more / Contribute
by morgon
on Jan 30, 2015 at 07:04

    I am currently playing around with Dancer2 and wonder about the proper way to start a long-running child-process.

    But even though I currently use Dancer2 I guess my question is more about PSGI in general.

    I don't know that much about it but my understanding is that PSGI/Plack abstracts away the deployment details, so in theory your PSGI application should not be affected regardless of whether the app is later deployed as GCI or running under starman or whatever, but I cannot see how you can abstract away all the mechanics of forking (e.g. what part of the system reaps the childs again etc).

    So what is the best practice here?

    Not forking at all and using some job-qeueue mechanism or dealing with it on a case-by-case basis as there is no abstraction for it?

    Please enlighten me...

How does the while works in case of Filehandle when reading a gigantic file in Perl
7 direct replies — Read more / Contribute
by raj4489
on Jan 30, 2015 at 06:15

    I have a very large file to read, so when I use while for reading it line by line, the script starts taking more time to read the line as I dig deep in the file; and to mention the rise is exponential.

    while (<$fh>) {do something}

    Does while has to parse through all the lines it has already read to go to the next unread line or something like that? How can I overcome such a situation? Thanks in advance

Error when using formatting excel output using addformat
2 direct replies — Read more / Contribute
by sanind
on Jan 30, 2015 at 05:48
    I am getting the following error when using addformat during hash to excel conversion. Can't locate object method "addformat" via package "Spreadsheet::WriteExcel::Worksheet" at ./ line 1012, <ets_qor_report>
    #!/usr/bin/perl use strict ; use envParser ; use Data::Dumper ; use Tie::IxHash ; use File::Basename ; use KscAccessDD ; use Getopt::Long ; use Text::Table ; use Spreadsheet::WriteExcel ; #use Spreadsheet::WriteExcel::Format ; <.....Hash preparation code...> my $XL = Spreadsheet::WriteExcel->new('output_file.xls'); my $WS = $XL->add_worksheet('my data'); my $format = $WS->add_format(); $format->set_bold(); $format->set_color('black'); $format->set_align('center'); $format->set_size('10'); my $count = 1 ; my ($ROW, $COL) = (0, 0); for my $colname (keys %ets_qor_hash) { $WS->write($ROW, $COL,$count); ++$COL; }
    Any help is appreciated
Converting .pl to .exe file
5 direct replies — Read more / Contribute
by mohad
on Jan 30, 2015 at 00:23

    hello All, I'm new to Perl and have written a small code. Now I want this .pl file to be converted into .exe file so that it can run on any system(only windows) without perl being installed. can someone please guide step by step ?

    here is my code:

    use strict; use warnings; use Cwd; my @operations = ( { label => 'Harcoded font size value !!', test => sub { $_[0] =~ /font-size\s*(:|=)\s*[^%]+$/ }, }, { label => 'Forced Line height value !!', test => sub { $_[0] =~ /line-height/ }, }, { label => 'position absolute !!', test => sub { $_[0] =~ /position\s*(:|=)\s*absolute/ }, }, ); my $dirpath = getcwd; my @filepaths = glob( "$dirpath/*.css" ); print "no css files\n" unless @filepaths; for my $filepath ( @filepaths ) { print "\n>>> $filepath <<<\n"; $_->{count} = 0 for ( @operations ); # set or reset counts. open my $filehandle, '<', $filepath or die "could not open '$filep +ath': $!"; while ( my $line = <$filehandle> ) { $_->{test}->( $line ) and $_->{count}++ for ( @operations ); # + for each operation, call the test and increment count if it returns +true. } close $filehandle; print "$_->{label} $_->{count} Instance found\n" for ( @operations + ); # for each operation, print the outcome. }
SOAP::Lite client request over HTTPS hangs until connection is reset by server
2 direct replies — Read more / Contribute
by kiteboy
on Jan 29, 2015 at 23:34

    Wise monks,

    Iím trying to use a script that sends a SOAP::LITE request over HTTPS. The script was written by a colleague, and while I have a bit of perl experience I have no SOAP or HTTPS apart from reading up the past few days. The script make one SOAP::LITE request to a server, hangs for 60 seconds and then continues as expected with an expected response. It appears the SOAP::LITE request hangs until the connection is reset by the server (causing a 60 sec delay between making the request and being able to process the response).

    I have confirmed that the server response quickly to the request and then the script hangs until the reset is received using tcpdump.

    Here is a sanitized version of the code. Hopefully there is enough to give an idea what it is doing...

    #!perl -w use Net::SSL; use SOAP::Lite; $SOAP::Constants::DO_NOT_USE_CHARSET = 1; use XML::LibXML; my ($function, @params) = @ARGV; $ENV{HTTPS_CERT_FILE} = "./xxxxx.crt"; $ENV{HTTPS_KEY_FILE} = "./xxxxx.key"; my $sitetoken= SOAP::Header->name( CODE REMOVED ); my $ppsoapheader=SOAP::Header->name( CODE REMOVED ); my $auth = SOAP::Header->name( CODE REMOVED ); my $namespace = SOAP::Data->new( CODE REMOVED ); $soap_response = SOAP::Lite -> uri(' +viceAPI/V1') -> proxy('' +) -> GetEDUMigrationData($namespace, $ppsoapheader, $auth);

    Wise monks, any ideas why this is hanging until the server reset is received?

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.
  • 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 cooling their heels in the Monastery: (4)
    As of 2015-01-31 04:04 GMT
    Find Nodes?
      Voting Booth?

      My top resolution in 2015 is:

      Results (255 votes), past polls