Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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
parse string into multidimensional hash
2 direct replies — Read more / Contribute
by chickenman
on Feb 20, 2018 at 02:58
    Hi there,
    i need your help. I am trying to parse the following output.

    Configuration - DGCONF Protection Mode: MaxAvailability Databases: DB1 - Primary database DB2 - Physical standby database Properties: FastStartFailoverThreshold = '30' OperationTimeout = '30' FastStartFailoverLagLimit = '30' CommunicationTimeout = '180' ObserverReconnect = '0' FastStartFailoverAutoReinstate = 'TRUE' FastStartFailoverPmyShutdown = 'TRUE' BystandersFollowRoleChange = 'ALL' ObserverOverride = 'FALSE' ExternalDestination1 = '' ExternalDestination2 = '' PrimaryLostWriteAction = 'CONTINUE' Fast-Start Failover: DISABLED Configuration Status: SUCCESS
    As a result i need a hash like this
    $VAR2 = { 'Configuration' => 'DGCONF', 'ProtectionMode' => 'MaxAvailability', 'Databases' => { 'DB1' => 'Primarydatabase', 'DB2' => 'Physicalstandbydatabase' }, 'Properties' => { 'ExternalDestination2' => '', 'FastStartFailoverThreshold' => '30', 'ObserverReconnect' => '0', 'ObserverOverride' => 'FALSE', 'OperationTimeout' => '30', 'PrimaryLostWriteAction' => 'CONTINUE', 'FastStartFailoverAutoReinstate' => 'TRUE', 'BystandersFollowRoleChange' => 'ALL', 'FastStartFailoverPmyShutdown' => 'TRUE', 'ExternalDestination1' => '', 'FastStartFailoverLagLimit' => '30', 'CommunicationTimeout' => '180' }, 'ConfigurationStatus' => 'SUCCESS' };
    Please help!
It's borken, whom do I tell
2 direct replies — Read more / Contribute
by mpersico
on Feb 20, 2018 at 00:15
    I am using homebrew to try and install Perl 5.26 on Cygwin and Microsoft's WSL, Ubuntu flavor. Both builds failed. The WSL had test failures, which isn't that bad, but during the tests, a debugger was started and that paused the build until I interacted with it. Cygwin has compile errors. Who gets the logs? Cygwin and Microsoft? The Homebrew folks? Do I report as an RT entry? Thanks
Date::Parse - how to correctly parse dates between 1901 and 1969
6 direct replies — Read more / Contribute
by eniad
on Feb 19, 2018 at 14:29

    I am parsing dates and datetimes input by users who aren't too careful with their formatting. Date::Parse seems great because it handles most cases I need to handle.

    Except datetimes between 1901-01-01 00:00:00 and 1968-12-31 23:59:59, as I found out today. For those datetimes, Date::Parse str2time adds an extra 100 years when it parses the datetime to epoch time.

    Here is the code I am using to parse the datetimes:

    #!/usr/bin/perl #--------------------------------------------------------------------- # # # format variable date inputs #--------------------------------------------------------------------- use strict; use warnings; use Date::Parse; use DateTime; my $DEFAULT_TIME_ZONE = "GMT"; my @dates = ( "1899-06-24 09:44:00", "1900-12-31 23:59:59", "1901-01-01 00:00:00", "1960-12-31 23:59:59", "1966-06-24 09:44:00", "1968-12-31 23:59:59", "1969-01-01 00:00:00", "1969-12-31 23:59:59", "1970-01-01 00:00:01", "2000-01-01 00:00:00", "2017-06-24 23:59:59", "2018-06-24 09:44:00", "2238-06-24 09:44:00" ); foreach my $string (@dates) { # format datetime field from any valid datetime input # default time zone is used if timezone is not included in string my $epoch = str2time( $string, $DEFAULT_TIME_ZONE ); # error if date is not correctly parsed if ( !$epoch ) { die("ERROR ====> invalid datetime ($string), " . "datetime format should be YYYY-MM-DD HH:MM:SS"); } my $date = DateTime->from_epoch( epoch => $epoch ); printf( "formatting datetime: value = %20s, epoch = %20u, " . "date = %20s\n", $string, $epoch, $date ); } exit 0;

    Side note: I need to improve my error handling because the valid date 1970-01-01 00:00:00 will throw an error.

    The additional 100 years for dates between 1901 and 1969 can be seen in the output:

    formatting datetime: value = 1899-06-24 09:44:00, epoch = 18446744071 +484095456, date = 1899-06-24T09:44:00 formatting datetime: value = 1900-12-31 23:59:59, epoch = 18446744071 +532098815, date = 1900-12-31T23:59:59 formatting datetime: value = 1901-01-01 00:00:00, epoch = +978307200, date = 2001-01-01T00:00:00 formatting datetime: value = 1960-12-31 23:59:59, epoch = 2 +871763199, date = 2060-12-31T23:59:59 formatting datetime: value = 1966-06-24 09:44:00, epoch = 3 +044598240, date = 2066-06-24T09:44:00 formatting datetime: value = 1968-12-31 23:59:59, epoch = 3 +124223999, date = 2068-12-31T23:59:59 formatting datetime: value = 1969-01-01 00:00:00, epoch = 18446744073 +678015616, date = 1969-01-01T00:00:00 formatting datetime: value = 1969-12-31 23:59:59, epoch = 18446744073 +709551615, date = 1969-12-31T23:59:59 formatting datetime: value = 1970-01-01 00:00:01, epoch = + 1, date = 1970-01-01T00:00:01 formatting datetime: value = 2000-01-01 00:00:00, epoch = +946684800, date = 2000-01-01T00:00:00 formatting datetime: value = 2017-06-24 23:59:59, epoch = 1 +498348799, date = 2017-06-24T23:59:59 formatting datetime: value = 2018-06-24 09:44:00, epoch = 1 +529833440, date = 2018-06-24T09:44:00 formatting datetime: value = 2238-06-24 09:44:00, epoch = 8 +472332640, date = 2238-06-24T09:44:00

    The Date::Parse documentation suggests it can handle dates at least as old at 1901-01-01. The Time::Local documentation suggest it should be able handle dates even older.

    How should I handle this oddity? Is there a better way to parse variable input formats?

Copying files from one file to another file
4 direct replies — Read more / Contribute
by harishnv
on Feb 19, 2018 at 11:25

    I have an array where each each array is in this format "path of the file :number: content in one line". i want to read each line and create different files(.txt) in one directory. If the path is same, i will append it to the old file else create a new file?

    input : example it's in the format of file path: line number : comment a[0]-tests/right/case1: 12 : //comment a[1]-tests/right/case1: 13 : //comment a[2]-test/right/case3: 5 : //comment

    expected output :

    in each file there should be the same path and the whole line should be printed. If the path is different then i should create a new file.

Failed to find share dir for dist 'Dancer2'
2 direct replies — Read more / Contribute
by markong
on Feb 19, 2018 at 07:13
    Hello, I've cloned the Dancer2 official git repo and now I'm trying to setup a dev environment (inside a perlbrew local lib) and I'm stuck at this command, receiving back:
    Failed to find share dir for dist 'Dancer2' at lib/Dancer2/CLI/Command/ line 62.
    which I guess is to be expected seen that I haven't installed the 'Dancer2' dist from CPAN and File::ShareDir::dist_dir('Dancer2') will fail.

    I'm trying to execute the dancer script from the cloned checkout code base, in order to setup a test dancer2 project, but it seems that it is not something supported?!

    I'm wondering if anybody has experience with the code base and can suggest something. Is the Dancer2 distro installation from CPAN mandatory to execute the 'dancer2' script or else how do you call that 'script/dancer2' bin around that share dir check while developing?

hey how to recursively access the directroies to search for a keyword and print its file name and the keyword line?
4 direct replies — Read more / Contribute
by harishnv
on Feb 18, 2018 at 06:11

    Nothing to see here folks - harishnv provided no content. (This comment provided by GrandFather.)

creating and managing many hashes
7 direct replies — Read more / Contribute
by Gtforce
on Feb 18, 2018 at 02:18

    My data is as follows:

    2018-01-01 apple 200 50000 2018-01-02 apple 201 60000 2018-01-03 apple 202 70000 2018-01-04 apple 198 80000 2018-01-01 orange 400 30000 2018-01-02 orange 401 35000 2018-01-03 orange 402 36000 2018-01-04 orange 405 28000

    where apple and orange are my products, and I have over 2,000 distinct products. The values 201,202, etc are the prices of those products on the respective dates represented in the first column (and the quantities aka inventories are in the last column). My data series is for the past 3 years for each of these products.

    I need to pass the data series (i.e., price and inventory) for one product at a time to a subroutine that calculates the mean,, etc.

    I also need to pass the data series for a combination of two products at a time (a pair) to a subroutine that calculates the correlation between the two data series.

    I have currently done this using arrays over the 2 million pairs that arise from 2,000 products and it ran for 4 days before my patience ran out and I terminated the process. I've only just started reading up on hashes and I think I can speed up things if I get the data series for one product into one hash, and the data series for another product into another hash (likewise for 2,000 products or hashes).

    The reading I've done so far warns me against using variables for hashnames. Any advice you can offer would be greatly appreciated, thanks.

Most direct way of loading XS?
3 direct replies — Read more / Contribute
by YenForYang
on Feb 17, 2018 at 20:59

    Recently I've been looking into what XSLoader and DynaLoader and I've been trying to figure out what they do behind the scenes so that maybe I can do (what they do) ("low-level") if I so wish (for whatever reason). It seems to me that just uses DynaLoader--it's a convenient module but using it to load XS seems like just an extra step. Well I've looked at DynaLoader as well, and I'm lost on how it does what it does. It implements functions like dl_load_file, dl_install_xsub, and dl_find_symbol but I can't seem to find any definitions. So I'm guessing they are defined in XS or something...but if the subroutines for loading XS are defined in XS, how does DynaLoader manage to do anything? How do I represent one-way dependency?
1 direct reply — Read more / Contribute
by jkeenan1
on Feb 17, 2018 at 19:45

    I am attempting to use the Graph module from CPAN for the first time. I am having trouble understanding how the module implements the concept of a "directed graph".

    Suppose I have 3 nodes, "Jolt", "Lord", and "Tornado". I want to indicate that "Tornado" depends on each of "Jolt" and "Lord". Based on the Graph distribution's t/06_new.t and t/18_add_edge.t, I believe I can say that with the following program:

    $ cat graph.t #!/usr/bin/env perl use strict; use warnings; use Graph; use Test::More qw( no_plan ); my @vertices = qw( Jolt Lord Tornado ); my $g = Graph->new(vertices => [ @vertices ]); ok(defined $g, "Constructor returned defined value"); isa_ok($g, 'Graph'); for my $v (@vertices) { ok($g->has_vertex($v), "vertex $v"); } my @edges = ( [ 'Tornado' => 'Jolt' ], [ 'Tornado' => 'Lord' ], ); for my $e (@edges) { ok($g->add_edge(@{$e}), sprintf("Add edge %-12s -> %s", @{$e})); }
    When I run this, I get this output:
    $ prove -v graph.t graph.t .. ok 1 - Constructor returned defined value ok 2 - An object of class 'Graph' isa 'Graph' ok 3 - vertex Jolt ok 4 - vertex Lord ok 5 - vertex Tornado ok 6 - Add edge Tornado -> Jolt ok 7 - Add edge Tornado -> Lord 1..7 ok All tests successful. Files=1, Tests=7, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.11 cusr + 0.00 csys = 0.13 CPU) Result: PASS
    So far so good. But to me "depends on" means that I should not be able to add an edge from "Jolt" to "Tornado" or from "Lord" to "Tornado" because I've already established the other direction for those edges. So my expectation is that, I when I add the following unit test:
    ok(! $g->add_edge( 'Lord' => 'Tornado '), "I expect that I cannot add Lord -> Tornado edge");
    ... the test will PASS because it is phrased in the negative. Instead, the test FAILs:
    $ prove -v graph.t graph.t .. ok 1 - Constructor returned defined value ok 2 - An object of class 'Graph' isa 'Graph' ok 3 - vertex Jolt ok 4 - vertex Lord ok 5 - vertex Tornado ok 6 - Add edge Tornado -> Jolt ok 7 - Add edge Tornado -> Lord not ok 8 - I expect that I cannot add Lord -> Tornado edge # Failed test 'I expect that I cannot add Lord -> Tornado edge' # at graph.t line 23. 1..8 # Looks like you failed 1 test of 8. Dubious, test returned 1 (wstat 256, 0x100) Failed 1/8 subtests Test Summary Report ------------------- graph.t (Wstat: 256 Tests: 8 Failed: 1) Failed test: 8 Non-zero exit status: 1 Files=1, Tests=8, 0 wallclock secs ( 0.01 usr 0.01 sys + 0.11 cusr + 0.00 csys = 0.13 CPU) Result: FAIL

    So, what am I not grokking about Graph and directed graphs? Is it possible to implement what I want with Graph, or is some other module better for this?

    Thank you very much.

    Jim Keenan
Redirecting Output to shell command from within perl
4 direct replies — Read more / Contribute
by dracut
on Feb 16, 2018 at 19:32
    Hey guys, not sure if this has been answered here before, but I was looking to feed less a string stored within a variable $output.
    Essentially I'm trying to do the following from within the script:


    Any help would be greatly appreciated!

Add your question
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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (3)
    As of 2018-02-24 18:34 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (310 votes). Check out past polls.