Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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
Perl MongoDB Results and Version (batch_size??)
1 direct reply — Read more / Contribute
by maikelnight
on Sep 21, 2018 at 15:57
    Hi Monks, i have some code that works pretty fine with MongoDB v0.705.0.0 :
    use Data::Dumper; use DateTime::Format::Strptime; use POSIX qw(strftime); use MongoDB; use Data::Structure::Util qw( unbless ); use strict; use warnings; my $mongoclient = MongoDB::MongoClient->new( host => '127.0.0.1', port => 27017 ); my $db = $mongoclient->get_database('database'); my $collect = $db->get_collection('collection')->aggregate([ {'$group' => { '_id' => {_path => '$path' , _ip => '$IP', _time => '$TIME'}, '_count' => { '$sum' => 1}, '_docs' => { '$push' => '$_id' } } }, { '$match' => { '_count' => { '$gt' => 1} }} ]); my $mongo_aggregate = unbless $collect; my @out = (@$mongo_aggregate); print Dumper @out;
    I receive a few thousand results what is expected and checked in database....so far so good...On another system with MongoDB v1.2.2 i receive only 101 results:
    use Data::Dumper; use DateTime::Format::Strptime; use POSIX qw(strftime); use MongoDB; use Data::Structure::Util qw( unbless ); use strict; use warnings; my $mongoclient = MongoDB::MongoClient->new( host => '127.0.0.1', port => 27017 ); my $db = $mongoclient->get_database('database'); my $collect = $db->get_collection('collection')->aggregate([ {'$group' => { '_id' => {_path => '$path' , _ip => '$IP', _time => '$TIME'}, '_count' => { '$sum' => 1}, '_docs' => { '$push' => '$_id' } } }, { '$match' => { '_count' => { '$gt' => 1} }} ]); my $mongo_aggregate = unbless $collect->{'_docs'}; my @out = (@$mongo_aggregate); print Dumper @out;
    If i dump my $collect i found a hint that says: '_batch_size' => 101 I believe thats the point where im stucking. I dont know how to solve the issue nor i can fix that with documentation (as im advanced beginner). Can someone please shed some light on me, please. Thanks, regards,
Prototypes required even after mocking a sub
2 direct replies — Read more / Contribute
by stevieb
on Sep 21, 2018 at 15:11

    So, I had a user of my Mock::Sub distribution file an issue report where trying to mock a subroutine that has prototypes threw a warning. After monkeying about with it, I was able to quell said warnings, but it raised something else that I don't understand. I'll post some code and context, then get to what I'm hoping to have answered.

    This is a runnable example based on the snippet that the user reported:

    use warnings; use strict; use Mock::Sub; my $m = Mock::Sub->new; sub foo ($$$){ return undef; } my $foo_sub = $m->mock('foo');

    Result:

    Prototype mismatch: sub main::foo ($$$) vs none at /home/stevieb/perl5 +/perlbrew/perls/perl-5.26.1/lib/site_perl/5.26.1/Mock/Sub/Child.pm li +ne 122. Prototype mismatch: sub main::foo: none vs ($$$) at /home/stevieb/perl +5/perlbrew/perls/perl-5.26.1/lib/site_perl/5.26.1/Mock/Sub/Child.pm l +ine 140.

    All well and good. As a response to the user, I drummed up a test script with a signal handler to catch Prototype warnings, and just evaporate them (I'll incorporate this into the distribution directly if the reporter is satisfied, enabled only if requested explicitly):

    use warnings; use strict; use feature 'say'; use Mock::Sub; $SIG{__WARN__} = sub {say $_[0] if $_[0] !~ /Prototype/}; my $m = Mock::Sub->new; sub foo ($$$){ say "hello!"; # doesn't get called return undef; } my $foo = $m->mock('foo'); $foo->return_value('test'); say foo(1, 2, 3); say foo(1, 2, 3); say foo(1, 2, 3); say $foo->called_count;

    That code does the right thing, insofar that the mocked sub is properly called and the prototype warnings are no longer displayed:

    test test test 3

    Now, what I found while testing, is that initially, I called foo() with no parameters, but the prototype stuck, resulting in fatality. Instead of foo(1, 2, 3); (proper number of params), I had just foo(), and...

    Not enough arguments for main::foo at mock.pl line 21, near "()" Execution of mock.pl aborted due to compilation errors.

    All I do when I mock out a sub, is overwrite the symbol table for it (actual code from the module):

    { no strict 'refs'; no warnings 'redefine'; my $mock = $self; weaken $mock; *$sub = sub { @{ $mock->{called_with} } = @_; ++$mock->{called_count}; if ($mock->{side_effect}) { if (wantarray){ my @effect = $mock->{side_effect}->(@_); return @effect if @effect; } else { my $effect = $mock->{side_effect}->(@_); return $effect if defined $effect; } } return if ! defined $mock->{return}; if ($mock->{return}[0] && $mock->{return}[0] eq 'params'){ return ! wantarray ? $_[0] : @_; } else { return ! wantarray && @{ $mock->{return} } == 1 ? $mock->{return}[0] : @{ $mock->{return} }; } }; }

    My question here, is if the symtab entry was overwritten correctly (ie. the mocked sub is most definitely called as desired), why does perl still think that it requires the prototyped parameters? Clearly, that information is stored somewhere, but where and how?

    Can someone point me in a direction I can look down to understand this, or even explain it to me?

    Thanks, as always,

    -stevieb

Win32::OLE on cygwin64, can't make OLE.xs
1 direct reply — Read more / Contribute
by gargle
on Sep 21, 2018 at 05:05

    Fellow monks,

    I can't install Win32::OLE 0.1712 on my cygwin64. I already changed stricmp into strcasecmp by means of look Win32::OLE, but make Win32::OLE now gives me an unexpected error:

    OLE.xs:925:17: fout: formaat is geen stringconstante en er zijn geen f +ormaat-argumenten [-Werror=format-security] warn(SvPVX(sv));

    As my cygwin64 is in dutch, it reads: error: format is no string constant and there are no format arguments.

    Any ideas on how to proceed? Do I log a bug in cpan for this module or is there a depency on some sort of cygwin library that I am missing?

    --
    if ( 1 ) { $postman->ring() for (1..2); }
Memoize problem with old Perl
1 direct reply — Read more / Contribute
by Anonymous Monk
on Sep 20, 2018 at 23:28
    #!/usr/bin/perl # SSCCE from examples at "perldoc Memoize" # Memoize works as expected on recent Perls 5.20-28. # Builds huge cache and does not work on 5.18. # Thoughts on why, and how to handle it? use strict; use warnings; use diagnostics; use Data::Dumper; use DB_File; use File::Spec; use integer; use Memoize; use POSIX (); my $title = 'Compute Fibonacci numbers'; my $tempd = File::Spec->tmpdir; my $tempf = $] . '_memoize_fib'; my $cache = File::Spec->catfile($tempd,$tempf); my $ARGS = @ARGV ? shift : 8; unlink $cache if -e $cache and $ARGS eq 'purge'; my $db = tie my %cache => 'DB_File', $cache, POSIX::O_RDWR|POSIX::O_CR +EAT, 0666; memoize 'fib', SCALAR_CACHE => [HASH => \%cache]; print "Perl $] Memoize $Memoize::VERSION Tempdir $tempd Tempfile $tempf Filesize ", -s $cache, " bytes Question $ARGS Answer ", fib($ARGS), "\n\n", # Because Data::Dumper panics on multigig dumps with # panic: sv_setpvn called with negative strlen -8260 eval { Data::Dumper->Dump([\%cache],[('%cache')]) }; unlink $cache if $@; print "$title\nUsage: $0 n, $0 purge\n"; sub fib { my $n = shift; return $n if $n < 2; fib($n-1) + fib($n-2); }

    Perl 5.018002 Memoize 1.03 Tempdir /tmp Tempfile 5.018002_memoize_fib Filesize 146800640 bytes Question 8 Answer 21 $%cache = { '0' => undef, '1' => undef, '2' => undef, '3' => undef, '4' => undef, '5' => undef, '6' => undef, '7' => undef };
    Perl 5.020003 Memoize 1.03 Tempdir /tmp Tempfile 5.020003_memoize_fib Filesize 49152 bytes Question 8 Answer 21 $%cache = { '0' => '0', '2' => '1', '4' => '3', '6' => '8', '1' => '1', '3' => '2', '5' => '5', '7' => '13' };
    Perl 5.028000 Memoize 1.03_01 Tempdir /tmp Tempfile 5.028000_memoize_fib Filesize 49152 bytes Question 8 Answer 21 $%cache = { '0' => '0', '2' => '1', '4' => '3', '6' => '8', '1' => '1', '3' => '2', '5' => '5', '7' => '13' };
WWW::Mechanize::Chrome How to close a tab
1 direct reply — Read more / Contribute
by fowibotak
on Sep 20, 2018 at 22:21
    Hello, I can't seem to figure out how to close a tab when using WWW::Mechanize::Chrome.
    my $mech = WWW::Mechanize::Chrome->new(); $mech->get('https://www.google.com/');
    There is no $mech->close_tab() function. So the question is, how do you close the tab you're currently working with? Thanks
First Web Crawl Task
2 direct replies — Read more / Contribute
by bennierounder
on Sep 20, 2018 at 18:28

    Hi guys,

    I'm very frustrated with this code

    #!/usr/bin/perl -w # a simple web crawler use strict; use LWP::Simple; my $url = shift || die 'Please provide an initial url after filename!' +; my $max = 10; my $html = get($url); my @urls; while ($url =~ s/(https:\/\/\S+)[">]//) { push @urls, $1; print @urls; } mkdir "web" , 0755; open (URLMAP, ">", "web/url.map" ) || die ("can't open web\/url.map\n" +); my $count = 0; for (my $i=0; $i<$max; $i++) { my $source = $urls[int(rand($#urls+1))]; getstore($source, 'web/$count.html'); print URLMAP "$count\n$source\n"; $count++; } close URLMAP;
    1,17 Top

    I run the script, perl web_crawl.pl https://www.money.co.uk and I get this!

    perl web_crawl.pl https://www.google.com
    Use of uninitialized value $source in concatenation (.) or string at web_crawl.pl line 27.
    Use of uninitialized value $source in concatenation (.) or string at web_crawl.pl line 27.
    Use of uninitialized value $source in concatenation (.) or string at web_crawl.pl line 27.
    Use of uninitialized value $source in concatenation (.) or string at web_crawl.pl line 27.
    Use of uninitialized value $source in concatenation (.) or string at web_crawl.pl line 27.
    Use of uninitialized value $source in concatenation (.) or string at web_crawl.pl line 27.
    Use of uninitialized value $source in concatenation (.) or string at web_crawl.pl line 27.
    Use of uninitialized value $source in concatenation (.) or string at web_crawl.pl line 27.
    Use of uninitialized value $source in concatenation (.) or string at web_crawl.pl line 27.
    Use of uninitialized value $source in concatenation (.) or string at web_crawl.pl line 27.

    I'm trying to eventually get the prices and company names, so for example for this part of the site https://www.money.co.uk/travel-money/japanese-yen-exchange-rate.htm I want to get the prices on offer into an array in order (highest first), maybe keeping a note of the company name so may need a hash or array of hashes.
    That's the end goal, but stuck on the first hurdle, which is viewing the sites html in files where i can search the prices, then extract them from the files!!! If you can think of a better way and point me in the right direction on finding the solution, I'm all ears! Thanks in advance!

    Please help!

returning to the outer loop
4 direct replies — Read more / Contribute
by Anonymous Monk
on Sep 20, 2018 at 17:03

    This is greatly simplified - I hope it makes sense. :)

    I am trying to loop over rows of an array doing a test. If the test fails, I want to go all the way back to the outer loop, which the documentation suggests will work only with foreach.

    #!/usr/bin/perl -w use strict; my @x = ( ['aaaaa', 'bbbbb', 'ccccc', 'ddddd',], ['eeeee', 'fffff', 'ggggg', 'hhhhh',], ['iiiii', 'jjjjj', 'kkkkk', 'lllll',], ); for my $i (1 .. 1000) { for my $a (@x) { my $ifails = 0; for my $j (0 .. (scalar @$a) - 1 ) { <get external data string for pattern matching here, put i +n $c> if ($c =~ /$a->[$j]/) { $ifails++; } } if ($ifails > 1) { want to go to outer loop here, and not proc +ess the next (and subsequent) row(s) } } }

    Is there some way of doing that? Note that in the actual application, I do have more more processing below the inner loop, so a "last" statement at the test doesn't work.

    Thank you in advance.

Replace strings in text file
4 direct replies — Read more / Contribute
by TonyNY
on Sep 20, 2018 at 12:28

    Hi,

    I'm trying to replace strings in a text file but cannot get it to work using the following code:

    system("sed -i -e 's/The action failed./failed_build/g' $lookuptxtfile +");

    Any help modifying this code or using a better way to accomplish this would be greatly appreciated.

I need outputting the results of a query in a formatted way to a scalar, so that I can email the results
1 direct reply — Read more / Contribute
by gandolf989
on Sep 20, 2018 at 11:10
    I am writing a perl script to take SQL stats for queries in my Oracle database and output the results in a formatted way to a scalar, so that I can send an email. I found this format syntax, but I am not sure if it is still supported or if I am using it correctly. I just want to make the formatting easy to read, so that I can send it to the developers and they can see the worst SQL running in the Oracle database. Is there another way that I should write this code? Or do I just need to fix the syntax.
    sub print_query_results { my $dbh = shift; my $sql_query = shift; my $sql = SQL::Beautify->new; my ( $sql_id, $min_last_load_time, $buffer_gets, $disk_reads, $exec +utions, $sorts, $parse_calls, $sql_fulltext ); my $return_scalar = qq{ }; my $sth = $dbh->prepare( $sql_query ); $sth->execute(); $sth->bind_columns( undef, \$sql_id, \$min_last_load_time, \$buffer +_gets, \$disk_reads, \$executions, \$sorts, \$parse_calls, \$sql_full +text ); while( $sth->fetch() ) { format OUTPUT = @|||||||||||||||@||||||||||||||||||||@|||||||||||||||@|||||||||||||||@ +|||||||||||||||@|||||||||||||||@||||||||||||||| 'SQL_ID', 'min_last_load_time','buffer_gets', 'disk_reads', ' +executions', 'sorts', 'parse_calls', $sql_id, $min_last_load_time, $buffer_gets, $disk_reads, $ +executions, $sorts, $parse_calls, @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $sql->query( $sql_fulltext ) . open OUTPUT, '>', \$return_scalar; write OUTPUT; close OUTPUT; } return $return_scalar; }
$var == 1 fails when $var = 1
4 direct replies — Read more / Contribute
by mjlush
on Sep 20, 2018 at 11:01

    I've found certain triplets of numbers that added up and put in a variable are, equal to 1, print as 1, are true on looks_like_number, match 1 when evaluated with eq but do not match 1 using using ==.

    The order the numbers are added matters, the script below produces output in the form.

    0.688 + 0.289 + 0.023
    total is 1
    looks like a number
    fails on ==
    matches on eq
    
    0.688 + 0.023 + 0.289
    total is 1
    looks like a number
    matches on ==
    matches on eq
    
    0.559 + 0.380 + 0.061
    total is 1
    looks like a number
    matches on ==
    matches on eq
    
    #!/usr/bin/perl
    use strict;
    use warnings;
    use Scalar::Util qw(looks_like_number);
    
    
    while (<DATA>) {
        if (m{^#}) {
    	print;
    	next;
        }
        chomp;
        my ($x, $y, $z) = split(m{ });
    #   my $var = $x + $y + $z;
        my $var = $x;
        $var += $y;
        $var += $z;
        
        print "$x + $y + $z\n";
        print "total is $var\n";
        if (looks_like_number($var)) { 
    	print "looks like a number\n";
        }
        else {
    	print "doesn't look like a number\n"
        }
        if ($var == 1) {
    	print "matches on ==\n";
        }
        else {
    	print "fails on ==\n";
        }
        if ($var eq 1) {
    	print "matches on eq\n";
        }
        else {
    	print "fails on eq\n"
        }
        print "\n";
    }
    
    __DATA__
    #FAIL
    0.688 0.289 0.023
    0.500 0.422 0.078
    0.693 0.290 0.017
    0.207 0.563 0.230
    0.491 0.421 0.088
    0.498 0.420 0.082
    0.696 0.285 0.019
    0.693 0.286 0.021
    0.517 0.409 0.074
    # ORDER CHANGED 
    0.688 0.023 0.289
    0.422 0.078 0.500
    # PASS
    0.559 0.380 0.061
    0.648 0.314 0.038
    0.546 0.414 0.040
    0.600 0.348 0.052
    0.653 0.311 0.036
    0.741 0.245 0.014
    0.787 0.201 0.012
    0.651 0.318 0.031
    0.627 0.331 0.042
    

Add your question
Title:
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?
    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 lurking in the Monastery: (4)
    As of 2018-09-22 05:06 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Eventually, "covfefe" will come to mean:













      Results (190 votes). Check out past polls.

      Notices?
      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!