Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
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
[CLOSED] Dereference hash into hashes by key
3 direct replies — Read more / Contribute
by mlodato
on May 22, 2018 at 12:04
    O Wise Ones,
    Is there a one-line way to do something along the lines of:
    my $hash = { key1 => 'value1', key2 => 'value2', }; my ($hash1, $hash2) = _insert_perl_magic_;
    Such that then $hash1 becomes {key1 => 'value1'} and $hash2 becomes {key2 => 'value2'}?

    The closest I've gotten is:

    my @hashes = map { {$_ => $hash->{$_}} } qw(key1 key2); my ($hash1, $hash2) = @hashes;
    But it doesn't seem to work in one line (probably because of some lazy generation or something (I confess I don't truly understand how map works)

-T changed behavior
4 direct replies — Read more / Contribute
by tothestars
on May 22, 2018 at 02:06

    Hi, I am not a Perl programmer but I have a Perl issue that needs to be resolved and will greatly appreciate your help.

    Here’s the case:

    perl -le 'print "Version $] : ", -T "spk_pre_180208_180715_180419_bpo1 +2_p.bsp" ? "text" : "not text"' Linux: Version 5.010001 : not text Version 5.014002 : not text Version 5.016003 : not text Version 5.026001 : tex

    Somewhere (between version 5.20.3 and 5.22.4) there is a Perl patch causing a change in -T's behavior. A file that was previously evaluated as Binary is now evaluated as text.

    Any insight?

    Thanks so much for your help!

    2018-05-22 Athanasius added code and paragraph tags

XMLOut in XML::Simple returning unwanted xml format
3 direct replies — Read more / Contribute
by perl_help27
on May 21, 2018 at 10:10
    Hello Monks, I kind of need your help. I have the following code:
    use XML::Simple; use Data::Dumper; my $hash= { 'root' => { 'item1' => { 'item1a' => 'n1', 'item1b'=>'jh' } } }; my $xs = new XML::Simple; my $xml = $xs->XMLout($hash, NoAttr => 1, RootName=>undef,); print Dumper $xml;
    This is outputting :
    <root> <name>item1</name> <item1a>n1</item1a> <item1b>jh</item1b> </root>
    But I want the xml to be ALL nested like that:
    <root> <item1> <item1a>n1</item1a> <item1b>jh</item1b> </item1> </root>
    What do you think I should do?? Please help I am stuck :S Thanks
perltidy and UTF-8 BOM
2 direct replies — Read more / Contribute
by morelenmir
on May 21, 2018 at 00:07

    As part of the shift to working with a plain text editor I would like to employ the 'perltidy' batch file and script to ensure my code is readable and consistent in presentation. However I have encountered a problem in doing so.

    I use UTF-8 encoding with a matching BOM for all my Perl sources. Unfortunately when I run 'perltidy' on a file with this layout I recieve the following error:

    Perltidy version is 20180220 1: unexpected character decimal 239 (ï) in script 1: unexpected character decimal 187 (») in script 1: unexpected character decimal 191 (¿) in script 1: Giving up after error

    If I remove the BOM, but otherwise keep the UTF-8 encoding then 'perltidy' works as expected. Alternately if I set the -UTF8 switch then I get both the error message and the entire source file becomes double-spaced on new lines. Is there any way around this problem?

    "Aure Entuluva!" - Hurin Thalion at the Nirnaeth Arnoediad.
Problems starting the debugger
1 direct reply — Read more / Contribute
by morelenmir
on May 19, 2018 at 21:58

    Hey Guys!

    I am in the process of getting back in to Perl after being away from programming as a whole for quite a while. I thought this might be a good time to learn how to use the built-in debugger. Previously I ran 'EPIC' inside 'Eclipse', but ended up very much disliking that IDE. Therefore this time around I intend to write programmes in a text editor called 'EditPad Pro' and then employ the native Perl debugger as necessary. Unfortunately I have run in to some problems straight out of the gate,

    As a test I started with the simplest of all one-line programmes, saved as 'debug_test.pl':

    say "Hello";

    Next, at the console window I used the command:

    perl -d debug_test.pl

    This is the output I received from Perl:

    Loading DB routines from perl5db.pl version 1.51 Editor support available. Enter h or 'h h' for help, or 'perldoc perldebug' for more help. <main::(test.plx:1): print "hello"; Unable to get Terminal Size. The Win32 GetConsoleScreenBufferInfo call + didn't work. The COLUMNS and LINES environment variables didn't work +. at C:/StrawberryPERL/perl/vendor/lib/Term/ReadLine/readline.pm line 41 +0. at C:/StrawberryPERL/perl/vendor/lib/Term/ReadKey.pm line 462. Term::ReadKey::GetTerminalSize(GLOB(0x28025f4)) called at C:/S +trawberryPERL/perl/vendor/lib/Term/ReadLine/readline.pm line 410 readline::get_window_size called at C:/StrawberryPERL/perl/ven +dor/lib/Term/ReadLine/readline.pm line 1114 readline::init called at C:/StrawberryPERL/perl/vendor/lib/Ter +m/ReadLine/readline.pm line 208 require Term/ReadLine/readline.pm called at C:/StrawberryPERL/ +perl/vendor/lib/Term/ReadLine/Perl.pm line 63 eval {...} called at C:/StrawberryPERL/perl/vendor/lib/Term/Re +adLine/Perl.pm line 63 Term::ReadLine::Perl::new("Term::ReadLine", "perldb", GLOB(0x2 +8b27bc), GLOB(0x28025f4)) called at C:/StrawberryPERL/perl/lib/perl5d +b.pl line 6868 DB::setterm() called at C:/StrawberryPERL/perl/lib/perl5db.pl +line 1849 DB::_DB__read_next_cmd(undef) called at C:/StrawberryPERL/perl +/lib/perl5db.pl line 2786 DB::DB called at test.plx line 1 SetConsoleMode failed, LastError=|6| at C:/StrawberryPERL/perl/vendor/ +lib/Term/ReadKey.pm line 346. at C:/StrawberryPERL/perl/vendor/lib/Term/ReadLine/readline.pm line 1 +581. readline::readline(" DB<1> ") called at C:/StrawberryPERL/per +l/vendor/lib/Term/ReadLine/Perl.pm line 11 Term::ReadLine::Perl::readline(Term::ReadLine::Perl=ARRAY(0x61 +1b1c), " DB<1> ") called at C:/StrawberryPERL/perl/lib/perl5db.pl li +ne 7367 DB::readline(" DB<1> ") called at C:/StrawberryPERL/perl/lib/ +perl5db.pl line 1858 DB::_DB__read_next_cmd(undef) called at C:/StrawberryPERL/perl +/lib/perl5db.pl line 2786 DB::DB called at test.plx line 1

    I am using a fresh install of the newest Strawberry Perl (32Bit), which is release 5.26.2.1. I run this in Windows 7 (64Bit), patched with the latest updates. For what it is worth the same error occurs if I try the 64Bit edition of Strawberry Perl either. Other than this, Perl programmes themselves run without any problem. It is only when I try to execute them under the native debugger that I encounter an issue.

    Can any of you chaps suggest a solution for this?

    A quick search of the forum came up with a very similar issue reported by 'Ovid' way back in 2007. However that gentleman encountered the error while employing something called 'Prove', which I have never come across before and is certainly not something I am using myself. I think most of the suggestions in that thread related to using 'Prove', so I do not know how to apply them in my own far simpler situation.

    UPDATE:

    Well... A degree more persistence with the search function, both here and over at Google suggested another approach to sort this out that was not centred on 'Prove'; create an environment variable 'TERM' and set its value to 'dumb' (case sensitive for each I believe). After doing so perl -d debug_test.pl began working like a charm!!! So... I guess that is the fix. Which is good of course, but I have no idea why I couldn't find that result the first half-dozen times I searched for an answer... Weird indeed. Still. The jobs a good'un--problem solved!

    "Aure Entuluva!" - Hurin Thalion at the Nirnaeth Arnoediad.
How do I use "Long Doubles" in perl?
3 direct replies — Read more / Contribute
by cnd
on May 19, 2018 at 10:46
    My perl has them:
    #perl -e 'use Config;print "long doubles\n" if $Config{d_longdbl} eq " +define";' long doubles
    But they are not default:-
    # perl -e 'use Config;if ($Config{uselongdouble} eq "define") {print " +long doubles by default\n";} else {print "not default? How to use???\ +n"}' not default? How to use???
    So how do I force their use?

    I do not want to use external modules, and yes, I know all about base-2 and float precision.

'%hash = ()' is slower than 'undef %hash'
8 direct replies — Read more / Contribute
by rsFalse
on May 18, 2018 at 06:13
    Hello.

    Today I've found, that my code with %hash = () is slower than undef %hash about 1.2 times. Perl 5.20.1. My hash contained simple values, not a Hash of Hashes or smth.

    I think that %hash = () should be aliased to undef %hash. What is your opinion?
find all numeric values from a string and join them
3 direct replies — Read more / Contribute
by Anonymous Monk
on May 18, 2018 at 05:46

    Hi, Im trying to have all numeric values from a sting and join them with pipe string is

    EEH_ErrorCode=( 15, /* Component */ 65 /* Error */) and my output should be 15|65

    while ($flag == 0 ){ if ($str =~ m/\)/ ) { $flag = 1; } else{ my @array=split(/' '/,$str); if(defined $array[0] ){ $array[0] =~ s/[^0-9]//g}else{$array[0] +=''}; if(defined $array[1] ){ $array[1] =~ s/[^0-9]//g}else{$array[1] +=''}; } }
Distinguishing a filehandle for an in-memory string
2 direct replies — Read more / Contribute
by jrw
on May 17, 2018 at 19:42

    I have noticed that many file IO operations, such as read(), work on in-memory string filehandles, but sysread() doesn't. So, how can I tell if someone has passed me filehandle to such a thing, so I can work around this limitation?

    #!/usr/bin/perl use strict; use warnings; sub dbg { my ($op, $fh, $rc, $scalar) = @_; $rc = "UNDEF" unless defined $rc; print "<$op><$rc><$scalar>\n"; close $fh or die; } sub doit_read { my ($fh) = @_; my $rc = read $fh, my $scalar, 5; dbg "read", $fh, $rc, $scalar; } sub doit_sysread { my ($fh) = @_; my $rc = sysread $fh, my $scalar, 5; dbg "sysread", $fh, $rc, $scalar; } my $fh; open $fh, "<", \"/etc/passwd" or die; doit_sysread $fh; open $fh, "<", \"/etc/passwd" or die; doit_read $fh; open $fh, "<", "/etc/passwd" or die; doit_sysread $fh; open $fh, "<", "/etc/passwd" or die; doit_read $fh;

    Output:

    <sysread><UNDEF><> <read><5></etc/> <sysread><5><jrw32> <read><5><jrw32>
Using ExtUtils::MakeMaker to install non-Perl files
1 direct reply — Read more / Contribute
by Anonymous Monk
on May 17, 2018 at 03:45

    I'm trying to install a Perl app using ExtUtils::MakeMaker, as a first step towards building a .deb package.

    The app requires various (read-only) text files, icons and sound files that need to be installed too.

    MANIFEST correctly lists all files, Perl and non-Perl, but running Makefile.PL only installs the Perl files.

    What's the recommended shows no interest in anything that's not Perl. I read the docs for EU::MM, and also perlnewmod etc, none of which offer clues.

    What's the recommended method of installing everything at once?

Variable declared in script, used by module, and used in script
4 direct replies — Read more / Contribute
by ExReg
on May 16, 2018 at 13:04

    Having a bad morning remembering. I have a script that runs a bunch of checks. It uses a module that contains an array of checks. There are variables defined in the script that are in the checks in the module that I can get to work. Here is a simplified example:

    checks.pl use strict; use warnings; use check_module; my $home_dir = '/home/mine/'; for my $check ( @checks ) { print "Checking $check->{name}\n"; `check->{script}`; } check_module.pm package check_module; use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(@checks); our @checks = ( { name => "Anybody home?", script => qq/echo $home_dir/, }, ); 1;

    When I run it I get

    Checking Anybody home?

    How do I get the $home_dir to evaluate so that I get

    Checking Anybody home? /home/mine

    I hope I typed this simplified example OK. It is on another system that cannot use CPAN or anything else except that which is installed. Thanks.

Abusing Map
5 direct replies — Read more / Contribute
by writch
on May 16, 2018 at 12:34
    I have a loop that calculates the difference between a variable in an array to the next element in the array. I'm currently using a pretty standard manner of doing this, namely

    for (my $i=0;$i<@a;++$i){ $b[$i] = $a[$i] - $a[$i+1]; }

    I wondered if there was any way to address the "$_ + 1" thought in a map statement. I've been looking for any examples, but I don't find them. Obviously this isn't it, but that's the thought at least.

    @b = map{$_ - $_+1}, @a;
New Cool Uses for Perl
Conways Game of Life in PDL
1 direct reply — Read more / Contribute
by mxb
on May 16, 2018 at 11:31

    Edit: Apparently this is in the PDL Documentation, as an example. Whoops! Still, it was a good learning exercise :)

    Rather than a ported numpy tutorial, this is a self developed implementation of Conways Game of Life written in Perl/PDL. Hopefully people find this interesting as I feel it shows how concise PDL code can be.

    The code is fairly straightforward. There is a single function conway() which accepts a single argument of the game arena. This is a two dimensional PDL matrix. Alive cells are represented by a one, dead ones by zero. The conway() function sums the value of each cell along with value of its nine neighbours into a temporary variable $tmp. It then applies the rules of the game, which are:

    • Any live cell with fewer than two live neighbors dies, as if caused by under population.
    • Any live cell with two or three live neighbors lives on to the next generation.
    • Any live cell with more than three live neighbors dies, as if by overpopulation.
    • Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

    This is implemented as an elementwise or and an elementwise and.

    The main loop of the game is in the body of the code and simply displays the generation and the game arena and awaits input

    The game arena is initialised with a 'glider', but feel free to experiment. As PDL wraps around by default, the surface is that of a torus.

    Enter a blank line for the next generation, anything else to exit

    Enjoy

    #!/usr/bin/env perl use strict; use warnings; use 5.016; use PDL; sub conway { my $pdl = shift; die "Not 2D piddle" unless $pdl->ndims == 2; # Add up all values: my $tmp = $pdl + # original $pdl->transpose->rotate(-1)->transpose + # north $pdl->transpose->rotate(-1)->transpose->rotate(-1) + # northeast $pdl->rotate(-1) + # east $pdl->transpose->rotate(1)->transpose->rotate(-1) + # southeast $pdl->transpose->rotate(1)->transpose + # south $pdl->transpose->rotate(1)->transpose->rotate(1) + # southwest $pdl->rotate(1) + # west $pdl->transpose->rotate(-1)->transpose->rotate(1); # northwest # Cell is alive if it's either: return ( $tmp == 4 & $pdl == 1 ) | # Alive +3 neighbors $tmp == 3; # Alive +2 neighbors or dead +3 neighbors } my $arena = pdl(byte, [ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], ] ); my $gen = 0; while (1) { print "Generation: $gen (press enter for next)\n"; print $arena; $arena = conway($arena); $gen++; exit if <STDIN> ne "\n"; }
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 surveying the Monastery: (7)
As of 2018-05-23 11:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?