Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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
Time::Piece epoch parsing
2 direct replies — Read more / Contribute
by vsespb
on Jul 01, 2016 at 09:45
    Why if I parse epoch value (%s), it outputs different epoch time? (I have system timezone Europe/Moscow).
    use strict; use warnings; use Time::Piece; my $t = Time::Piece->new(); print Time::Piece->VERSION, "\n"; $t = $t->strptime( $ARGV[0], '%s' ); print "Input $ARGV[0]\n"; print "Output ".$t->epoch(), "\n"; print $t, "\n"; print $t->strftime, "\n";
    1.30 Input 1415998800 Output 1415988000 Fri Nov 14 21:00:00 2014 Fri, 14 Nov 2014 21:00:00 MSK
    Is this a bug in Time::Piece or I misunderstand how it should work?

    also input 1415998800 is "Fri, 14 Nov 2014 21:00:00 GMT", so output "Fri, 14 Nov 2014 21:00:00 MSK" is wrong too.
Tk UCS-2LE:code point too high
2 direct replies — Read more / Contribute
by Anonymous Monk
on Jun 30, 2016 at 15:12

    Dear monks

    I know Tk is not capable of showinf all Unicode charachters. That's okay for me. hat I want to avoid is that my GUI doesn't break if a string contains a unicode character which is not supported. This is a working example:

    use strict; use warnings; use utf8; use Tk; my $mw = MainWindow->new; my $text = "🙂"; $mw->Label(-text => $text)->pack; MainLoop;

    If I would know all unsupported characters I could perform a substitution and delete them... but I don't know them. In $text I'd like to have only supported characters. Any idea?

Gtk2::Dialog window size
2 direct replies — Read more / Contribute
by smittypaddler
on Jun 30, 2016 at 11:49
    How do I set the dialog window size so the entire title is displayed. set_size_request wants pixels, and I don't know how to get $title size in pixels. Here's my code:
    $title=($answer eq $Answer) ? "Yes" : "No, s/b $Answer"; $dialog=Gtk2::Dialog->new($title,$parent, [qw/modal destroy-with-parent/], 'ok' => 'ok', ); $response=$dialog->run; $dialog->destroy;
Are "use constant" constants really inlined?
3 direct replies — Read more / Contribute
by Darkwing
on Jun 30, 2016 at 03:14

    the docu of constant says that constants such as

    use constant DEBUG => 0;
    are inlined. In, the corresponding lines of code are:
    ... if (@_ == 1) { my $scalar = $_[0]; *{"${pkg}::$name"} = sub () { $scalar }; } ...
    But here, a local variable is defined and then used within a sub defined in this block. This should result in a closure rather than in an inlinable subroutine: whenever this block is executed, a new $scalar variable is created and it won't be removed since the subroutine accesses it. So, i understand closures to work. But then the body of the sub isn't a constant value, it is a variable, and so it should not result in an inlinable constant.

    Well, i'm wrong? Are those constants really inlined? Bit if so, why does it work? Why aren't they closures?

Reformat command output inline
3 direct replies — Read more / Contribute
by RenMcCourtey
on Jun 29, 2016 at 08:54

    Hello, I'd like to seek your help on tedious task I'm fighting today. I need to reformat ldapsearch output to acceptable form, and I'd prefer to do that inline with one command. Originally I had complex piped steps of sed and grep and then I realized perl could do that better but I don't know how. Basically I'd like to drop empty or ^dn: lines and merge some other lines together while trimming them. Source looks like this:

    dn: distinguished_name1 cn: common_name1 orclnetdescstring: complex_address_line1 dn: distinguished_name2 cn: common_name2 orclnetdescstring: complex_address_line2 dn: distinguished_name3 cn: common_name3 orclnetdescstring: complex_address_line3
    And the result should go like this:
    common_name1=complex_address_line1 common_name2=complex_address_line2 common_name3=complex_address_line3
    Now I believed this one liner, which I completed with help here would do that: perl -p0e 's/\n^$|\n^dn:*$//mg','s/cn: //g','s/\norclnetdescstring: /=/g' nejms.txt but it doesn't. And as it's getting more complex, I can't see the errors and maybe I'd be better with original sequence of more generic commands, I'm not sure.
Remove SOME new lines
4 direct replies — Read more / Contribute
by RenMcCourtey
on Jun 29, 2016 at 05:06

    Hi, I just hit the issue somewhere I wouldn't expect it. Let's say I want to remove new lines from file, but only some of them. It's obvious to me how to get rid of them all, and it's also what is described everywhere. But while it's easy to replace all Us with Os and all new lines with 'bla', how do I remove new lines from lines starting with U only? I tried following without any success:

    perl -pe 's/\nU/U/g' /tmp/nejms.txt perl -pe 's/^U/U/g' /tmp/nejms.txt
state is the root of evil?
4 direct replies — Read more / Contribute
by basiliscos
on Jun 29, 2016 at 05:04

    Hello dear monks,

    My colleges too-frequently use state feature for aggressive caching. That makes some test problematic, and the functions to be unpure. I'm asking them do not use state, but it seems my arguments aren't strong enough.

    I think I've read some article about state feature misuse, but I can't remember it. If you have a link to some blog, please, share it

    PS. May be I'm not right and state isn't so bad?

    WBR, basiliscos.
RegExp: words excepstions list similar to characters' one.
3 direct replies — Read more / Contribute
by nikolay
on Jun 29, 2016 at 02:23
    Hi. How do i use words excepstions list (like [^qwe|asd]) -- similar to characters' one ([^ghjk])? -- I try to write a script, that would exchange word parts, that contains sign '-', where these parts do not contain (exception) given lists? For example, in the code below
    $z='Web-developer, perl-program, explicit-element, function-call, 2-x +speed.'; print "|$z|\n---\n"; $vrm='(?^ui:(\W)([^(\d|web)]+)-([^(proramm|call)]+)) "1>$1<3>$3< 2> +$2<"'; @bz=split "\t", $vrm; for( $i=0; $i<$#bz; $i+=2 ){ while( $z=~s#$bz[$i]#$bz[$i+1]#g ){ print "|$z|\n"; # <STDIN>; } }

    i want that each part in the 2-word combination, except 'explicit-element', will remain the same, and only 'explicit-element' be turned to 'element explicit' -- because their parts are listed in lists: 'Web-developer' remains the same because its first part 'Web' is in the regular expression, before the sign '-', same for '2-x', whereas 'perl-program' and 'function-call' second parts ('program' and 'call') are listed in the regular expression after sign '-'.

    So, what to do in PERL w/ the exception list for words? Thank you for any advance.

undefined value as filehandle from
2 direct replies — Read more / Contribute
by wanderedinn
on Jun 28, 2016 at 09:46

    I am trying to understand why I am receiving the following error:

    Can't use an undefined value as filehandle reference at /home/geof/httppost/lib/perl5/site_perl/5.8.8/LWP/ line 236

    I have to admit, I don't understand what the code is attempting. Maybe someone can enlighten me? Code with line numbers below:

    233 my $content = ""; 234 open( my $fileb, ">", \$content ); 235 $self->{agent}->setopt( CURLOPT_WRITEDATA, $fileb ); 236 $self->{retcode} = $self->{agent}->perform;

    How is it that the open() above will even work? Can anyone explain to me why I an getting the 'undefined' error? Thanks for any assistance.

randfunc in Config
4 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,
Best option for "switch/case" functionality?
7 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
1 direct reply — Read more / Contribute
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?
New Meditations
RFC: Compiling Gtk+ stack on windows with strawberry perl
1 direct reply — Read more / Contribute
by frazap
on Jun 30, 2016 at 11:02

    I'm on Windows 7 with a perl 5.24 install in C:/strawberry

    I lost so many hours trying to understand why dmake always failed with undefined reference, that I thougt usefull to give a recipe to others when I finaly found a workaround. Here it goes:

    • download the latest bundle for the Gtk+ stack from At the time of this writing (july 2016), the package installed using msys2 crashed on the tests with cairo so I return to the gnome ftp site.
    • unzip it in, say C:\prog, and rename the top folder to gtk+ so that you have a C:\prog\gtk+\lib folder that contains a lot of *.def, *.lib, *.dell, *.dell.a files
    • From the control panel use "System - Advanced system settings - Environment variables" to check or add if missing, the variable PKG_CONFIG_PATH in your user variables. Set it to C:prog\gtk+\lib\pkgconfig;C:\strawberry\c\lib\pkgconfig
    • add C:\prog\gtk+\bin to your PATH environment variable
    • from the cpan shell, download the following perl packages using the get command
      • Cairo
      • Glib
      • Pango
      • Gtk2
      The compilation and installation of these package will have to be done in the above order.
    • Each package is downloaded by cpan in C:\strawberry\cpan\build. To open a command shell in each of these folder, select the folder and while pressing the shift key, show the menu with the mouse right click, and select "open command window here".

      From the cpan shell, the command looks perl_module is another way to open a subshell in the corresponding build/module_folder

    • Edit C:\prog\gtk+\lib\pkgconfig\cairo.pc and replace the line having prefix=/devel/target/whatever with prefix=${pcfiledir}/../..

      Repeat this change for the files:

      gobject-2.0.pc pango.pc gtk+-2.0.pc
    • open a command shell in the Cairo-1.... folder
      • run the commands
        perl INC="-IC:/strawberry/c/include/freetype2 -IC:/prog/gt +k+/include/cairo -IC:/prog/gtk+/include/ -I. -I.\build"
      • Check that there is no "Warning (mostly harmless): No library found for ... ". Changing the prefix line in the pc file of the first library searched by allow perl to find all the external libraries.
      • Run
        dmake dmake test dmake install
    • Open a command shell in the cpan/build/Glib.... folder
      • run
      • perl INC="-IC:\prog\gtk+\include\glib-2.0 -IC:\prog\Gtk+\l +ib\glib-2.0\include -I. -I./build" dmake dmake test dmake install
      • On my pc, dmake test failed (tests in t/a.t all failled) but I installed Glib anyway.
    • Open a command shell in the cpan/build/Pango.... folder
      • run
      • perl INC="-IC:\prog\gtk+\include -IC:/prog/gtk+/include/f +reetype2 -IC:/prog/Gtk+/include/cairo -IC:\prog\Gtk+\include\Pango-1. +0 -IC:\prog\Gtk+\include\glib-2.0 -IC:\prog\Gtk+\lib\glib-2.0\include + -I. -I./build -IC:\strawberry\perl\site\lib\Glib\Install -IC:\strawb +erry\perl\site\lib\Cairo\Install" dmake dmake test dmake install
    • Open a command shell in the cpan/build/Gtk2-.... folder
      • run
      • perl INC="-IC:\strawberry\perl\site\lib\Glib\Install -IC:\ +strawberry\perl\site\lib\Pango\Install -IC:\strawberry\perl\site\lib\ +Cairo\Install -IC:\prog\gtk+\include -IC:\prog\Gtk+\include\glib-2.0 +-IC:\prog\gtk+\include\pango-1.0 -IC:\prog\Gtk+\include\cairo -IC:\pr +og\Gtk+\include\atk-1.0 -IC:\prog\gtk+\include\gdk-pixbuf-2.0 -IC:\pr +og\gtk+\include\gtk-2.0 -IC:\prog\gtk+\include\freetype2 -IC:\prog\Gt +k+\lib\glib-2.0\include -IC:\prog\Gtk+\lib\gtk-2.0\include -I. -I./bu +ild" dmake dmake test dmake install


    The command given above for perl Makefile.PL has been used for the gtk+-bundle_2.24.10

    A newer version could contains new libraries and this would cause dmake to fail with filex.h not found

    In that case, you have to add the path to the missing file with -Ic:/this/is/where/thatmissing/file/is in the INC arg given to Makefile.PL.

    Perl modules already installed have this header file in


    And external libraries to uninstalled perl modules have their header files somewhere in

    C:\prog\gtk+\include C:\prog\Gtk+\include\xxx C:\prog\Gtk+\lib\xxx\include
    So rerun
    perl Makefile.PL INC="changed to include the new path" dmake

    until dmake gives no error.

    Now if you change the path where your gtk+ libraries have been unzipped (or if you remove C:/prog/gtk+/bin from your PATH environment variable, your perl scripts using Gtk2 will crash.

    To gain independance from C:/prog/gtk+/bin, you need to copy some dll from this folder to your perl tree directory. This small script does this (the dll list hold for gtk+-bundle_2.24.10):

    use strict; use warnings; use File::Copy; my $from= "C:/prog/gtk+/bin/"; my %fm=('C:/strawberry/perl/site/lib/auto/Glib/' => [qw( libglib-2.0-0.dll intl.dll libgthread-2.0-0.dll libgobject-2.0-0.dll )], 'C:/strawberry/perl/site/lib/auto/Cairo/' => [ qw( libcairo-2.dll libexpat-1.dll freetype6.dll libpng14-14.dll libfontconfig-1.dll zlib1.dll )], 'C:/strawberry/perl/site/lib/auto/Pango/' => [qw( libgmodule-2.0-0.dll libpango-1.0-0.dll libpangocairo-1.0-0.dll libpangoft2-1.0-0.dll libpangowin32-1.0-0.dll )], 'C:/strawberry/perl/site/lib/auto/Gtk2/' => [ qw( libgtk-win32-2.0-0.dll libgio-2.0-0.dll libgdk_pixbuf-2.0-0.dll libatk-1.0-0.dll libgdk-win32-2.0-0.dll )]); foreach my $dest (keys %fm){ my $files = $fm{$dest}; foreach my $file (@$files){ print "will copy $from$file to $dest/$file\n"; copy("$from$file", "$dest/$file") or die("failed... : $!"); } }

    HTH !


PWM on raspberrypi with bcm2835
1 direct reply — Read more / Contribute
by martell
on Jun 25, 2016 at 13:14

    Dear Monks

    I thought I share this small Perl program demonstrating the possibility to do PWM (Pulse Wide Modulation) on Raspberry Pi in order to control the intensity of a led. This is a common introduction example to try for people learning about electronics on Raspberry Pi. Well documented examples for python exists, but not for Perl.

    The reason I share this here, is that the current CPAN module Device::BCM2835 doesn't define the necessary function to do this (that I see). So it took me some tinkering before I could control the PWM from Perl. Novice users of Perl may find this example code the thing they need.

    The solution I apply is the use of the Inline::C module to call some small wrapper functions I wrote in C around an existing C example I found on the internet. They call in turn the bcm2835 functions needed. This is tested on a raspberry pi B, revision 2 unit, using a single led and a single resistor.

    Be aware that on later versions of Raspberry Pi the GPIO pin layout is different and you have to use other constants in the functions calls in the C code. So YMMV, but enough documentation exists on the internet to figure out the correct values.

    This code assumes you have installed the Inline::C module, Time::HiRes module, and the bcm2835 C library on your device. See Device::BCM2835 module for the appropriate links to the C library. It is not installed by default. Run as root on Raspberry Pi because you need to have access to some low level functions.

    Today I'm not proficient in XSLoader to update the Device::BCM2835 module itself, but if I have some spare time, I will look into it. Would be a nice additional skill to master.

    Any thoughts or comments appreciated.


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 examining the Monastery: (12)
As of 2016-07-01 15:31 GMT
Find Nodes?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?

    Results (7 votes). Check out past polls.