Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
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
regex not matching how I want it to :(
2 direct replies — Read more / Contribute
by glwa
on Oct 18, 2018 at 15:27
    usually I managed with regexes myself, but this time I am lost, so I decided to ask wiser people the question is why the following code:
    while ( $line=~/<a href=\"(.*?)\.htm\">/ig ) {
    on $line being
    <a href="test1.htm"> test1</a><br> <a href="test2.htm"> test2</a><br> +<a href="test3.htm"> test3</a><br>
    is matching
    test1"> test1</a><br> <a href="test2
    and not just
    test2
Runs from Command Line but NOT from Perl
3 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 18, 2018 at 11:24
    I have this code that prints out a command and also runs it from Perl. When I run the curl command from Perl the remote server, queried from the curl request, complains about an HTTP 400 error.

    When I run the same command, copied from the Perl print statement, it runs fine and returns a JSON string.

    This behavior happens on Linux and Windows with Strawberry Perl. Any ideas?

    Here is the code:
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $command = <<'END'; curl 'https://tax-calculator-api.taxfoundation.org/taxcalc/tcja_submit +' -H 'Origin: https://taxfoundation.org' -H 'Accept-Encoding: gzip, d +eflate, br' -H 'Accept-Language: en-US,en;q=0.9' -H 'User-Agent: Mozi +lla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like + Gecko) Chrome/69.0.3497.100 Safari/537.36' -H 'content-type: applica +tion/json' -H 'Accept: */*' -H 'Referer: https://taxfoundation.org/20 +18-tax-reform-calculator/' -H 'Connection: keep-alive' --data-binary +$'{"filing_status": 1, "child_dep": 2, "nonchild_dep": 0, "ordinary_i +ncome1": 150000, "ordinary_income2": 0, "business_income": 0, "ss_inc +ome": 0, "qualified_income": 0, "401k_contributions": 0, "medical_exp +enses": 0, "sl_income_tax": 0, "sl_property_tax": 0, "interest_paid": + 0, "charity_contributions": 0, "other_itemized": 0}' --compressed END print $command; my $data = `$command` or die "$!\n"; print Dumper $data;
Loop through all directory and files and lines
2 direct replies — Read more / Contribute
by MissPerl
on Oct 18, 2018 at 09:25
    Hi fellow monks! CODE EDITED

    I want to go into first directory/i/j/*, then read all the files and all the lines inside every file. Eg, /somewhere/1/2/*

    Currently the script gave me no error, but the output was surprisingly all from the (else condition).

    I couldnt make sure, since there are a lot of files inside it, but there should at least have some cases match the (if ) condition.

    Can anyone have a look at the script i wrote and tell me if anything i have overlooked ?

    Each file, I want to find if any three of the strings exists, if it does, I'll store its filepath in store_location.

    Also the (i, j), was from an array with even number contents, odd number will be i; while even is j.
    # !/usr/bin/perl use strict; use warnings; use File::Glob 'bsd_glob'; my $store_location = '/path/to/file'; my $Dir1 = "/somewhere"; my $Dir2 = "/somewhereelse"; my @first_directory = ( bsd_glob("$Dir1/"), bsd_glob("$Dir2/") ); my @store_array = qw (1 2 3 4 5 6); foreach my $first (@first_directory){ while(my ($i,$j) = splice(@store_array,0,2)){ my $second_directory = "$first/$i/$j"; if (-e $second_directory and -d $second_directory){ my @third_directory = bsd_glob("$first/$i/$j/*"); foreach my $file (@third_directory){ open(FILE, "<" , $file) or die "Can't open file '$file +': $!"; while (<FILE>){ if (($_ =~ /TbhODK/) or ($_ =~ /octuov/) or ($_ =~ + /qas_uop/)) { open(my $filehand, '>>', $store_location) or d +ie "Fail to open file '$store_location' $!"; print $filehand "$first/$i/$j/$file \n"; close $filehand; } } } close FILE; } else{ open(my $filehand, '>>', $store_location) or die "Fail to +open file '$store_location' $!"; print $filehand "$first/$i/$j (fail to exist)\n"; close $filehand; } } }#first_directory
    Kindly let me know if it's unclear,I will improve the sentence.thank you perlmonks !

Date::Calc in years?
3 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 18, 2018 at 05:13
    Hi Monks!
    My code below:
    use strict; use warnings; use Date::Calc qw(:all); my $date_birth = '1999-12-13'; my $date_death = '2080-05-21'; my @array_birth = split(/-/, $date_birth); my @array_death = split(/-/, $date_death); my $dd = Delta_Days(@array_birth, @array_death); my $years_alive = sprintf("%.1f", $dd/365); print $years_alive,"\n";
    works ok I would say, but I guess it is not 100% accurate (since I assume years of 365 days and I do not take into account months and days of birth and death.
    Can you help me fix it?
how to push multiples row of values into hash and do comparison
5 direct replies — Read more / Contribute
by darkmoon
on Oct 18, 2018 at 02:11

    Hi, I am new to perl. I have two files, I need to do comparison to find out the matching and non-matching data. I got two problems now: Question 1: one of my hashes can only capture the 2nd row of the 'num', i tried to use `push @{hash1{name1}},$x1,$y1,$x2,$y2` , but it still returning the 2nd row of the 'num'.

    File1 :
    name foo num 111 222 333 444 name jack num 999 111 222 333 num 333 444 555 777
    File2:
    name jack num 999 111 222 333 num 333 444 555 777 name foo num 666 222 333 444
    This is my code:
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $input1=$ARGV[0]; my $input2=$ARGV[1]; my %hash1; my %hash2; my $name1; my $name2; my $x1; my $x2; my $y2; my $y1; open my $fh1,'<', $input1 or die "Cannot open file : $!\n"; while (<$fh1>) { chomp; if(/^name\s+(\S+)/) { $name1 = $1; } if(/^num\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) { $x1 = $1; $y1 = $2; $x2 = $3; $y2 = $4; } $hash1{$name1}=[$x1,$y1,$x2,$y2]; } close $fh1; print Dumper (\%hash1); open my $fh2,'<', $input2 or die "Cannot open file : $!\n"; while (<$fh2>) { chomp; if(/^name\s+(\S+)/) { $name2 = $1; } if(/^num\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) { $x1 = $1; $y1 = $2; $x2 = $3; $y2 = $4; } $hash2{$name2}=[$x1,$y1,$x2,$y2]; } close $fh2; print Dumper (\%hash2);
    My output:
    $VAR1 = { 'jack' => [ '333', '444', '555', '777' ], 'foo' => [ '111', '222', '333', '444' ] }; $VAR1 = { 'jack' => [ '333', '444', '555', '777' ], 'foo' => [ '666', '222', '333', '444' ] };
    My expected Output:
    $VAR1 = { 'jack' => [ '999', '111', '222', '333', '333', '444', '555', '777' ], 'foo' => [ '111', '222', '333', '444' ] }; $VAR1 = { 'jack' => [ '999', '111', '222', '333', '333', '444', '555', '777' ], 'foo' => [ '666', '222', '333', '444' ] };

    Question 2: I tried to use this foreach loop to do the matching of keys and values and print out in a table format. I tried this :

    print "\t\tFIle1\t\t\t\t\tFile2\n"; print "Name\tX1\tY1\tX2\tY2\t\t\tX1\tY1\tX2\tY2\n"; foreach my $k1(keys %hash1) { foreach my $k2(keys %hash2) { if($hash1{$name} eq $hash2{$name2}) { if($hash1{$x1}{$y1}{$x2}{$y2} == $hash2 +{$x1}{$y1}{$x2}{$y2}) { print "$name\$x1\$y1\$x +2\$y2\n"; } } } }

    but Im getting the header only.

    File1 File2 Name X1 Y1 X2 Y2 X1 Y1 X2 Y2
    my desired output for matching :
    File1 File2 Name x1 y1 x2 y2 x1 y1 x2 y2 jack 999 111 222 333 999 111 222 333 333 444 555 777 333 444 555 777
    Any help?
Is use of a simple DSL for a configuration a good idea?
2 direct replies — Read more / Contribute
by nysus
on Oct 17, 2018 at 20:30

    I've been studying the Rex module. Rex allows you to manage remote servers over ssh using a configuration file. It uses a crude "DSL," or "domain specific language" for adding tasks through a configuration file. Here's a sample of the configuration file:

    set user => "root"; group "frontend" => "server1", "server2", "server3", "server4"; group "local" => "mango", "debian01"; desc "Show uptime"; task "uptime", group => "local", sub { run "uptime"; }; desc "Show free space"; task "show_disk_free", sub { run "df -h"; }; desc "Upload file"; task "upload", "mango", sub { upload "/tmp/test.txt", "/tmp/test.txt"; };

    The idea behind this DSL is straightforward. Each keyword is a call to a function and the arguments are separated by commas. I'm not even sure if this is the true definition of a DSL but that's what they call it in their documentation.

    Anyway, I've never seen anything like this before and I'm wondering if there are any advantages to this approach to a configuration file. Are there any big advantages/disadvantages to this approach? And why not just use something more universal like JSON? My experience with config files is limited to writing my own Simple::Config files and .ini files with Dist::Zilla. So I'm interested in learning more about best practices for setting up config files. Thanks.

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

Follow JavaScript link
2 direct replies — Read more / Contribute
by bachoA4o
on Oct 17, 2018 at 15:22
    Hello , I'm making simple scraper that follows JavaScript generated links. I was reading for WWW::Scripter but could not manage to use it (I'm still newbie in perl ). I want to to scrape the links of the web page and follow a given link then download the page and so on. My progression until now : I'm able to download the HTML and all the links of a given page with WWW::Mechanize. I`ll be glad if someone write some sample code and give a some explanation for the WWW::Scripter module
How can you get normal log output when testing log output with Test::Output?
1 direct reply — Read more / Contribute
by nysus
on Oct 17, 2018 at 14:52

    I'm using Test::Output to test the output of a module generated by Log::Log4perl. The tests work, but the output of Log::Log4perl is getting suppressed. I can use redirection to get the logs to show on the screen, but then the tests fail. I'm out of random things to try at this point and hoping some a Monk can steer me right. Here is a sample test:

    stderr_like { Spin->run; } qr/Loading Spinfile from module/i, 'loads module spinfile';

    The above passes. But if I change it to:

    stdout_like { Spin->run; } qr/Loading Spinfile from module/i, 'loads module spinfile';
    I get the following, which prints the logs, but fails the test:
    # Running my tests [INFO ] No Spinfile given in command, looking in current directory. + Spin.Command: 26 [INFO ] No Spinfile found in the current directory, looking in module +share directory. Spin.Command: 33 [INFO ] Loading Spinfile from module share directory + Spin.Command: 36 not ok 1 - loads module spinfile # Failed test 'loads module spinfile' # at t/02-command.t line 31. # STDOUT: # # doesn't match: # (?^i:Loading Spinfile from module) # as expected

    I've tried just about everything. The combined_like function does not help. And I've tried all kinds of different way of redirecting stderr/stdin to the point of exhaustion. I'm sure there's probably something simple I can do. Thanks.

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

email address as username
1 direct reply — Read more / Contribute
by nateh
on Oct 17, 2018 at 12:18

    I'm having trouble with my perl script as the username in my script is an email address with the @ symbol and it is throwing an error. For instance:

    $user = 'first.last@somewhere.com';

    I'm unable to change the username to be something other than an email and therefore remove the @ symbol. Is there a way to keep such a username and get it to work in perl? Thanks

Library for realtime graphs in perl.
4 direct replies — Read more / Contribute
by tobbes
on Oct 17, 2018 at 07:58
    I am not able to find anything to suite my needs. The program needs to recieve data in realtime and plot them on a graph. Idealy the graph should also be interactive, you should be able to click on it and set listeners. I have found a very good module in python here https://matplotlib.org/ but unfortunately the script I am going to extend is written in perl. I have also tried to install Inline::Python without success as I am running windows 10. Does anyone know of a perl library resembling matplotlib?

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 chanting in the Monastery: (7)
    As of 2018-10-19 00:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      When I need money for a bigger acquisition, I usually ...














      Results (106 votes). Check out past polls.

      Notices?