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 find and replace with a twist
2 direct replies — Read more / Contribute
by Anonymous Monk
on Jul 17, 2018 at 00:32

    How do I find and replace every letter in a word with a modified version of the word? Basically I need to find any letter [a-zA-Z] and replace it with [aA][zZ]. For Example:

    my $word = "layer" my $new_word = "[l][a][y][e][r]"

    2018-07-17 Athanasius added code tags within text

IF condition with a range
4 direct replies — Read more / Contribute
by Anonymous Monk
on Jul 16, 2018 at 10:12
    Hi Monks,
    I want to write the following into and IF clause: "If the number is exactly equal, or equal+1 or equal-1".
    I guess I could write it like this:
    if($x==$y or $x==$y-1 or $x==$y+1)

    Is there a more compact way to write this?
Print inside SIGNALS
3 direct replies — Read more / Contribute
by pedrete
on Jul 16, 2018 at 09:46
    Hi PerlMonks...

    Is there any safe way of print to STDOUT inside a signal?

    something like this

    alarm 10; $SIG{ALRM} = \&Finish; sub Finish { print "Timeout reached"; }

    does not work in Linux Debian.


Tk::grid -sticky option not stretching frames
3 direct replies — Read more / Contribute
by boleary
on Jul 16, 2018 at 08:07

    I have been struggling with the -sticky option with Tk::grid. It does not seem to want to stretch a container for me.

    In this simple example I use $gridOrPack to select whether I will grid or pack 2 frames. Then I add a label to one frame and a button to the other frame...

    when I set $gridOrPack to 'pack' I pack with -expand=>1 and fill=>'both', It behaves just like I want... the frames stretch to fill the mainWindow

    when I set $gridOrPack to 'grid' I grid with the -sticky option set to 'nsew' (I've also tried 'ew' and 'ns') and the frames do not stretch.

    Am I missing something silly?

    use strict; use Tk; use Tk::LabFrame; use Tk::Treeplus; use Tk::ErrorDialog; my $mw = MainWindow->new(); my $gridOrPack="grid"; #"pack"; #my $gridOrPack="pack"; my $tpFrame; my $butFrame; my $tpFrame=$mw->LabFrame(-label=>'TreePlusFrame'); my $butFrame=$mw->LabFrame(-label=>'ButtonFrame'); if ($gridOrPack=~/grid/i) { $tpFrame->grid(-row=>1, -column=>1, -sticky=>'ew'); $butFrame->grid(-row=>2, -column=>1, -sticky=>'ew'); } else { $tpFrame->pack(-side=>'top',-expand=>1,-fill=>'both'); $butFrame->pack(-side=>'top',-expand=>1, -fill=>'both'); } $tpFrame->Label(-text=>'I like pack better')->pack(-side=>'top'); $butFrame->Button( -text => 'Exit', -command => sub { exit(0) }, ) ->pack(qw(-side bottom -pady 10)); MainLoop();
DESTROY Function call
1 direct reply — Read more / Contribute
by zeeshan1995
on Jul 16, 2018 at 06:22
    Does perl guarantee that the  DESTROY() function will be called immediately after the closing braces } for the objects in the scope created using  new() ( as C++ does)?
$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?
1 direct reply — 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?

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.