Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
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
How can I read DATA in parent and child?
5 direct replies — Read more / Contribute
by Skeeve
on Feb 18, 2019 at 08:33

    I'm trying to read DATA in a parent and in a child process, but seem to be unable to achieve this.

    Simply using <DATA> didn't work.

    So I tried fdopen, but still just the parent seems to be able to read my DATA.

    What am I doing wrong?

    use IO::Handle; use strict; use warnings; my $x= IO::Handle->new(); $x->fdopen(fileno(DATA), "r"); my $y= IO::Handle->new(); $y->fdopen(fileno(DATA), "r"); my $pid= fork(); die unless defined $pid; if ($pid == 0) { print "Start c\n"; while (<$x>) { print "c: $_"; sleep 1; } print "stop c\n"; } else { print "Start p\n"; while (<$y>) { print "p: $_"; sleep 1; } print "stop p\n"; } __DATA__ a b c d e

    Output is:

    Start p p: a Start c stop c p: b p: c p: d p: e stop p

    Update In an earlier version I mixed up parent and child. Fixed.


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Multiple queries on DBI corresponding to multiple csv files?
4 direct replies — Read more / Contribute
by jtech
on Feb 18, 2019 at 05:54

    Hi Perlmonks,

    I am new at the Perl world and this is my first post and script

    The below script generates .csv files corresponding to each executed query using DBI but, I am not happy with the code redundancy.

    Had to search for some examples but didn't find anything. I would appreciate an example using a hash to understand how it works

    Is there any nice way to do it?

    Thanks in advance

    # Create multiples csv file corresponding to each sql query my $csvfile1 = "csvfile1.csv"; my $csvfile2 = "csvfile2.csv"; my $csvfile3 = "csvfile3.csv"; # DBI CONNECTION my($dbh) = DBI->connect("dbi:Ingres:$dbname","$user","") or die "Could not connect to database $dbname\n"; #tb1 select my ($sth) = $dbh->prepare(SELECT * FROM tb1) or die "Prepare failed: $DBI::errstr\n"; $sth->execute() or die "Prepare failed: $DBI::errstr\n"; #create csvfile1 according to tb1 select open my $fh, ">raw", $csvfile1 or die "$csvfile1: $!"; $fh->print (join(",", @{$sth->{NAME}}), "\n"); #show header while (my @row = $sth->fetchrow_array()) { $fh->print (join(",", @row), "\n"); }; close $fh or die "$csvfile1: $!"; $sth->finish(); #tb2 select my ($sth) = $dbh->prepare(SELECT * FROM tb2) or die "Prepare failed: $DBI::errstr\n"; $sth->execute() or die "Prepare failed: $DBI::errstr\n"; #create csvfile2 according to tb2 select open my $fh, ">raw", $csvfile2 or die "$csvfile2: $!"; $fh->print (join(",", @{$sth->{NAME}}), "\n"); #show header while (my @row = $sth->fetchrow_array()) { $fh->print (join(",", @row), "\n"); }; close $fh or die "$csvfile2: $!"; $sth->finish(); #tb3 select my ($sth) = $dbh->prepare(SELECT * FROM tb3) or die "Prepare failed: $DBI::errstr\n"; $sth->execute() or die "Prepare failed: $DBI::errstr\n"; #create csvfile3 according to tb3 select open my $fh, ">raw", $csvfile3 or die "$csvfile3: $!"; $fh->print (join(",", @{$sth->{NAME}}), "\n"); #show header while (my @row = $sth->fetchrow_array()) { $fh->print (join(",", @row), "\n"); }; close $fh or die "$csvfile3: $!"; $sth->finish(); $dbh->disconnect();
Fastest way of changing the beginning of a txt file..
7 direct replies — Read more / Contribute
by pedrete
on Feb 18, 2019 at 04:08

    Hi!

    i want to read a huge text file reading its lines... once i have found the line i was looking for, i want to "cut" the file so it will start at this very line (discarding previous ones).

    There are many ways to do this, but looking for the fastest way i was wondering...

    is there any way to tell the OS some thing like: "Set actual FILEHANDLE's position as the file-start postion"??? in a way that if i close the FILEHANDLE, then the file will only have valid lines...

    Thank...

    Pedreter.

Increase speed of nested loop Win32 Excel Manipulation
3 direct replies — Read more / Contribute
by Sasuke300
on Feb 16, 2019 at 12:55

    Hey guys I have a problem with the speed of one part of my perl script. My script deals with a manipulation of a excel file. The part where the speed is very low is a nested loop and I have no idea how to avoid that. I can show you the part:

    use strict; use warnings; use Win32::OLE; use Win32::OLE qw(in with); use Win32::OLE::Const "Microsoft Excel" for ($begincol;$begincol<$totcol;$begincol++) { for ($beginrow;$beginrow<$maxrows;$beginrow++) { my $cap = $Sheet2->Cells($beginrow,5)->{'Text'}; my $app = $sheet2->Cells(3,$begincol)->{'Text'}; my $yes = "yes"; if (index($app, $cap) != -1) { $Sheet->Cells($beginrow,$begincol)->{Value} = $asis; } } $beginrow = 7; }
New to perl - Check authenticity of cpan mods installed/used
5 direct replies — Read more / Contribute
by gradius85
on Feb 16, 2019 at 11:00

    Hi everyone,

    I am very new to perl and programming in general. I have started in a position that has me working with legacy Perl code, which has a number of modules from ‘cpan’. Due to this opportunity I am excited to learn Perl since I have read great things about it.

    So far the syntax has been pretty easy for me to understand, and honestly grasping it a little faster then I did Python (all those silly name.item.something format...lol).

    Anyway my main question.

    I have never used cpan, but I have been watching some youtube videos. However, how do I check or validate the authenticity of the libraries/modules (not sure what is the true name) installed from cpan?

    I am worried, because I heard that some libraries on Github had some bitCoin mining software compiled into the library.

    I do not have a college degree, and self-taught myself Python the basics (if you could call that), so I am not allowed to work on that team that uses JavaScript or other programming languages, and I volunteered to work on the legacy perl code items, with the hope of getting good to get experience and exposure to different programming styles and methods.

    Also,

    Looks like some of the code used object oriented style of code, from what I researched.

    Can anyone offer a good book that discussed OOP styles? Does not have to be perl related, I do not get the concept of OOP and I am struggling with it.

    Thank you so much

Result of failed regexp match not pushed onto array
2 direct replies — Read more / Contribute
by zork42
on Feb 16, 2019 at 07:02

    I'm completely mystified by this.

    In the 3 example code blocks below I'm pushing the same things onto an array.

    When I push the result of a failed match, nothing is pushed onto the array (unless I first save the result in a variable).

    Why is this?!

    Thanks!

    use strict; use warnings; my @foo; push @foo, 1; print scalar @foo, "\n"; # output +: 1 push @foo, 'abc' =~ /abc/; print scalar @foo, "\n"; # output +: 2 push @foo, 'abc' =~ /abcd/; print scalar @foo, "\n"; # output +: 2 - nothing added to @foo?! push @foo, ''; print scalar @foo, "\n"; # output +: 3 push @foo, 0; print scalar @foo, "\n"; # output +: 4 push @foo, undef; print scalar @foo, "\n"; # output +: 5 @foo = (); print "\n"; push @foo, (1); print scalar @foo, "\n"; # output +: 1 push @foo, ('abc' =~ /abc/); print scalar @foo, "\n"; # output +: 2 push @foo, ('abc' =~ /abcd/); print scalar @foo, "\n"; # output +: 2 - nothing added to @foo?! push @foo, (''); print scalar @foo, "\n"; # output +: 3 push @foo, (0); print scalar @foo, "\n"; # output +: 4 push @foo, (undef); print scalar @foo, "\n"; # output +: 5 @foo = (); my $bar; print "\n"; $bar = 1; push @foo, $bar; print scalar @foo, "\n"; + # output: 1 $bar = 'abc' =~ /abc/; push @foo, $bar; print scalar @foo, "\n"; + # output: 2 $bar = 'abc' =~ /abcd/; push @foo, $bar; print scalar @foo, "\n"; + # output: 3 - this time its been added to @foo $bar = ''; push @foo, $bar; print scalar @foo, "\n"; + # output: 4 $bar = 0; push @foo, $bar; print scalar @foo, "\n"; + # output: 5 $bar = undef; push @foo, $bar; print scalar @foo, "\n"; + # output: 6
Perl to C problem
2 direct replies — Read more / Contribute
by lbrandewie
on Feb 16, 2019 at 00:49

    Hey folks,

    I'm having a problem. I'm porting some perl to C to speed it up, and I can't get the two languages to agree on what's going on. I'm using ActiveState's perl 5.24.1 for Windows. Consider the following code:

    
    $test = "\n\n\n\n\n";
    
    for ($x = 0; $x < length($test); $x++) {
        print ord(substr($test, $x, 1));
        <STDIN>;
    }
    
    

    The code prints a string of 10s, indicating that the 13/10 combination in Windows has been translated to a bare 10. I can understand that, for compatibility reasons. But it seems to mean there is no way in Windows of telling a 13/10 combo from a bare 10. This makes a difference in my code, in that I can't get C and perl to hash the same string the same way. Am I missing something? Is there no way at all to tell a 13/10 from a 10 in Windows perl?

    I believe the upshot of this, where my code is concerned, is that perl is doing it "wrong" compared to C and I'll never get the two to agree. I hope I'm wrong about that. I note with interest and mild annoyance that strlen("\n") == 1 even in C for windows. But C allows me to get at the underlying character buffer and perl does not.

    Thanks,

    Lars

Function::Parameters, Types::Standard, and Perl::Critic
2 direct replies — Read more / Contribute
by choroba
on Feb 15, 2019 at 18:36
    After switching to Function::Parameters, I noticed Perl::Critic no longer worked. For example, for the old fashioned
    sub foo { $_[0] + 1 }
    perlcritic -1 outputs
    Always unpack @_ first at line 22, column 1. See page 178 of PBP. (S +everity: 4) Subroutine "foo" does not end with "return" at line 22, column 1. See + page 197 of PBP. (Severity: 4)

    but for the equivalent

    use Function::Parameters; fun foo ($x) { $x + 1 }
    it doesn't say anything.

    I've found a partial workaround: running perlcritic on the output of B::Deparse instead of the source itself. It reports lots of sins committed by Function::Parameters themselves (e.g. Magic variable "$^H" should be assigned as "local" at line 14, column 44. See pages 81,82 of PBP.) but seems to work.

    Unfortunately, it gets more complex when Types::Standard are added to the mix.

    use Function::Parameters; use Types::Standard qw( Num ); fun foo (Num $x) { $x + 1 }
    The code doesn't look much more complicated, but running perl -MO=Deparse on it just hangs. Output of strace ends with endlessly repeating lines:
    mmap(NULL, 192512, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1 +, 0) = 0x7fe772fdf000 mremap(0x7fe771772000, 532480, 536576, MREMAP_MAYMOVE) = 0x7fe77177200 +0 brk(0x49ac000) = 0x49ac000 brk(0x49cd000) = 0x49cd000 mremap(0x7fe771772000, 536576, 540672, MREMAP_MAYMOVE) = 0x7fe77177200 +0 brk(0x49ee000) = 0x49ee000 brk(0x49ed000) = 0x49ed000 brk(0x4a0e000) = 0x4a0e000 brk(0x4a2f000) = 0x4a2f000 mremap(0x7fe771772000, 540672, 544768, MREMAP_MAYMOVE) = 0x7fe77177200 +0 ...
    The perl process seems to grow in memory very slowly at the same time.

    Has anyone succeeded in linting a code that uses the above mentioned modules? Tips, tricks, suggestions welcome.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Parsing JSON out of an incremental stream
2 direct replies — Read more / Contribute
by Sec
on Feb 15, 2019 at 11:40

    Hi. I'm looking for a simple way to parse JSON objects out of a stream.

    I am calling an HTTP service that returns a stream of json objects over time.

    To read it, i registered a simple callback handler in LWP::UserAgent

    $ua->add_handler( response_data => sub { my($response, $ua, $h, $data) = @_; return "true" if $data =~ /^\s*$/; print "Callback triggered\n"; print "data: .${data}.\n"; my $result = eval { $json->decode( $data ) }; if ($@){ print "error: $@\n"; return "true"; }; print Dumper $result; return "true"; } );

    As expected, i sometimes get partial json objects in $data.

    My first idea would be to so something like m/{.*?}/ against $data. Or rather some more elaborate version that deals with balanced parens and quoted strings.

    My question is, is there an easier / simpler / more straightforward way to deal with something like this that I am missing?

    -- Sec
    EDIT: to clarify a bit.

    The problem is that in the callback $data may just be

    { "foo": "bar" } { "foo" : "q
    which means I have to grab one object, handle it, keep the rest and wait for more data.

    I know how to write code for that "the long way". I was asking if there is something clever or an existing module that can help with this.

Print out hash
2 direct replies — Read more / Contribute
by catfish1116
on Feb 15, 2019 at 11:07

    I am trying to create a hash and then print it out. When executing program, I am getting $key & $value need explicit package. O wise monks, please show me the error of my ways :)

    #! /usr/bin/perl use v5.12; use warnings; ## 2/15/19 my @people = qw{ fred barney fred wlima dino barney fred pebbles +}; my %count; # new empty hash $count{$_}++ foreach @people; while ( ($key, $value) = each %count) { print "$key => $value\n"; } ~

    TIA the catfish

can has ELI5 namespace::autoclean pls?
2 direct replies — Read more / Contribute
by Ea
on Feb 15, 2019 at 09:58
    Friday afternoon question: what's the real purpose of namespace::autoclean in your OO code?

    I've seen it in classes written with Moose, but the description Keep imports out of your namespace doesn't really speak to me about the advantage of using it. Where am I going to get bitten if I don't use it. Just a simple answer will do.

    thanks,

    Ea

    Sometimes I can think of 6 impossible LDAP attributes before breakfast.

    Mojoconf was great!

Useful heuristics for analyzing arrays of data to determine column header
3 direct replies — Read more / Contribute
by nysus
on Feb 14, 2019 at 18:06

    I'm writing a Perl module to try to autodetect whether a spreadsheet/csv file has a header row. It's a somewhat tricky problem especially when trying to factor in the kind of malformed data people might feed in. I'm sticking with simple cases for now. I'd like to try to do some statistical analysis of the data to help me. Unfortunately, my knowledge of statistics is very weak. I'm feeling my way in the dark. So take this sample column for example:

    STATE NY NY NY NJ NY

    It's obviously a column of states. One thing that might jump out to a computer is that the length of the first row is 5 letters while the rest of the rows are two letters. Things can of course get fuzzier. The first row might have 5 letters while the rest of the columns have 2 OR 3 letters. Other tell-tale signs might be the header column is a string while the column is full of numbers. Or the header column might be named "STATUS" while the data might only contain the words "ACTIVE" or "RETIRED." The size of the column is also an important factor. If there is a lot of data, any statistical approach will likely be more accurate.

    The Math::NumberCruncher module has some useful functions I think I could use like standard deviation to help me analyze things like how diverse the dataset is for a certain property (length, number of unique values, etc.). But I'm not really sure how I might apply it to be useful in a practical way. I found this interesting article was useful but there's no code and not being familiar with statistics, I'm still not clear on exactly how that analysis was done.

    I intend to analyze each column and try to come up with some kind of "likely has header" factor based on the analysis. If it looks like most columns have a header, then it will determine that the spreadsheet has a header row.

    Sorry this question is so open-ended. But any tips/advice you can think of would be appreciated.

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

New Cool Uses for Perl
Conversions of SI Units
1 direct reply — Read more / Contribute
by choroba
on Feb 16, 2019 at 16:57
    My son (11) has just learned SI units and their prefixes at school. They started just with metres and litres, only some of the prefixes (mili, centi, deci, hecto, kilo) and they don't know the floating point yet. Because of his broken arm, he missed several days at school and needed to practice. So, I've written a Tk application for him to practice. It takes one parameter, the number of formulas to generate.

    Feel free to localize it using the constants at the beginning. Adding the floating point left as an exercise for the reader.

    It's been a practice for me, too, because of Function::Parameters.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2019-02-19 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I use postfix dereferencing ...









    Results (104 votes). Check out past polls.

    Notices?