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
Syntax Perl Version support $c = () = $a =~ /\./g
5 direct replies — Read more / Contribute
by h2
on Jul 17, 2018 at 14:02

    Hi Perl Monks, I have what is hopefully a simple question, but one I have been unable to search for due to the odd syntax of the below.

    The program I'm working on has a core requirement to work on older systems, and has a cutoff of Perl 5.008 (selected because Redhat mid 2000s release was quite late to support 5.010) or newer, though that could change in the future to either a touch earlier version, or a touch later version (but certainly never newer as max oldest than 5.010). As an aside, so far Perl 5.x has far exceeded my wildest expectations in this regard, the program is in fact working on everything! No matter how weird or arcane.

    my $a='3.4.4'; my $c = () = $a =~ /\./g; print "$c\n"

    However, my question is this: when did Perl begin supporting this type of structure?

    $c = () = $a =~ /\./g

    I've come across this several times, and I'm allured by it, it calls to me, but I have to be careful using structures that could trigger warnings or failures in old systems.

    I'd actually like to significantly expand my use of this type of structure, so besides this specific example, can anyone point me to the right search terms that describe it, that's multiple assignments of some value generated on the right side of the items? And also I believe various items that could be in the second position, (), though I'm not positive about that. And has this support been consistent since version 5.xxx? Are there various types of this assignment that have been introduced earlier or later?

    I have the big Larry Walls Perl book, but I don't know what this is called to begin learning more about it, and what variants will be safe to use.

Using Perl XS in 5.26 fails with error loadable library and perl binaries are mismatched
3 direct replies — Read more / Contribute
by jobinjoseph121
on Jul 17, 2018 at 08:35

    We have build Perl 5.26.1 from source and used the same Perl to create a perlmodule.c file from an XS file perlmodule.xs. But when we try to execute this C code, we get the following error.
    ../perlmodule.c: loadable library and perl binaries are mismatched (got handshake key 0xe280080, needed 0xde00080)

    Previously we were using Perl 5.16 and there were no issues. Could you please let us know whether we need to rewrite the XS module for Perl 5.26. Currently we are using the same XS module which is used in Perl 5.16. I am not sure if that is creating the issue here.

    We are generating the .c files with following command.
    perl ExtUtils/xsubpp -typemap typemap -typemap ExtUtils/typemap perlmodule.xs > perlmodule.cpp

    Following is the configuration details.

    Summary of my perl5 (revision 5 version 26 subversion 1) configuration +: Platform: osname=linux osvers= archname=x86_64-linux-thread-multi uname='linux #1 mon jul 3 18:25:39 utc 2006 +x86_64 x86_64 x86_64 gnulinux ' config_args='-d -s -e -U uselargefiles -U use64bitint -U usemorebi +ts -D usemultiplicity -D usethreads -D prefix=/opt/perl/a -D installu +srbinperl=no -D useshrplib -D ccflags= -D libperl=libperl.so -D man1d +ir= -D perladmin=none -D pager=/usr/bin/less -D otherlibdirs=/opt/pe +rl/a/lib/site_perl' hint=recommended useposix=true d_sigaction=define useithreads=define usemultiplicity=define use64bitint=define use64bitall=define uselongdouble=undef usemymalloc=n default_inc_excludes_dot=define bincompat5005=undef Compiler: cc='cc' ccflags ='-D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -pipe -f +stack-protector -I/usr/local/include -D_FORTIFY_SOURCE=2' optimize='-O2' cppflags='-D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -pipe -f +stack-protector -I/usr/local/include' ccversion='' gccversion='4.1.0 (SUSE Linux)' gccosandvers='' intsize=4 longsize=8 ptrsize=8 doublesize=8 byteorder=12345678 doublekind=3 d_longlong=define longlongsize=8 d_longdbl=define longdblsize=16 longdblkind=3 ivtype='long' ivsize=8 nvtype='double' nvsize=8 Off_t='off_t' lseeksize=8 alignbytes=8 prototype=define Linker and Libraries: ld='cc' ldflags =' -fstack-protector -L/usr/local/lib' libpth=/usr/local/lib /usr/lib64/gcc/x86_64-suse-linux/4.1.0/../.. +/../../x86_64-suse-linux/lib /usr/lib /lib/../lib64 /usr/lib/../lib64 + /lib /lib64 /usr/lib64 /usr/local/lib64 libs=-lpthread -lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc -lgdb +m_compat perllibs=-lpthread -lnsl -ldl -lm -lcrypt -lutil -lc libc=libc-2.4.so so=so useshrplib=true libperl=libperl.so gnulibc_version='2.4' Dynamic Linking: dlsrc=dl_dlopen.xs dlext=so d_dlsymun=undef ccdlflags='-Wl,-E -Wl,-rpath,/opt/perl/a/lib/5.26.1/x86_64-linux-t +hread-multi/CORE' cccdlflags='-fPIC' lddlflags='-shared -O2 -L/usr/local/lib -fstack-protector' Characteristics of this binary (from libperl): Compile-time options: HAS_TIMES MULTIPLICITY PERLIO_LAYERS PERL_COPY_ON_WRITE PERL_DONT_CREATE_GVSV PERL_IMPLICIT_CONTEXT PERL_MALLOC_WRAP PERL_OP_PARENT PERL_PRESERVE_IVUV USE_64_BIT_ALL USE_64_BIT_INT USE_ITHREADS USE_LOCALE USE_LOCALE_COLLATE USE_LOCALE_CTYPE USE_LOCALE_NUMERIC USE_LOCALE_TIME USE_PERLIO USE_PERL_ATOF USE_REENTRANT_API Built under linux

    Thanks and Regards,
    Jobin Joseph
regex find and replace with a twist
3 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
7 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
4 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
4 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.

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.