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

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
Re learning Perl
3 direct replies — Read more / Contribute
by Anonymous Monk
on Mar 02, 2015 at 16:18


    I've been dabbling with Perl on and off since a while. Managed to write some decent scripts. After a two year gap, am looking forward to give it another try.

    I started with Learning Perl Book, as many here would have started, and by the time I finished about 70% of it, was able to write scripts that helped automate monitoring stuff. Of course I have forgotten most of the syntax etc, so wondering if its better to start once again with "Learning Perl", or directly jump to Intermediate Perl and then, as and when need arises, "refill" the gap by going back to Learning Perl? I've heard that it's the next book to go for after Learning Perl.

    Wanted to see how much I still remember. So quickly whipped up the following.

    #!/usr/bin/perl use warnings; use strict; my @numbers = (1..100); my @tripled = map {$_ *3} @numbers; print "tripled -> $_\t" foreach (@tripled); print "\n"; my @ends_with_2 = grep {$_ =~ /2$/} @numbers; print "Ends With 2 -> $_\t" foreach (@ends_with_2); my %somehash = ( "firstname" => "first", "secondname" => "middle", "lastname" => "surname", ); print "\n"; foreach my $thing (sort keys %somehash) { print "Key$thing, Value is $somehash{$thing}\n"; }

    And it worked.

    perl tripled -> 3 tripled -> 6 tripled -> 9 tripled -> 12 triple +d -> 15 tripled -> 18 tripled -> 21 tripled -> 24 tripled -> +27 tripled -> 30 tripled -> 33 trip led -> 36 tripled -> 39 tripled -> 42 tripled -> 45 triple +d -> 48 tripled -> 51 tripled -> 54 tripled -> 57 tripled -> +60 tripled -> 63 tripled -> 66 trip led -> 69 tripled -> 72 tripled -> 75 tripled -> 78 triple +d -> 81 tripled -> 84 tripled -> 87 tripled -> 90 tripled -> +93 tripled -> 96 tripled -> 99 trip led -> 102 tripled -> 105 tripled -> 108 tripled -> 111 triple +d -> 114 tripled -> 117 tripled -> 120 tripled -> 123 tripled -> +126 tripled -> 129 tripled -> 132 trip led -> 135 tripled -> 138 tripled -> 141 tripled -> 144 triple +d -> 147 tripled -> 150 tripled -> 153 tripled -> 156 tripled -> +159 tripled -> 162 tripled -> 165 trip led -> 168 tripled -> 171 tripled -> 174 tripled -> 177 triple +d -> 180 tripled -> 183 tripled -> 186 tripled -> 189 tripled -> +192 tripled -> 195 tripled -> 198 trip led -> 201 tripled -> 204 tripled -> 207 tripled -> 210 triple +d -> 213 tripled -> 216 tripled -> 219 tripled -> 222 tripled -> +225 tripled -> 228 tripled -> 231 trip led -> 234 tripled -> 237 tripled -> 240 tripled -> 243 triple +d -> 246 tripled -> 249 tripled -> 252 tripled -> 255 tripled -> +258 tripled -> 261 tripled -> 264 trip led -> 267 tripled -> 270 tripled -> 273 tripled -> 276 triple +d -> 279 tripled -> 282 tripled -> 285 tripled -> 288 tripled -> +291 tripled -> 294 tripled -> 297 trip led -> 300 Ends With 2 -> 2 Ends With 2 -> 12 Ends With 2 -> 22 + Ends With 2 -> 32 Ends With 2 -> 42 Ends With 2 -> 52 + Ends With 2 -> 62 Ends With 2 -> 72 Ends With 2 -> 82 Ends With 2 -> 92 Key is firstname, Value is first Key is lastname, Value is surname Key is secondname, Value is middle

    I remembered Map and Grep, but did not remember the syntax. A quick look at the perldocs refreshed it and it sort of came back to me. So question is, should I jump in to reading (and trying out exercises) in Intermediate Perl, or should I go back to Learning Perl and start all over again?

Splitting folded MIME headers into indivual headers?
3 direct replies — Read more / Contribute
by sebastiannielsen2
on Mar 02, 2015 at 15:43

    Im trying to split a scalar containing multiple MIME header lines, that might be folded, into a Array that should hold one complete header per element

    I tried with the following:

    @fixedheaders = split(/\n\S/, $fixedmsgheader);

    But that eats the first char in header lines. I need to split the header line so it takes folded header lines in consideration and NOT splitting a folded header line in the middle.

SSL Certificate Verification problem, using LWP::UserAgent
1 direct reply — Read more / Contribute
by ManyHats
on Mar 02, 2015 at 13:44

    The script below fails with this error: 500 Can't connect to (certificate verify failed) Can't connect to (certificate verify failed) LWP::Protocol::https::Socket: SSL connect attempt failed error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed at /Library/Perl/5.16/LWP/Protocol/ line 47.

    My best take on the error message is that the SSLeay (openssl) code is unable to verify the certificate. However, if I use the command 'openssl s_client -host -port 443', I get an OK at the end (see below), making me think that openssl is able to verify the certificate. So I have a "reality mismatch" with which I need help.

    New, TLSv1/SSLv3, Cipher is AES256-SHA Server public key is 2048 bit Secure Renegotiation IS supported Compression: NONE Expansion: NONE SSL-Session: Protocol : TLSv1 Cipher : AES256-SHA Session-ID: 8FA80F5DE8D33D29B20AE38665A96F1EE29517E229038F6EDDE9D8 +CA294DFEF8 Session-ID-ctx: Master-Key: D1E833AEFD8C0D9345BAD5996ED1B1D0D6E8F58CABBCAE47071B15 +0B49AD510A1A2D4075719C5296D01FDDBA3DFD67C3 Key-Arg : None Start Time: 1425321673 Timeout : 300 (sec) Verify return code: 0 (ok)

    If I uncomment the line that sets verify_hostname to zero, the error goes away. It is my understanding that all this option does is to disable the identify check that is done outside of openssl.

    Note that "working" code will still break because this code snippet does not providing the proper login information. In other words, I expect a whiny message from Salesforce that will include "INVALID_LOGIN: Invalid username, password"

    I do get more debug information if I run the program like this: 'perl -MIO::Socket::SSL=debug30 Testcase':

    Newton:salesforce_work tpl$ perl -MIO::Socket::SSL=debug30 Testcase DEBUG: .../IO/Socket/ new ctx 140688297857808 DEBUG: .../IO/Socket/ socket not yet connected DEBUG: .../IO/Socket/ socket connected DEBUG: .../IO/Socket/ ssl handshake not started DEBUG: .../IO/Socket/ not using SNI because openssl is too +old DEBUG: .../IO/Socket/ set socket to non-blocking to enforce + timeout=120 DEBUG: .../IO/Socket/ Net::SSLeay::connect -> -1 DEBUG: .../IO/Socket/ ssl handshake in progress DEBUG: .../IO/Socket/ waiting for fd to become ready: SSL w +ants a read first DEBUG: .../IO/Socket/ socket ready, retrying connect DEBUG: .../IO/Socket/ Net::SSLeay::connect -> -1 DEBUG: .../IO/Socket/ ssl handshake in progress DEBUG: .../IO/Socket/ waiting for fd to become ready: SSL w +ants a read first DEBUG: .../IO/Socket/ socket ready, retrying connect DEBUG: .../IO/Socket/ Net::SSLeay::connect -> -1 DEBUG: .../IO/Socket/ ssl handshake in progress DEBUG: .../IO/Socket/ waiting for fd to become ready: SSL w +ants a read first DEBUG: .../IO/Socket/ socket ready, retrying connect DEBUG: .../IO/Socket/ ok=0 cert=140688300414128 DEBUG: .../IO/Socket/ Net::SSLeay::connect -> -1 DEBUG: .../IO/Socket/ SSL connect attempt failed DEBUG: .../IO/Socket/ SSL connect attempt failed error:140 +90086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify fai +led DEBUG: .../IO/Socket/ fatal SSL error: SSL connect attempt +failed error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certif +icate verify failed DEBUG: .../IO/Socket/ IO::Socket::INET6 configuration fail +ed DEBUG: .../IO/Socket/ free ctx 140688297857808 open=140688 +297857808 DEBUG: .../IO/Socket/ free ctx 140688297857808 callback DEBUG: .../IO/Socket/ OK free ctx 140688297857808 500 Can't connect to (certificate verify fail +ed) Can't connect to (certificate verify failed) LWP::Protocol::https::Socket: SSL connect attempt failed error:1409008 +6:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed +at /Library/Perl/5.16/LWP/Protocol/ line 47.
    The message from line 2458 says that OK is zero, which makes me think that openssl already said that the certificate is bad (does openssl 0.9.8 check identity? Why would it not like it now even though is seems to like it in the openssl s_client command?)

    I'd appreciate any enlightenment

    #!/usr/bin/perl -w use strict; use LWP::UserAgent; use Mozilla::CA; # To set debug we can run the program like this: perl -MIO::Socket::S +SL=debug30 Testcase # Print versions #print "LWP::UserAgent->VERSION is " . LWP::UserAgent->VERSION . "\n"; my ($Request, $Reply, $res); # A User agent for all WWW requests my $ua = LWP::UserAgent->new( timeout => 120 ); $ua->ssl_opts( # verify_hostname => 0, SSL_ca_file => Mozilla::CA::SSL_ca_file(), SSL_verifycn_scheme => 'http', SSL_verifycn_name => '', ); my $LoginXML = <<EOF; <?xml version="1.0" encoding="utf-8" ?> <env:Envelope xmlns:xsd="" xmlns:xsi="" xmlns:env=""> <env:Body> <n1:login xmlns:n1=""> <n1:username>USERNAME</n1:username> <n1:password>AUTHCODE</n1:password> </n1:login> </env:Body> </env:Envelope> EOF $Request = HTTP::Request->new(POST => " +rvices/Soap/u/22.0"); $Request->header( 'Content-Type' => 'text/xml; charset=UTF-8', 'SOAPAction' => 'login' ); $Request->content($LoginXML); # Make the request and check the results $res = $ua->request($Request); $Reply= $res->content; if (!($res->is_success)) { warn $res->status_line, "\n"; warn $Reply, "\n"; } else { warn "The request succeeded\n"; }
change unintialized warning
3 direct replies — Read more / Contribute
by hailholyghost
on Mar 02, 2015 at 08:31
    Hello, I have a 355 line code that takes about 3 hours to run. I find errors like "Use of uninitialized value in subroutine entry at line 311" I have 4 different arrays on this line which could have been uninitialized. How can I set perl to change this warning to something like "Use of uninitialized value $hash{key1}{key2} in subroutine entry at line 311" in other words, how can I get the uninitialized warnings to tell me which value is uninitialized? To compensate for the vague warning, I have littered my code with "if(!defined($var))" but I would prefer a better solution than this. thanks, -DEC
Who called Perl?
4 direct replies — Read more / Contribute
by Discipulus
on Mar 02, 2015 at 03:25
    Hello monks, I asked yet this in chat and i'm almost convinced there is no answer but..
    as we say in my country ask is free so:

    Perl has some knoweledge about who called him? It must be some shell in all Linux flavours (bash, csh, ksh..) or in win32 the stupid cmd.exe, or maybe another modified shell-like program (Console2, powershell.. ).

    Once Perl has started I can easily make a system call to get description of the PID using $$ but i'd like to investigate the parent of such PID.

    Any ideas?

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
How can I debug a weird "Undefined subroutine..." error that should be defined.
1 direct reply — Read more / Contribute
by dly
on Mar 01, 2015 at 03:12

    Apologies for no real code, but the simple case does not exhibit my problem, only my real world case needing an FTP server set up exhibits the problem.

    The Simple code:

    # OVERRIDE_BUILD imports run_star_commands(). use App::Fetchware qw(:OVERRIDE_BUILD); run_star_commands();

    seems to work just fine :(

    But my complicated test environment involving a test FTP server exhibits this problem (Partial code listing):

    ... use Sub::Mage 'sublist'; diag("WEBAPPSLOADED!!!!!!!!!!!["); print "$_\n" for sublist(); diag("]"); use B::Deparse; my $deparse = B::Deparse->new("-p", "-sC"); my $body = $deparse->coderef2text(\&run_star_commands); diag("RUNSTARCOMMANDS["); diag("$body"); diag("]"); # Use run_start_commands from :OVERRIDE_BUILD to support uploa +d_commands # being a true ONEARRREF and having multiple commands to call. run_star_commmands(@upload_commands); } else { ...

    That has the following output:

    # WEBAPPSLOADED!!!!!!!!!!![ have after DATA_PROT_CONFIDENTIAL ... run_star_commands ... constructor filter splitpath CMD_OK download_dirlist # ] # RUNSTARCOMMANDS[ # { # package App::Fetchware; # use warnings; # use strict; # no feature; # use feature ':5.10'; # (my(@star_commands) = @_); # foreach my $star_command (@star_commands) { # if (($star_command =~ /,\s*/)) { # (my(@star_commands) = split(/,\s*/, $star_command, 0)); # foreach my $split_star_command (@star_commands) { # run_prog($split_star_command); # } # } else { # run_prog($star_command); # } # } # } # ] Undefined subroutine &App::FetchwareX::WebApp::run_star_commmands call +ed at /home/dly/Desktop/Code/App-Fetchware/lib/App/FetchwareX/WebApp line 768 (#2) (F) The subroutine indicated hasn't been defined, or if it was, it + has since been undefined. Uncaught exception from user code: Undefined subroutine &App::FetchwareX::WebApp::run_star_commma +nds called at /home/dly/Desktop/Code/App-Fetchware/lib/App/FetchwareX +/ line 768. at /home/dly/Desktop/Code/App-Fetchware/lib/App/FetchwareX/ +line 768. App::FetchwareX::WebApp::build('a') called at t/App-FetchwareX +-WebApp.t line 733 # Tests were run but no plan was declared and done_testing() was not s +een. # Looks like your test exited with 25 just after 5. Dubious, test returned 25 (wstat 6400, 0x1900) Failed 1/6 subtests Test Summary Report ------------------- t/App-FetchwareX-WebApp.t (Wstat: 6400 Tests: 6 Failed: 1) Failed test: 5 Non-zero exit status: 25 Parse errors: No plan found in TAP output Files=1, Tests=6, 0 wallclock secs ( 0.03 usr 0.01 sys + 0.36 cusr + 0.12 csys = 0.52 CPU) Result: FAIL

    As you can see from the output right before I call run_star_commands() I call Sub::Mage's sublist() subroutine that returns all subroutines defined in the current package. I snipped the output, but the important part is that run_star_commands() is listed.

    I also use B::Deparse to deparse run_star_commands() to see if run_star_commands() can be deparsed, and it deparses it just fine.

    Then the very next thing I do is call run_star_commands(), but it fails with an "Undefined subroutine" error message.

    My test setup is a real disaster, so testing my code would require installing a bunch of modules, setting some crazy environment variables, and access to a FTP server. So, instead of asking you guys to git clone my repo, set insane environment variables, and setup an FTP server, what I'm looking for is a way to debug this problem myself.

    Could setting a watchpoint in the debugger for run_star_commands() reveal what's making it vanish without a trace right before perl calls it? Watchpoints only seem to work for regular variables as using something like:

    w $App::FetchwareX::WebApp{run_star_commands}{CODE}

    causes the debugger to freak out. Is their some sort of way of accessing a subroutine "as a variable", so that I can set a watch expression on it to see where it changes?

ip_match doesn't seem to work
3 direct replies — Read more / Contribute
by mwhiting
on Feb 27, 2015 at 10:54

    This is something of a ressurection of a question from this past month.

    I am using Session Id's and have started trying to use -ip_match so that you can't cut and paste an url with the session id embedded in it from one computer to the next. But since I have added -ip_match I don't get any different results when accessing the same script from computers with different IP's. I modified my Use statement to be:

    use CGI::Session qw/-ip-match/;

    instead of my usual use cgi::Session; line. I also tried adding

    $session::IP_MATCH = 1;

    to the mix, after the creation of the session object, and still I see no difference in the results on browsers with different ip's. The different computers still access the session info just fine and pass the authentication using info in the session variable.

    What am I missing here in the use of -ip_match ? Your help would be greatly appreciated!   :)

Want END block to run except when usage()/--help invocation
2 direct replies — Read more / Contribute
by CoVAX
on Feb 26, 2015 at 23:50

    This is the first time I've used an END block. I need its functionality because I call 'die' at several points throughout this program and I have to always perform the code in the END block.

    I was making the final edits to a program's usage() function and noticed the following warnings following a --help invocation:

    perl -w -h
    Use of uninitialized value $time in concatenation (.) or string at ks. +pl line 253. Use of uninitialized value in concatenation (.) or string at lin +e 258. Use of uninitialized value in concatenation (.) or string at lin +e 258. Use of uninitialized value in concatenation (.) or string at lin +e 258. Use of uninitialized value in concatenation (.) or string at lin +e 258. Character in 'c' format wrapped in pack at line 262. Use of uninitialized value $time in concatenation (.) or string at ks. +pl line 283.

    Ah so! (German for "I see!") -- these warnings were the result of the END block being (unconditionally) executed after the usage() function.

    While I understand why this is happening, my question is: is there a way to have an unconditional END block execute except when the program's usage() function is being invoked? If not, then I propose to create yet another function to be called prior to die()'ing that will execute what's presently in the END block.

    Here's the relevant code:

    #!/usr/bin/perl -w use strict; use Getopt::Std; ... # Don't want END block to execute when these are invoked: sub HELP_MESSAGE(); # getopts() --help automatically invokes VERSIO +N_MESSAGE() first sub VERSION_MESSAGE(); # getopts() supports arguments --version and -- +help sub usage(); ... INIT { $| = 1; # Make Getopt::Std exit after printing # VERSION_MESSAGE() and/or HELP_MESSAGE() $Getopt::Std::STANDARD_HELP_VERSION = 1; } ...By design, main-line code comes to this END block: # An END block is always executed (barring some exceptions) as # late as possible, even after die() is called END { export_to_file_csv(); export_to_storable(); warn "Pages : $pages of Total Pages: $total_pages"; warn "Projs : $projs of Total Projs: $total_projs"; warn sprintf ("Images: %d amounting to bytes: %s", $imgs, $bytes)); } # END OF PROGRAM (normal or otherwise).

    I thank you for your time and assistance.

    Searched for donut and crumpit. Found donate and stumbit instead.
cpanm not retrieving remote files
2 direct replies — Read more / Contribute
by cLive ;-)
on Feb 26, 2015 at 16:43

    Confused about this. From the cpanm build log:

    Searching Net::SSH on cpanmetadb ... --> Working on Net::SSH Fetching -> FAIL Download +.tar.gz failed. Retrying ... -> FAIL Download +.tar.gz failed. Retrying ... -> FAIL Download +.tar.gz failed. Retrying ... -> FAIL Failed to download +t-SSH-0.09.tar.gz -> FAIL Failed to fetch distribution Net-SSH-0.09

    But, if I run this in the shell:


    the module downloads as expected, and I can install manually.

    That's great and all, but then I have to install each dependency manually.

    I've deleted the .cpanm and .cpan directories in /root, but I still keep hitting this issue with every module I install. I've tried specifting wget or curl as CL args on cpanm, but they don't seem to be making a difference.

    It worked fine when I first installed it, but then just stopped a few weeks ago.

    I'm not sure how to debug this one. Any ideas?

Why did @$ variable bite me in the ass?
3 direct replies — Read more / Contribute
by dmitri
on Feb 26, 2015 at 14:12
    So there I was, happily rolling along, writing my class, and using an array ref as my underlying data structure. I then wrote another method:
    sub another_method { my ($self, $magic_val) = @_; $$[MAGIC_VALS]{$magic_val} = 1; }
    and ran it. There were no compile-time or run-time errors, but the logic did not work. Can you spot the problem? That's right: it should be $$self[MAGIC_VALS]{$magic_val} instead! What is this magical @$? Perl does not complain about it and will happily use this array:
    use strict; use warnings; $$[0] = 1; $$[1] = 'dude!'; print "@$", "\n"; # Prints "1 dude!"
    It's not in perlvar... What's up with this variable?
HELP: Problem with PDF
2 direct replies — Read more / Contribute
by cristofayre
on Feb 26, 2015 at 13:24
    I'm tearing my hair out!!

    I generated a PDF with AP12. If I click the file locally, it loads into reader fine. But no matter what I do in perl, it stops loading data at around 4K of a 212K file. (Reading the data sent to page in a text editor, it seems to stop at %%EOF)

    I have tried the usual "While(<xx>)" and "@file=<ZZ>", and even tried the "local $/" to set to undefined size ... but still it stops reading at 4K / %%EOF

    << /Info 4 0 R /Root 1 0 R /Size 815 >> startxref 207080 %%EOF

    So I can't even 'grab' it to send it to a zip file, (everything is code based from generating file to delivery) 'cos I can't read the entire file ... My ultimate aim is to get the file to the browser, or to a save prompt - but can't do either.

    Annoying thing is, I've got it to work before using the "Content-disposition" "content-type: application/pdf" ... but I suspect those files are less than the buffer size (??)

    I even tried it with another file (8MB) Whilst I got the correct number of pages after 20 seconds - and having to switch to an older reader ... there was no text on any page!!

    I can't even email it to people ... cos I can't read the entire file to memory.

    Final try:

    open (QR, "<QR_labels.pdf"); binmode(QR); print "Content-type: application/pdf\n\n"; print 'Content-Disposition', "attachment; filename=Booth_guide.pdf\n\n +"; while(read(QR,$buffer,10000)){ print $buffer; } close(QR);

    Same thing. Only 4K delivered to browser from 212K, and breaking as shown above. This has me beaten! I don't know what to try next

[OT] Stats problem
7 direct replies — Read more / Contribute
by BrowserUk
on Feb 26, 2015 at 04:47

    Any group of 4 bytes in memory can contain any of 2^32 values.

    4GB of memory contains 2^29 (1/2 a billion) 8-byte aligned, 4-byte fields.

    If a runaway loop writing random bytes overruns its buffer and scribbles over one of the 32-bit fields, what are the odds that the 4-bytes, viewed as an unsigned 32-bit integer, would match that fields offset into the 4GB of ram?


    memory:(hex)[00000000........][00000008........][00000010........][000 +00018........]...[12345678........][12345680........]... offset:(dec) 0 8 16 24 + ... 305419896

    The premise is that if free blocks are maintained with their offset into the 4GB block, and a buffer overun occurs, then any block that has been overwritten is easily detectable, with a high degree of certainty. A far higher degree than if the typical fixed known bit-pattern is written there.

    Corruption could write any of the possible values anywhere; and if a fixed bit-pattern is used as the marker, there is, assuming total random corruption, a 1 in 2^32 chance of a specific field being mistaken for the fixed bit pattern. But with potentially 2^29 fields, those odds reduce markedly. I think to 1:(2^32/2^29) = 1 in 8 chance if the whole 4GB were corrupted.

    But the odds that an exact offset value will be written at that exact offset has to be many time higher. But how high?

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
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 drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2015-03-03 01:05 GMT
Find Nodes?
    Voting Booth?

    When putting a smiley right before a closing parenthesis, do you:

    Results (62 votes), past polls