Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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
XML::Twig - '#CDATA' attribute in hashref disappears after first call to XML::Twig::Elt->new()
2 direct replies — Read more / Contribute
by ateague
on Jul 20, 2018 at 17:56

    Good afternoon!

    I am experiencing a rather unexpected issue creating new elements with XML::Twig's XML::Twig::Elt->new() method where the '#CDATA' key in my attribute hashref gets undefined after passing it to new().

    e.g.:

    my $attributes = $sibling_element->atts(); $attributes->{'#CDATA'} = 1; # '#CDATA' attribute is defined here my $elt1 = XML::Twig::Elt->new('present', $attributes, 'This is in CDA +TA'); # '#CDATA' attribute is undefined here my $elt2 = XML::Twig::Elt->new('missing', $attributes, 'This is not in + CDATA');

    Is this supposed to happen? I did not see anything mentioning this in the CPAN docs...

    Thank you for your time.

    Perl info:

    perl -v This is perl 5, version 26, subversion 0 (v5.26.0) built for MSWin32-x +64-multi-thread
    XML::Twig info: 3.52

PL_strtab/SHAREKEYS and copy-on-write leak
4 direct replies — Read more / Contribute
by sezal
on Jul 20, 2018 at 17:11

    Perl internally uses dedicated hash PL_strtab as shared storage for hash's keys, but in fork environment like apache/mod_perl this creates a big issue. Best practice says to preload modules in parent process, but nobody says it's eventually allocates memory for PL_strtab and these pages of memory tend to be implicitly modified in child processes. There are seems to be 2 major reasons of modification:

    Reason 1: reallocation (hsplit()) may happen when PL_strtab growths in child process.

    Reason 2: REFCNT every time new reference created.

    Example below shows 16MB copy-on-write leak in attempt to use hash. Attempts to recompile perl with -DNODEFAULT_SHAREKEYS fails (https://rt.perl.org/SelfService/Display.html?id=133384). I was able to get access to PL_strtab via XS module and ideally I'm looking for a way to downgrade all hashes created in parent to keep hash keys within a hash (HE object) rather than PL_strtab, i.e. turn off SHAREKEYS flag. This should allow to shrink PL_strtab to minimum possible size. Ideally it should have 0 keys in parent. Please let me know you think it's theoretically possible via XS.

    #!/usr/bin/env perl use strict; use warnings; use Linux::Smaps; $SIG{CHLD} = sub { waitpid(-1, 1) }; # comment this block { my %h; # pre-growth PL_strtab hash, kind of: keys %$PL_strtab = 2_000_000 +; foreach my $x (1 .. 2_000_000) { $h{$x} = undef; } } my $pid = fork // die "Cannot fork: $!"; unless ($pid) { # child my $s = Linux::Smaps->new($$)->all; my $before = $s->shared_clean + $s->shared_dirty; { my %h; foreach my $x (1 .. 2_000_000) { $h{$x} = undef; } } my $s2 = Linux::Smaps->new($$)->all; my $after = $s2->shared_clean + $s2->shared_dirty; warn 'COPY-ON-WRITE: ' . ($before - $after) . ' KB'; exit 0; } sleep 1000; print "DONE\n";
Efficient enumeration of pandigital fractions
5 direct replies — Read more / Contribute
by kikuchiyo
on Jul 20, 2018 at 16:51

    The fraction 6952 / 1738 has a curious property: each non-zero decimal digit appears exactly once in the expression, and the result of the divison happens to be the missing digit, 4.

    Are there, by any chance, other fractions that share this property? It is fairly simple to devise a semi-brute force solution to answer this question:

    restate the problem as abcd = efgh * i, generate all 5-element variations (k-permutations) of the set of digits 1..9, perform the multiplication and check that the result consists only of digits not in the sequence.

    Here is a somewhat optimized implementation:

    For base 10 this runs quickly enough to find that there is one additional solution. But for the obvious and straightforward generalization to higher bases this brute force solution is not going to cut it.

    Tinkering with the innards of the loop or using a different permutation engine might give us a speedup factor of two, while rewriting the whole thing in C might give us two orders of magnitudes. But we'd be still generating all permutations, and the number of those grows relentlessly as the base increases ((b - 1)! / (b/2 - 1)!):

    6 60
    8 840
    10 15120
    12 332640
    14 8648640
    16 259459200
    18 8821612800
    20 335221286400
    

    On my machine the program above needed 6 seconds to find all base-14 solutions, more than 3 minutes for base-16, and I dared not run it with higher bases.

    However, the number of actual solutions is much smaller:

    6 	1
    8 	2
    10 	2
    12 	18
    14 	136
    16 	188
    

    which suggests that there may be better, more efficient approaches that don't have to trudge through a lot of useless permutations to find the solutions. However, so far I haven't been able to find one.

    Any thoughts?

Can you limit the scope of require?
3 direct replies — Read more / Contribute
by taylorK
on Jul 20, 2018 at 14:58

    Hi Monks,

    First post so I apologize if I miss something or if there are small errors in the example I provided, I am just using it to explain my issue. I have run into this situation a few times now and have not been able to track down an answer. Let me be clear that this is for a work application so I cannot simply change the current setup of files etc...

    Background: We use multiple .conf files to house "parsers" for data files. Each of these .conf files has a sub that contains a header map in the form of an array. These .conf files are not packages.

    Question: I would like to write a program that will get that header map from one of these .conf files then get it from a different one as well but only for the purposes of a very simple operation. My current method is to use a require (as seen below) and grab the sub. The problem is when I use require a second time to get the second .conf file it completely overwrites the first require. Can I call this second one and have the scope of it limited to a particular subroutine or variable?

    Example:

    if (-e "myfilepath/test1.conf") { require "myfilepath/test1.conf" or die("myfilepath/test1.conf - $! +\n"); } else { die("Missing provider config file: myfilepath/test1.conf\n"); } $header_map = &get_config; # grab a file to parse while (defined(my $line = <FILE>)) { my $second_file = &compare(); # split $line into the header if ($first_file{test} =~ /$seconf_file{test}/i) { # etc.... } } sub compare { if (-e "myfilepath/test2.conf") { require "myfilepath/test2.conf" or die("myfilepath/test2.conf +- $!\n"); } else { die("Missing provider config file: myfilepath/test2.conf\n"); } $header_map_2 = &get_config; # grab a file to parse while (defined(my $line = <FILE>)) { # split $line into the header } return "$parsed_line"; }

    Thanks!

Constants lists in array slices
4 direct replies — Read more / Contribute
by LanX
on Jul 20, 2018 at 07:40
    The following code is surprising me in two ways:

    use strict; use warnings; use feature 'say'; use constant FL => 17,3,16; use constant FL2 => 22,4,13; my @row= "a".."z"; my @a = (" ") x 26; @a[17,3,16] = @row[22,4,13]; say "@a"; @a = (" ") x 26; @a[FL()] = @row[FL2]; say "@a";

    It seems like constant folding isn't happening at all!!!

    • I get a warning for FL2
      Scalar value @row[FL2] better written as $row[FL2] (ignoring that its a constant _list_)
    • Deparsing the code shows that FL and FL2 are called as functions
      @a[FL()] = @row[FL2()];
    Am I missing something?

    (This is perl 5, version 16,)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Writing IBM Mainframe floating point
3 direct replies — Read more / Contribute
by caden_varn
on Jul 20, 2018 at 06:19
    I need to create a file including single precision floating point fields in the IBM mainframe format (specifically to be read by the SAS s370frb4. informat). I am on an HP-UX 11 box which evidently uses a different floating point - when I use pack's 'f' option the value changes when read in by the SAS program on the other end. Is there an option or module that can do this easily? Any help greatefully received...
Perl dictionary
5 direct replies — Read more / Contribute
by dideod.yang
on Jul 19, 2018 at 20:03
    Hi monks. I am perl & python lover.. recently I made many scripy using perl.. So I want to know many functions as much as possible. Cpan give me many information about modules.. but function is not.. Do you monks have nice website? or manual? about perl function??
site_perl files
3 direct replies — Read more / Contribute
by yaklichk0719
on Jul 19, 2018 at 17:00
    Can somebody please explain me what should be in the folder perl5.10/lib/site_perl/? I have perl 5.10.1 under centOS 6.6 and it does not have site_perl. Another package that I have been been trying to run requires the presence of this path perl5.10/lib/site_perl. Where can I download files for this site_perl? Please help me to figure this out. I really appreciate it.
I need help installing DBD:Oracle and DBD
1 direct reply — Read more / Contribute
by gandolf989
on Jul 19, 2018 at 09:33

    I am trying to install DBD:Oracle and DBD so that I can write a script to clean up some attachments with our primary database, and change the pointers to show that the attachments are gone. But I ran into an issue with the above packages. I tried the initial install using cpan. but I did not have the environment variables set, so it did not compile correctly. I would like to remove and reinstall the packages, or mostly just get them working. I do have an 11.2.0.4 Oracle client on the server. But I will be connecting to another server for the database.

    Can someone point me in the right direction. The server is Redhat 6.8. Thanks

    $:~/ ./runMyPerlScript.pl Can't load '/usr/local/lib/perl5/site_perl/5.28.0/x86_64-linux/auto/DB +I/DBI.so' for module DBI: /usr/local/lib/perl5/site_perl/5.28.0/x86_6 +4-linux/auto/DBI/DBI.so: undefined symbol: PL_sv_immortals at /usr/li +b64/perl5/DynaLoader.pm line 200. at /usr/local/lib/perl5/site_perl/5.28.0/x86_64-linux/DBI.pm line 284 +. BEGIN failed--compilation aborted at /usr/local/lib/perl5/site_perl/5. +28.0/x86_64-linux/DBI.pm line 284. Compilation failed in require at /usr/local/lib/perl5/site_perl/5.28.0 +/x86_64-linux/DBD/Oracle.pm line 20. BEGIN failed--compilation aborted at /usr/local/lib/perl5/site_perl/5. +28.0/x86_64-linux/DBD/Oracle.pm line 20. Compilation failed in require at /home/oracle/scripts/purge_proddoc_sc +ript/purge_proddoc_script.pl line 16. BEGIN failed--compilation aborted at /home/oracle/scripts/purge_proddo +c_script/purge_proddoc_script.pl line 16 (#1) (F) The module you tried to load failed to load a dynamic extensio +n. This may either mean that you upgraded your version of perl to one that + is incompatible with your old dynamic extensions (which is known to h +appen between major versions of perl), or (more likely) that your dynami +c extension was built against an older version of the library that i +s installed on your system. You may need to rebuild your old dynamic extensions. Uncaught exception from user code: Can't load '/usr/local/lib/perl5/site_perl/5.28.0/x86_64-linux +/auto/DBI/DBI.so' for module DBI: /usr/local/lib/perl5/site_perl/5.28 +.0/x86_64-linux/auto/DBI/DBI.so: undefined symbol: PL_sv_immortals at + /usr/lib64/perl5/DynaLoader.pm line 200. at /usr/local/lib/perl5/site_perl/5.28.0/x86_64-linux/DBI.pm line 284 +. BEGIN failed--compilation aborted at /usr/local/lib/perl5/site_perl/5. +28.0/x86_64-linux/DBI.pm line 284. Compilation failed in require at /usr/local/lib/perl5/site_perl/5.28.0 +/x86_64-linux/DBD/Oracle.pm line 20. BEGIN failed--compilation aborted at /usr/local/lib/perl5/site_perl/5. +28.0/x86_64-linux/DBD/Oracle.pm line 20. Compilation failed in require at /home/oracle/scripts/purge_proddoc_sc +ript/purge_proddoc_script.pl line 16. BEGIN failed--compilation aborted at /home/oracle/scripts/purge_proddo +c_script/purge_proddoc_script.pl line 16. at /home/oracle/scripts/purge_proddoc_script/purge_proddoc_script.pl +line 16. $:~/scripts/purge_proddoc_script > cat /etc/redhat-release Red Hat Enterprise Linux Server release 6.8 (Santiago)
    #!/usr/bin/perl use strict; use warnings; use diagnostics; package purge_proddoc_script; use Getopt::Std; # use Sys::Hostname; use POSIX qw(setuid setgid); use English; use Carp; use Cwd 'abs_path'; use DBD::Oracle qw(:ora_session_modes); use DBI; use File::Basename; use IO::Handle; use Log::Log4perl qw( :easy ); use Text::Diff; local $SIG{__WARN__} = \&Carp::cluck; $SIG{__DIE__} = sub { # return if $^S; # ignore die in an eval block # Get the actual caller for the "die" and not the wrapper local $Log::Log4perl::caller_depth; $Log::Log4perl::caller_depth++; LOGDIE($_[0]); }; # possible log values: DEBUG, INFO, WARN, ERROR, FATAL my $debug_level="INFO"; # my $error_mail_list='bbyrd@aires.com, ASayed@aires.com, vkamins@aire +s.com, RKatru@aires.com'; my $error_mail_list='bbyrd@aires.com'; # Unbuffer STDOUT and STDERR select STDERR; $| = 1; select STDOUT; $| = 1;
Migrating from Windows Server 2003
6 direct replies — Read more / Contribute
by cochise33
on Jul 19, 2018 at 08:26
    I'm running Perl on a Windows 2003 Server. The time has come to finally move to a more current version of Windows and I was wondering if you guys have any suggestions or warnings. Am I going to have any problems with Windows Server 12 or 16?

    Though I've used Perl for 20 years, I've only installed/reinstalled it or any packages like GD or some .pdf scripts a couple of times, so from that angle I'm a beginner.

    We also use MDaemon for the email part of what we do.

    Thanks for any help,

    Steve
About personal function
3 direct replies — Read more / Contribute
by dideod.yang
on Jul 19, 2018 at 00:32
    Hi monks. I want to achieve using personal function with many arrays. Below script, I show you sample. According script, only print A... I want to print all of values(A B C) in @x. How can I achieve my personal function.
    my (@x,@y); @x =( "A" "B" "C"); @y =( "C" "D" "F"); &test(@x,@y): sub &test{ @test = @_[0]; print "@test\n"; }
Abuse of "or next" in expressions and "next" that returns value
5 direct replies — Read more / Contribute
by vr
on Jul 18, 2018 at 15:28

    Today I wrote a script, in which I preferred some logic expressed as 2nd fragment rather than the 1st:

    for ( $x .. $y ) { # some loop # ... skipped ... # fragment #1: next unless exists $gs_val-> { HT }; my $ht = $doc-> getValue( $gs_val-> { HT }); next unless exists $ht-> { Default }; my $df = $doc-> getValue( $ht-> { Default }); next unless exists $df-> { TransferFunction }; my $tf = $doc-> getValue( $df-> { TransferFunction }); next unless $tf eq 'Default; # ... skipped ... # fragment #2: next unless $doc-> getValue( $doc-> getValue( $doc-> getValue( $gs_val-> { HT } || next ) -> { Default } || next ) -> { TransferFunction } || next ) eq 'Default'; # ... skipped ... }

    As you see, I tried to improve the #1 with a few blank lines, but, to me, the #2 is more readable -- now, and I hope it will remain so in a year, when I have to try hard to remember what it all was about. Perhaps not everyone will agree.

    However, while experimenting and testing to see if such constructs are OK, I was surprised to find that Perl prefers even more unexpected form:

    >perl -we "for(\0,0){print${$_||next}}" 0 >perl -MO=Deparse -we "for(\0,0){print${$_||next}}" BEGIN { $^W = 1; } foreach $_ (\0, 0) { print ${next unless $_;}; } -e syntax OK >perl -we "for(\0,0){print${next unless $_}}" 0 >

    Looks like if "or next" is part of block that is supposed to return value, then Perl replaces it with "next unless" syntax. What's totally unexpected is that this "next unless" thing is actually valid and does return value.

    I can only guess that everything works the other way, in reality: "next unless" is optimized to "or next", therefore it returns value, and that for some strange reason B::Deparse shows "||next" as "next unless". That is only way this can be sane... Or is it not so? :)

New Meditations
use Memoize;
2 direct replies — Read more / Contribute
by Anonymous Monk
on Jul 16, 2018 at 15:18
    I was porting a script to a module and noticed it kept getting slower. The script could initialize its expensive data structure once at the top and be done with it, but in order to encapsulate, the module was calling the function several times. I remembered the core module Memoize and added one line to the top of the program and now it runs fast again, 4x faster than without Memoize!
    
    use Memoize; memoize('some_sub');
    
    
    Only 1.5 seconds to start a program that was taking 6 seconds!
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2018-07-21 21:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (450 votes). Check out past polls.

    Notices?