Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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
randfunc in Config
2 direct replies — Read more / Contribute
by choroba
on Jun 27, 2016 at 05:19
    Dear Monks,

    I got a strange tester failure report for Syntax::Construct. It seems there's a machine with Perl 5.20.2 whose $Config{randfunc} returns drand48 , but my test expects Perl_drand48 . See the relevant perl5200delta:

    > Perl now uses its own internal drand48() implementation on all platforms.

    Corion pointed me to a search for randfunc where the only possibly relevant line is the one from


    I don't understand what the file is used for, so this might be totally unrelated.

    So, my questions are:

    1. Is it OK to have Perl 5.20+ with $Config{randfunc} returning drand48 without the Perl_ prefix? If so, I can check just /drand48/ or use index in the test.
    2. Is there some other method besides using %Config to check that Perl uses its own drand48 function?

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Assign a hash to an array
5 direct replies — Read more / Contribute
by ravi45722
on Jun 27, 2016 at 05:05

    Here I am assigning(push) a hash to an array of hashes. But when I print hash Its perfect. But when I push that into an array its changing. Here is my code. I also tried with escape('\') character.

    $string = "column08=Submit & column10=Delivered & column09=Something" my @matches = grep defined, split / ([|&]) /, $string; $i = 1; foreach my $one (@matches) { if (($one =~ tr/=//) == 1) { my ($key,$value)=(split /=/,$one); %hash = (); $hash{'term'}{$key} = $value; push (@return_array,%hash); } else { push (@symbol_array,$one); } } print Dumper \%hash; print Dumper \@return_array;

    Expected Output:

    $VAR1 = [ 'term'=> { 'column08' => 'Submit' }, 'term'=> { 'column10' => 'Delivered' }, 'term'=> { 'column09' => 'Something' } ];
    Thanks in Advance
Best option for "switch/case" functionality?
2 direct replies — Read more / Contribute
by cheselton
on Jun 27, 2016 at 03:59
    I've been using the use feature qw( switch ) feature, pretty much since in came out in Perl 5.10. As of v5.18 or so, someone with enough clout, decided that the "smartmatch" feature was experimental, and this given/when construct appears to fall into the "smartmatch" category. Normally this isn't an issue, as I'm usually writing one-off scripts that tackle a specific task and/or run on identical platforms -- so the same Perl version all around. I'm currently working on a project that crosses different linux distributions, and therefore different Perl versions. Some (one, actually) before 5.18 and some (most) after (5.20 or so). The older ones run fine; the newer ones complain that the feature is "experimental". One can disable the warnings on the newer perls by using... no warnings "smartmatch::expierimental;...but the older perls don't recognize the pragma and fail to run. So what's the best way to use this case/switch functionality across perl versions without editing my code every time? (Note: I've tried the Switch module, but it doesn't seem to handle regular expressions very well -- especially with nested blocks.) Any help appreciated. TIA
Naming question
No replies — Read more | Post response
by QSeep
on Jun 27, 2016 at 01:42
    Moose's type-checking philosophy seems to be that we should specify type constraints at the level of an attribute, and check types at the point when an attribute is set. It does not normally check other method parameters. It can, though, e.g. with MooseX::Params::Validate or Moops. This can create performance problems with type-checking aggregates. If you have a large array, and you set a ArrayRefInt to point to it, the constraint must now check every element in the array. What if we had a class that contained an arrayref, and had a type constraint, and methods on that class that are used to add elements would check the type constraint for the added elements? Then when we passed one of these objects in as a Moose attribute, the check for the types of all elements would be constant time.
    package TypedArray; use Moose; has type => ( is => 'ro', isa => 'Moose::Meta::TypeConstraint', required => 1 ); has elems => ( is => 'ro', isa => ArrayRef, default => sub { [] }, ); sub BUILD { my $self = shift; $self->type->assert_valid($_) for @{$self->elems}; } sub elems_push { my $self = shift; $self->type->assert_valid($_) for @_; push @{$self->elems}, @_; } ... more methods and operator overloads ...
    Using overloaded operators, we could make them work much like built-in arrays. To create a new one, you would write, e.g.:
    TypedArray->new(type => Int, elems => [4, 8, -7])
    It would be nice to have a shorthand syntax to create them. We could use BUILDARGS to simplify it to:
    TypedArray->new(Int, 4, 8, -7)
    but it would be nice to get rid of even the `new`. We could make a function, e.g.:
    typedArray Int, 4, 8, -7
    Then we could define a parameterizable type constraint called TypedArrayOf:
    subtype TypedArrayOf as Parameterizable['TypedArray', 'Moose::Meta::TypeConstraint' +], where { my ($typed_array, $type) = @_; $typed_array->type->is_a_type_of($type) }, message { my ($typed_array, $type) = @_; return "expected TypedArrayOf[$type], found TypedArrayOf[@ +{[$typed_array->type]}]" };
    Unfortunately, we could not use the class name for the type constraint, because this is not a class constraint, and we do not want to conflict with the class constraint. So I chose a compromise by appending the word 'Of' to the name. Instead of having a single TypedArray class, another option would be to create a new class on the fly for each possible type parameter. We could use a parameterized role to build them, using Class::MOP to generate the new classes on demand. Then TypedArray would become a function, taking a single parameter that is an arrayref with the contained type. E.g.,
    would look up the Int type in a global cache, and if not found, would generate a new TypedArray class, parameterized on Int. That class would constrain all elements to be Ints. This could get expensive if we end up creating a lot of classes. What do folks think of the naming here? Can we be more succinct? More consistent? How can we avoid naming conflicts between 1) the class without a type parameter, 2) the class with a type parameter, 3) a constructor function without a type paramter, 4) a constructor function with a type parameter, 5) the type constraint without a parameter, 6) the type constraint with a parameter. What is the most "Moosey" approach?
undefined symbol: inflate
3 direct replies — Read more / Contribute
by geoffleach
on Jun 26, 2016 at 22:39
    A reference to "inflate" is being inserted into my module's .so. Any ideas on from whence it might be coming? Environment is linux and I'm not using zlib, MakeMaker is up-to-date and This is perl 5, version 22, subversion 2 (v5.22.2).

    Any suggestions?

Surely @a is local to sub load?
1 direct reply — Read more / Contribute
by philiprbrenan
on Jun 26, 2016 at 18:57
    #!/usr/bin/perl use v5.22; use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump); my @n = 1..2; sub load {return my @a unless @n; push @a, @n; @a } say "$_ ", dump([load()]) for 1..10; =pod 1 [1, 2] 2 [1, 2, 1, 2] 3 [1, 2, 1, 2, 1, 2] 4 [1, 2, 1, 2, 1, 2, 1, 2] 5 [1, 2, 1, 2, 1, 2, 1, 2, 1, 2] 6 [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2] 7 [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2] 8 [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2] 9 [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2] 10 [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2] This is perl 5, version 22, subversion 1 (v5.22.1) built for x86_64-l +inux-gnu-thread-multi =cut

    Surely @a is local to sub load?

Problem with SSL connection to PayPal
1 direct reply — Read more / Contribute
by Anonymous Monk
on Jun 26, 2016 at 11:48

    I been trying to update my perl cgi script to use SSL when responding to an Instant Payment Notification from PayPal. They provide a test url ( to test the basic connection to their servers.

    Using LWP::UserAgent, I got the following message:

    LWP::Protocol::https::Socket: SSL connect attempt failed with unknown error SSL wants a read first at /System/Library/Perl/Extras/5.18/LWP/Protocol/ line 51.

    I can easily connect to the host using curl and php, so it isn't a general machine configuration, but it is something specific to perl. Here is the code:

    #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; my $ua = LWP::UserAgent->new(); $ua->ssl_opts(verify_hostname => 1); $ua->ssl_opts(SSL_ca_file => './MozillaCAFile.crt'); my $host = ''; my $req = HTTP::Request->new(GET => "$host"); my $res = $ua->get($host); if (not $res->is_success) { print $res->as_string . "\n"; } else { print "Success\n"; print $res->decoded_content; }
Can't Insert Data To Multiple Table With DBI::Mysql
3 direct replies — Read more / Contribute
by pakeidoprek
on Jun 26, 2016 at 09:59

    Get Parameter By Form & then insert into tbl

    if ( param('Submit') ) { # get data from parameter to database my @params = param(); my $full_name = param ('full_name'); my $pob = param ('pob'); my $dob = param ('dob'); my $gender = param ('gender'); my $address = param ('address'); my $email = param ('email'); my $phone = param ('phone'); my $username = param ('username'); my $password = param ('password'); my $high_school = param ('high_school'); my $school_dep = param ('school_dep'); my $year_in = param ('years'); # data to tbl:students # ---------------------------------------------------------------- my $students = $dsn->prepare("INSERT INTO students (username, password) values (?,?) "); $students->execute($username,$password) or die $DBI::errstr; $students->finish(); # data to tbl:students_bio # ---------------------------------------------------------------- my $students_bio = $dsn->prepare("INSERT INTO students_bio ( username, full_name, pob, dob, gender, addr, email, phone, high_school, school_dep, years ) values (?,?,?,?,?,?,?,?,?,?,?) "); $students_bio->execute($username,$full_name,$pob,$dob, $gender,$address,$email,$phone, $high_school,$school_dep,$year_in) or die $DBI::errstr; $students_bio->finish(); # data to tbl:students_parent # ---------------------------------------------------------------- my $students_parent = $dsn->prepare("INSERT INTO students_parent (username) values (?) "); $students_parent->execute($username) or die $DBI::errstr; $students_parent->finish(); };

    I don't have problem to insert data to 1 tables with this method. but when i used this methods to insert data to multiple or more tables, the data just insert into 1st table and no activity with the 2nd $ 3rd table

    I'm so sorry if my english so bad. Thank's for the attention, Cheers for u, i'm from indonesia.

Array skips number if checked for defined.
3 direct replies — Read more / Contribute
by $h4X4_|=73}{
on Jun 26, 2016 at 09:57

    I have some thing happening in a package that seems strange to me. Lets say I have a blank array @array = (); when I check if the array{hash} is defined that should not be. Then push to that array and check the number again the array shifts to the next number.
    I hope the code explains it better...

    #!/usr/bin/perl use warnings; use strict; # add to @Foo::THING push(@Foo::THING, { 'check' => '1',} ); # Should say "Some Thing" Foo::check_THING(0) ? print "Some Thing\n" : print "No Thing\n"; # Should say "No Thing" Foo::check_THING(1) ? print "Some Thing\n" : print "No Thing\n"; # add to @Foo::THING push(@Foo::THING, { 'check' => '1',} ); # Check if it was added? Foo::check_THING(1) ? print "Some Thing\n" # I think it should be this : print "No Thing\n"; # But we get this # Why is it 2? Foo::check_THING(2) ? print "Some Thing\n" # WTF? : print "No Thing\n"; package Foo; our @THING = (); sub check_THING { my $id = shift; defined $THING[$id]{check} ? return 1 : return 0; }

Only showing part of POD when not rendered as HTML
1 direct reply — Read more / Contribute
by perlancar
on Jun 26, 2016 at 06:51

    I have some text tables on my module POD, e.g. Bencher::Scenario::Accessors::Get.

    I'm thinking of showing a sortable HTML table (which contains some JavaScript) instead when the POD is rendered to HTML as in sites like This will be done using =begin HTML ... =end HTML POD commands.

    I'd also like that the text table is not displayed on HTML output, so the user is not seeing two tables (one text and one HTML). Any idea? Two of mine so far:

    1) in the =begin HTML ... =end HTML snippet, hide the text table DOM element using some CSS or JS. But this will depend on how the POD is rendered to HTML, i.e. I can make it work on but it might not work on other sites like say which might render the HTML differently.

    2) put the text table in =begin man ... =end man *and* =begin text ... =end text. But this feels dirty. What about other output formats?

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 meditating upon the Monastery: (3)
    As of 2016-06-27 11:08 GMT
    Find Nodes?
      Voting Booth?
      My preferred method of making French fries (chips) is in a ...

      Results (337 votes). Check out past polls.