Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
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
Hash vs constant vs package vs other for data structure
5 direct replies — Read more / Contribute
by oldtechaa
on Mar 27, 2017 at 14:38

    I'm using an AoAoA currently for a data structure. An example of its use would be like this: $notes[$x][$y][0] = ... As you can see, the first two dimensions refer to the location of an object and the third refers to properties of that object such as flags and other data. My problem is that although the indices always refer to the same data member, it's not very readable or maintainable if you forget the index number for the data you want.

    A couple solutions I've thought of are below:

    • Use an AoAoH instead, and refer to each data member by name
    • Use constant declarations to name each index
    • Use upper-case variables to show their status as constant names for indices
    • Use a package with setter and getter functions or public data members and use an AoA with the package objects as the contents
    • Get other ideas from PM

    What do you think? I don't want to use any external CPAN distributions and I'd like the solution to not require too much boilerplate. Thanks for your time.

Filtering rows with Parse::CSV
3 direct replies — Read more / Contribute
by Anonymous Monk
on Mar 27, 2017 at 14:02
    Hello, and thanks in advance for your help.

    I'm attempting to get all rows from a CSV file where a specific column is not null. Unfortunately, I'm only getting 1 row back.

    For example, I'd like only rows 5, 7, and 9 to be printed.

    row 1-4 : myColumn is null
    row 5 : myColumn = 'ABC'
    row 6 : myColumn is null
    row 7 : myColumn = 'CDE'
    row 8 : myColumn is null
    row 9 : myColumn is 'EFG'

    My expected results are:
    ABC
    CDE
    EFG

    Instead, my results are:
    ABC

    Here's the code I am using:

    my $parser = Parse::CSV->new( file => $CSV_File, sep_char => ',', names => 1, filter => sub { $_->{myColumn} ? $_ : undef } ); while ( my $row = $parser->fetch ) { my $myColumn_value = $row->{'myColumn'}; print ("$myColumn_value"); }
Redistributable Apache Web Server with Strawberry Perl - Practical?
1 direct reply — Read more / Contribute
by whiteperl051
on Mar 27, 2017 at 05:03
    I'm looking into creating a Windows Desktop App/server with Apache for Windows and Strawberry Perl.

    The way it would work is that I redistribute Apache with Strawberry Perl with my Perl scripts which are the app. When installing my App on a Windows PC it acts like a web server serving up web pages. My app will be working like a web site. I'm planning on creating a media server. Users install it on their PC's and media is served through a web browser to their PC's or other devices.

    If I understand the Apache license correctly then I can redistribute Apache in this manner? My application will be free.

    I've tried other solutions like Mojolicious but Apache + Strawberry Perl seems to be the most stable and practical solution. Can anybody see any major issues in going this route? (Apache + Strawberry Perl) I would hate to put in many hours on this just to find it is not practical.

Howto convert Image::Grab jpg object to Image::Magick object???
2 direct replies — Read more / Contribute
by dazz
on Mar 27, 2017 at 04:24
    Hello

    I have grabbed a jpg image from an IP camera with Image::Grab. So I have a valid image object.

    I want to pass the image to a subroutine. Within the subroutine, I want to use an Image::Magick method to do a read test on the image. </p)

    Back in the main code, I want to use the grabbed image object and annotate/composite using Image::Magick methods.

    So how do I pass a Image::Grab object to a sub-routine, then apply a read-only Image::Magick method to it? How do I take a Image::Grab object and apply a write Image::Magick method to it?

    The questions are similar but different. At present, I am achieving this by saving the Image::Grab jpg to hard drive, then reading it into a Image::Magick object. It is slow and inefficient.

    Dazz

Differnece between Spreadsheet::ParseXLSX and Spreadsheet::ParseExcel
3 direct replies — Read more / Contribute
by Sonali
on Mar 27, 2017 at 02:15

    Hello PerlMonks,

    I have been trying to parse a .xls and another .xlsx file using the Spreadsheet::ParseExcel and Spreadsheet::ParseXLSX modules

    When I dump the contents of both the files into two different files, the structure of the hash generated is very different, I wanted to know what exactly is the difference between the two parsers.

    My code goes like this,

    #!/usr/local/bin/perl use warnings; use strict; use Spreadsheet::XLSX; use Spreadsheet::ParseExcel; use Data::Dumper; my $Output_FileName = 'outfile.txt'; my $Output_FileName1 = 'outfile1.txt'; my $excel = Spreadsheet::XLSX -> new ('book1.xlsx'); my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->parse('book2.xls'); open(my $FH, '>', $Output_FileName) or die "Could not open file '$Outp +ut_FileName' $!"; print $FH Dumper(\$excel); close $FH; open(my $FH1, '>', $Output_FileName1) or die "Could not open file '$Ou +tput_FileName1' $!"; print $FH1 Dumper(\$excel); close $FH1;

    Is there a better way to dump both the files so that I can compare the two dumped files? In my case the files were mostly full of information like font etc...

    Thank you!

Joining elements of an array in a hash
5 direct replies — Read more / Contribute
by orangepeel1
on Mar 26, 2017 at 13:29
    I have a hash of arrays
    @array1 = ("one", "two", "three", "four", "five"); @array2 = ("banana", "pear", "apple"); my %hash = (numbers => \@array1, fruit => \@array2 );
    I would like to go through all the keys to the hash and join the elements of each array. I have tried something like this
    foreach my $group (keys %hash) { push @group, @{ $hash {$group}}; foreach (@group) { $statement = join ",", @group; } }
    However it does not join them. Does anyone have any suggestions for how I might go about this?
Log::Log4perl help in rotating file
2 direct replies — Read more / Contribute
by ravi45722
on Mar 26, 2017 at 02:56
    log4perl.rootLogger=DEBUG, LOGFILE log4perl.appender.LOGFILE=Log::Log4perl::Appender::File log4perl.appender.LOGFILE.filename=/home/GEMS/ravi/Node/logs/Script.lo +g log4perl.appender.LOGFILE.mode=append log4perl.appender.LOGFILE.layout=PatternLayout log4perl.appender.LOGFILE.layout.ConversionPattern=%d : %r : %c - %m%n +

    This is my config for the logger

    I seen

     Log::Dispatch::FileRotate

    to rotate the file. I need to rotate the file based on the date. (Every day one single file). But how to add this to my conf??

    In my conf pattern %d prints 2017/03/26 12:17:33 can I change this pattern to 2017-03-26 12:17:33.432

Need regexp to pick off second parenthetical item
2 direct replies — Read more / Contribute
by cormanaz
on Mar 25, 2017 at 20:50
    Good day Bros. I am processing email sender fields, some of which have an email address embedded in a set of parentheses following a string inside another set of parentheses. I need a regexp that will get only the former, leaving the latter with the sender name. To be clear, in the following example:
    #!/usr/bin/perl -w use strict; my $sentto = 'Fred Flintstone (US) (fredf@gmail.com)'; $sentto =~ m/\(.+?@.+?\)/; print "First: $`\nSecond: $&"
    I would like $` to contain "Fred Flintstone (US) " and $& to contain "(fredf@gmail.com)". The regexp I have in there puts " (US) " at the beginning of $&. I understand why this happens, but not how to fix it. I thought about maybe a look-back assertion for a close-paren, but I have some addresses like "Barney Rubble (barneyr@gmail.com)" so I don't think that will work. I'd like to have a regexp that will pick off the parenthetical address for both kinds of senders. Can someone suggest a solution?
    "I think computers have complicated lives very bigly. The whole age of, you know, computer has made it where nobody knows exactly what's going on." --D. Trump
Accessing passwords in a script
4 direct replies — Read more / Contribute
by nysus
on Mar 25, 2017 at 16:12

    I have a script that needs passwords for things like mysql databases on other servers. I want to do this as painlessly and as securely as possible. So what I'm thinking is that my script would prompt for a single master password and then it would be granted access to the other passwords. I found this possible solution which explains how to use the KeePass module which seems like it might be a good route. But I wanted to get some input from the Monks who might have other good ideas. Many thanks.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

Need clean code
3 direct replies — Read more / Contribute
by ravi45722
on Mar 25, 2017 at 13:34

    I am learning the Perl code. I also want to use the beauty of the perl.

    I can write like this

     $var or $var = 30;

    But in this case how can I write the code using or

    Or is there any smart way to write this

    if (undef $var) { $x = 40; } else { $x = 50; }
Looking for a standalone separator for Perl/Tk
1 direct reply — Read more / Contribute
by kbrannen
on Mar 24, 2017 at 13:04
    I'm looking for a standalone separator widget for Perl/Tk, something to draw a line between other widgets. Imagine something like HTML's "hr" tag that can be horizontal or vertical ... or for those familiar with the old Motif, something like XmSeparator.

    Does anyone know of an existing widget for this? If not, what could easily be made to do this?

    I can do an empty Frame with a raised relief and a non-zero borderwidth like:
    my $frame = $parent->Frame(); my $info = $frame->Label(-text => "General Info")->pack(); # separator ... sort of $frame->Frame(-background => "black", -borderwidth => 1, -relief => 'r +aised', -height => 2) ->pack(-fill => 'x', -padx => 5, -pady => 5); my $comments = $frame->Label(-text => "next widget")->pack();
    which gets me really close, but it seems cumbersome. I assume I can do something similar for verticals with some option changes, but I haven't tried it yet. Is there a better alternative anyone can suggest?
Capture::Tiny alternative
5 direct replies — Read more / Contribute
by melezhik
on Mar 24, 2017 at 06:23

    Hi! I have been using Capture::Tiny to read from processes I launch in my scripts. It works pretty good. Good module. The only thing I need more is reading processes STDOUT "in real time" which impossible with this module as it captures all the data till the process exits and then return it. It makes user waits till long running command finishes and don't let him to see it's STDOUT in real time.

    Another solution with well known construction:

    open(my $fh, '-|', $system_command) or die $!;
    while (my $line = <$fh>) {
        # Do stuff with each $line.
    }
    
    Does not work for me, as it for mysterious reasons _sometimes_ ( some rare cases of $system_command ) it waits forever even when $system_command finishes and return all the data as STDOUT.

    Any suggestions?

    PS both workaround for open(my $fh, '-|', $system_command) or suggestion new IPC related module would be fine.


    Thanks.
New Meditations
#p5p finds your lack of failing tests disturbing
1 direct reply — Read more / Contribute
by Corion
on Mar 27, 2017 at 13:42

    5.26 will come with a major change in how Perl builds and runs modules. The current directory will not be in @INC when running Perl programs.

    This affects all programs that call require or use and expect files relative to the current directory to be available. Besides plugin systems, any Makefile.PL using for example Module::Install is a likely offender. So far very little has been caught in the net of CPAN testers.

    As an example, one of my tests loads Makefile.PL:

    use lib '.'; use vars '%module'; require 'Makefile.PL'; # Loaded from Makefile.PL %module = get_module_info(); my $module = $module{NAME};

    This will fail under 5.26+ without the use lib '.'; statement at the top.

    I've altered @INC, pray I do not alter it further

    If you want to check whether your modules should still work and test OK under the upcoming 5.26 release, I think that the following oneliner should give you a good indication:

    perl -M-lib=. Makefile.PL && make && make test

    If you run author tests or prefer your tests to be run using prove, you have to take care of prove invoking a fresh Perl:

    # Windows syntax: set PERL5OPT=-M-lib=. perl Makefile.PL && dmake && prove -bl xt t
    @rem Shell syntax: export PERL5OPT=-M-lib=. perl Makefile.PL && make && prove -bl xt t

    Please test your distributions and release new versions if they need . in @INC. Thanks!

    Update: Added call to action and noted where use lib '.' helps.

OT: Got fired this week
12 direct replies — Read more / Contribute
by karlgoethebier
on Mar 23, 2017 at 05:43

    As i'm aged 60 + i guess i'll never get a new job. I think i need to face retirement. I first thought to delete my account on PM and through away all the code i ever wrote as well as my hand library and forget about everything related to programming. But it isn't so easy. I spent the last 20 years with this stuff. I'll stay a bit here - for fun. And perhaps i'll learn a new programming language ;-)

    Update: What should i say? I'm deeply moved. Thank you very much to all for encouragement and good advice.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    Furthermore I consider that Donald Trump must be impeached as soon as possible

New Cool Uses for Perl
Automatically ensure your CPAN dists have up-to-date prereq version numbers
No replies — Read more | Post response
by stevieb
on Mar 26, 2017 at 19:29

    So... one of my distribution relies heavily on other distributions I've written, and it's hard to ensure my dependencies for my own modules are up-to-date in the prerequisite list in the build system. Sometimes I forget to bump a prereq before I do a release, which means I have to immediately do a one-line release the next day, because I'll have emails from CPAN Testers because tests are failing.

    I've been toying with a few ways to automatically check this for me. Below is one such hack I came up with. There's two vars that need to be set: $dist and $author. It then pulls the distribution from the CPAN, extracts all of it's prerequisite dependency information. Then, it fetches the list of all distributions I've put on the CPAN, and creates a dist/version hash.

    Note that this compares *only* the prereqs that I personally have uploaded. It'd be trivial to modify a bit to check them all.

    After the data is collected, it iterates the known dependencies, and if there's a match with one of my own other distributions, I compare versions. Currently, it just prints out the list, but I'm going to hack this into my Test::BrewBuild system as another command line option so that every build run, I'll be notified of any discrepancies. Eventually, I'll likely make it auto-update the Makefile.PL files for me with the new dep versions, as well as have it review the prereq versions in the current repo of the dist I'm working on, instead of comparing to the latest CPAN release, so I can correct the issues *before* pushing to PAUSE :)

    use warnings; use strict; use MetaCPAN::Client; my $c = MetaCPAN::Client->new; my $dist = 'RPi-WiringPi'; my $author = 'STEVEB'; check_deps($dist, $author); sub check_deps { my ($dist, $author) = @_; if ($dist =~ /:/){ die "\$dist must be hyphenated... don't use ::\n"; } my $release = $c->release($dist); my $deps = $release->{data}{dependency}; my $author_modules = author_modules($author); for my $dep (@$deps){ my $dep_mod = $dep->{module}; my $dep_ver = $dep->{version}; if (exists $author_modules->{$dep_mod}){ my $cur_ver = $author_modules->{$dep_mod}; print "$dep_mod: \n" . "\tdep ver: $dep_ver\n" . "\tcur ver: $cur_ver\n\n"; } } } sub author_modules { my ($author) = @_; my $query = { all => [ { author => $author }, { status => 'latest' }, ], }; my $limit = { '_source' => [ qw(distribution version) ] }; my $releases = $c->release($query, $limit); my %rel_info; while (my $rel = $releases->next){ my $dist = $rel->distribution; $dist =~ s/-/::/g; $rel_info{$dist} = $rel->version; } return \%rel_info; }

    Output:

    perl perl/dependency_version_compare/compare.pl RPi::DigiPot::MCP4XXXX: dep ver: 2.3603 cur ver: 2.3603 RPi::BMP180: dep ver: 2.3603 cur ver: 2.3603 RPi::ADC::MCP3008: dep ver: 2.3603 cur ver: 2.3603 RPi::SPI: dep ver: 2.3606 cur ver: 2.3606 RPi::DAC::MCP4922: dep ver: 2.3604 cur ver: 2.3604 RPi::WiringPi::Constant: dep ver: 0.02 cur ver: 0.02 RPi::DHT11: dep ver: 1.02 cur ver: 1.02 WiringPi::API: dep ver: 2.3609 cur ver: 2.3609 RPi::ADC::ADS: dep ver: 1.01 cur ver: 1.01
oneliner: autorun script when I save it in the editor
2 direct replies — Read more / Contribute
by FreeBeerReekingMonk
on Mar 26, 2017 at 18:08
    Sometimes you get spoiled by IDE's that have F5 to save and run what you have scripted so far... so... what can you do if you have 2 xterms (one for vi, the other for the output)?

    perl -E 'while(-f $ARGV[0]){ $now=(stat(_))[9]; system($^X,@ARGV) if($ +now-$prev); $prev=$now; sleep 1}' /home/user/test.pl foo bar

    with test.pl having:

    #! env perl my $p1 = $ARGV[0]; my $p2 = $ARGV[1]; print "param1=$p1 param2=$p2\n";

    yields:

    param1=foo param2=bar

    Tested to work under Win10 and Linux

    Of course, there are better implementations. inotifywait or auditd if available on your system...

    any perl golfers?

    Update: we now incorporate the improvement made by haukex. Feel free to add more parameters if you need these

New Monk Discussion
Prohibit empty nodes
2 direct replies — Read more / Contribute
by hippo
on Mar 23, 2017 at 11:54

    It's a minor annoyance but hopefully an even more minor fix. I suggest that the site code be modified such that entirely empty nodes are automatically rejected. It's a particular time-and-resource waster in the case of anonymous posts as they serve absolutely no value - would anyone dispute this?

    There might conceivably be some argument for allowing empty non-anonymous posts (although what it might be escapes me) so that could be up for debate. If you think these are worth allowing then do please contribute to this thread.

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 making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2017-03-27 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (324 votes). Check out past polls.