Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
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
$0 in perl pod, not expanding to script name
2 direct replies — Read more / Contribute
by spatterson
on Jul 16, 2018 at 03:48

    I have a perl script containing some brief documentation in pod format. Normally (or at least outside of perlpod) $0 is the script name, but in perlpod, $0 is output literally as '$0'.

    Is there any way I can dynamically access the current script name inside perlpod? I'm returning to perl after a few years on PHP & can vaguely remember doing something similar in the past.

Is this a valid approach to finding if a path through a set of points has completed?
3 direct replies — Read more / Contribute
by atcroft
on Jul 16, 2018 at 03:26

    (I considered submitting this as a meditation, but due to my lack of knowledge on the topic, I thought better of posting there.)

    Recently I was thinking about a problem. Specifically, I was considering the idea from the point of view of "ants" (for lack of a better term) following all of the possible paths, and trying to think through how to determine if a path has been completed. As a starting thought experiment, I considered 6 points, with ants moving from each point to each remaining point. I thought of 5 different cases that could occur (points labeled '1'..'6', paths written ordered least to greatest):

    1. Incomplete connection - existing connections are 1-2 and 2-3.
    2. Incomplete connection - existing connections are 1-2, 2-3, 4-5, 5-6, and 1-6
    3. Incomplete connection - existing connections are 1-2, 2-3, 4-5, 5-6, and 1-6, extra connection 4-6
    4. Complete connection - existing connections are 1-2, 2-3, 3-4, 4-5, 5-6, and 1-6
    5. Complete connection - existing connections are 1-2, 2-3, 3-4, 4-5, 5-6, and 1-6, extra connections 3-5, 3-6, and 4-6

    The cases map out (roughly) as follows:

    (I realized as I was writing this that being able to find that a path might not be as useful as I thought, but that does not take *that much* away from this question.)

    I'm not aware of (or at least remember) dealing with graphs in the CS classes I took (years ago), so there may be a nice theory or approach I am not aware of. What I came up with was to create a matrix containing the number of connections between between points. (By writing all of the connections in least-greatest ordering, only half the matrix had to be used, as illustrated by the following. Unfilled entries are noted as '-', otherwise the count of connections is filled in in row-column order.)

    What I noticed was that in the cases (1-3) where a connection did not exist, there was at least one row in which the sum of entries on the row was zero, but in cases where a full path existed all rows had a non-zero sum. Is this approach too simplistic-minded (or did I just stumble upon something I should have known)?

    Sample code:

    Thank you for your attention and insights. (And my apologies if I have wasted your time.)

    Update: 2018-07-16

    Thank you for your feedback. To answer OM and tobyink, yes, apparently what I am looking for is a Hamiltonian path through the set. (I didn't know the proper term(s) to use to search, among other things.) To answer bliako, yes, I know ants would have started from each point, but for simplicity I showed only completed paths of equal length. To apply this to the original problem, I can see two ways: a) follow the idea of an actual ant, and track each ant's actual position, or b) knowing the edges and their lengths, I would probably look to move down the list of all edges (tracking the sum total) and update the matrix form (above, or other method) to check if a complete path exists.

waiting for process to finish
1 direct reply — Read more / Contribute
by ovedpo15
on Jul 16, 2018 at 02:13
    Hey guys.
    Consider a function (lets call it A) which returns 1 if a process finished running, otherwise 0.
    I would like to write another function which should wait until the process will finish running.
    What's the best why to do it?

    I thought of using the while loop and the wait(30); command.
    Something like this:

    until(A()) { sleep(30); }
    I also can just call this subroutine until it will finish, although I don't think its smart because of all unnecessary junk.
    What's the best way to do so?
    I'm looking for the cleanest way to do it.
Net::SMTP_auth giving 421 errors with authentication
1 direct reply — Read more / Contribute
by philkime
on Jul 15, 2018 at 17:00
    I have libnet 3.11 which is the current latest version. I have an SMTP server which requires TLS and LOGIN auth. I can manually get all this to work fine with openssl s_client and manual commands up to the STARTTLS and then the AUTH LOGIN without problems. With perl Net::SMTP_auth, the AUTH LOGIN immediately closes the connection with a 421 error without returning prompts for the base 64 username/password. I cannot work out why. Something in the environment changed as this was working until recently, I suspect some SMTP server change. The TLS part is fine and the server enforces this before auth. I am using Net::SMTP_auth instead of Net::SMTP as the server does not advertise AUTH mechanisms which makes Net::SMTP fail to do authentication as it thinks the server can't do it. Manual:
    openssl s_client -starttls smtp -crlf -connect smtpserver:25 . . . 250 OK AUTH LOGIN . . . 334 VXNlcm5hbWU6
    This is all fine and working as expected - code 334 is the username prompt. However, with:
    my $smtp = Net::SMTP_auth->new('smtpserver'); $smtp->starttls; $smtp->auth('LOGIN', 'username', 'password);
    This dies with an SMTP 421 code "Connection closed" in the ->auth method. Debugging with Net::Cmd shows that this dies before it gets a username prompt and occurs immediately after sending "AUTH LOGIN".
How can I turn an op address into the right kind of B::OP?
2 direct replies — Read more / Contribute
by rockyb
on Jul 15, 2018 at 16:55

    O Omnificent Omnipotent Ones -

    In a running Perl program if I have an Op address (either by B::Concise, Devel::Callsite or via mysterious other ways) is there a simple way to cast that into the right kind of B::OP, short of walking an Opcode tree?

-scrollbars is unknown option in windowCreate
4 direct replies — Read more / Contribute
by Oberbee
on Jul 15, 2018 at 11:07

    Great Omniscient Masters,

    I am attempting to create a table of items with a uniform height inside an ROText widget. When attempting to use windowCreate to insert a scrollable ROText description I get this error:

    unknown option "-scrollbars" at ...

    Here is the code in question:

    $row0p0 = $top->Label(-text => $cat1, -height =>7, -width => 15, -relief => 'sunken')->grid( $row0p1 = $top->Label(-text => $lb12,-height =>7, -width => 15, -relief => 'sunken'), $row0p2 = $top->ROText(-height =>8, -width => 22, -wrap => 'word', -sc +rollbars => 'oe'), $row0p3 = $top->Button(-text => "Photo"), $row0p4 = $top->Button(-text => "Link")); $row0p2->insert('end', " [long description] "); $top->windowCreate('end', -window => $row0p0); $top->windowCreate('end', -window => $row0p1); $top->windowCreate('end', -window => $row0p2); $top->windowCreate('end', -window => $row0p3); $top->windowCreate('end', -window => $row0p4);

    How can I make this ROText widget scrollable?

Slow speeds with MozRepl
4 direct replies — Read more / Contribute
by emav
on Jul 14, 2018 at 15:50

    Here's the situation in my humble monastic cell... I recently bought a Synology NAS, installed Synology Drive and started syncing my drives to have a backup at hand in case an electronic fiend struck down my system.

    The thing is that I also use a nice little script I wrote to scan various sites for free e-books and comic strips that I can then download. The e-book script uses WWW::Mechanize::Firefox and MozRepl on Firefox 45.9 while the comics script uses LWP::Simple. The latter runs without a problem at all times. However, whenever the Synology Drive (SD) software is running, my e-book script simply stagnates and can hardly communicate with MozRepl on Firefox. Regularly, they would exchange data at a rate of hundreds of KB/s but, when SD is doing its syncing tasks, the data rate drops to meagre B/s. Even if I pause SD, I see no improvement. The only thing that does the trick is a reboot and making sure SD remains paused while my script is running.

    I have tried applying network filters that leave ample bandwidth for Perl and MozRepl to use but to no effect, of course, as they are both running on the same machine. I have tried to limit Synology Drive's CPU usage but that didn't work either. It never appeared to have a voracious appetite for CPU time, anyway. I've read that most syncing apps simply use too much memory to bring our machines to their knees but with 64GB on board I would be surprised to see it go in a flash down SD's drainpipe (problems appear even if SD starts running a couple of minutes before I trigger my script).

    So... I am at a loss as to how to go about spotting the problem (let alone solving it). Any pointers anybody?

Extract selected sections of POD
4 direct replies — Read more / Contribute
by Anonymous Monk
on Jul 13, 2018 at 17:21
    The title of this node is the description of Pod::Select. It does exactly what is needed but the POD warns, "NOTE: This module is considered legacy; modern Perl releases (5.18 and higher) are going to remove Pod-Parser from core and use Pod-Simple for all things POD." I'm familiar with Pod::Simple but don't see how to use it to replace Pod::Select. The docs mention subclassing but I don't want to roll my own anything to parse pod. I could do that manually with no modules in 2 seconds flat. Does anyone know how to "Extract selected sections of POD" with Pod::Simple or other core modules? Thank you.
Search between pattern and append
2 direct replies — Read more / Contribute
by Anonymous Monk
on Jul 13, 2018 at 10:39
    hi, I am trying to search between pattern and prefix for each line of the resultset. But it doesnt work. Please help. New to perl.
    perl -ne 's/^/ $i $j /g && print if /START $i/ .. /END $j/ ' file
    Thank you
Adding an array to an existing csv file as a new column
2 direct replies — Read more / Contribute
by c.con
on Jul 13, 2018 at 09:48

    I am looking to add an array to a csv file as a column. I basically have to csv files containing a column of numbers. There csv files have 100,000's of lines. Example:

    Time1 1 2 3 Time 2 4 5 6
    What I want to do is take columns from 2 different csv files, subtract the 2nd from the 1st and then add a new column to the 2nd file with a column called difference and the result. So far, I have got as far as creating an array with the difference in it. I am having trouble to actually put this array back as a column into the 2nd file. Does anyone have any suggestions. I want to keep the code I've written thus far if possible though. I was able to add the array of values to the end of the csv file but not after each line.

    use 5.10.0; use warnings; my $output = "results.txt"; my ($fh1, $fh2, $fh3, $fh4, $fh5); my ($file1, $file2) = @ARGV; my (@col1, @col2, @col3); my ($lines, $lines2, $lines3); my (@array, @array2) = (); my @diff = (); my $csv = "csv.csv"; ###Open files for read and write### open ($fh1, '<', $file1) or die $!; open ($fh2, '+<', $file2) or die $!; open ($fh3, '>', $output) or die $!; #open my $out, ">", "out.csv" or die $!; ###Reads lines from file into an array### while ($lines = <$fh1>){ chomp $lines;#Removes the new line @col = split "," , $lines; #Array where each index holds the dat +a of each column push @array, $col[5]; #adding element to the array containing sl +ack values for file } shift @array;#Removes the header line from the array -i.e column name ###Reads lines from file into an array### while ($lines2 = <$fh2>){ print $fh4 $lines2; chomp $lines2; #push @list, $lines2; @col2 = split "," , $lines2; push @arr, @col2; push @array2, $col2[5]; } shift @array2; ###Iterates through the array and finds the difference in slack### foreach my $i (@array){ $diffs = $array2[$i] - $array[$i];#Variable showing difference push @diff, $diffs;#Adding element to an array containing the diff +erence values } ###Prints out the 2 differenct slacks from each run and shows the diff +erence between them. #print $fh3 "Difference :", $array[$_] ,"-", $array2[$_], " = ", $diff +[$_], "\n" for 1 .. $#array; unshift @diff, "Difference";

    2018-07-14 Athanasius fixed closing code tag and added code tags around data


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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (9)
    As of 2018-07-18 13:16 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















      Results (393 votes). Check out past polls.

      Notices?