http://www.perlmonks.org?node_id=479

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
Can a regular expression include an arbitrary string variable?
4 direct replies — Read more / Contribute
by anautismobserver
on Oct 16, 2019 at 21:39

    I want to search an array for an arbitrary sting variable. All the examples I've found use regular expressions. Is there a way to devise a regular expression that includes an arbitrary string variable (unknown in advance)?

    For example:

    my #string_to_find = 'hidden'; my @array_to_search = ('Foo', 'whathiddenever', 'Bar', '12hidden456');
Can't remove ANSI markup
1 direct reply — Read more / Contribute
by glendeni
on Oct 16, 2019 at 21:35

    I output some text to a terminal by inserting colors via strings ala my $red = "\033[31m"; I would like to sometimes remove that markup for printing to a normal file. But all of the following fail to remove the markup:

    ( my $nocolorsline = $line ) =~ s/\033\[[\d;]*[a-zA-Z]//g; ( my $nocolorsline = $line ) =~ s/\e\[[\d;]*[a-zA-Z]//g; ( my $nocolorsline = $line ) =~ s/\Z\[\d;]*[a-zA-Z]//g; ( my $nocolorsline = $line ) =~ s/\x1b\[[\d;]*[a-zA-Z]//g;
    What am I missing ?
    Jack
    LATER - I was missing that I was changing the wrong line :-( So the above do work. Sorry for the inconvenience. Can't see how to delete this post.
Perl client blocking communication from server
No replies — Read more | Post response
by PerlNewbie94
on Oct 16, 2019 at 18:15
    Hello i am working with socket programming in Perl. It is a server client script. My server is able to handle multiple connections but my client hangs when i try to receive data from server. Client and server exchange two messages before communication between server and client can start. My client hangs when i try to catch the second message from the server. I am new to socket programming any help would be greatly appreciated.
    #!/usr/bin/perl -w use strict; use IO::Socket; @ARGV == 2 or die "IP:Portnumber Nickname needed "; my ( $host, $port, $kidpid, $handle, $line,$nickname, $connect_check, +$nick_che$ ($host, $port)=@ARGV; my $input = $ARGV[0]; $nickname = $ARGV[1]; my @arguments= split(':',$input); my $server_ip= $arguments[0]; my $server_port=$arguments[1]; $handle = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $server_ip, PeerPort => $server_port ) or die "can't connect to port $server_port on $server_ip: $!"; $handle->autoflush(1); print STDERR "[Connected to $host:$port]\n"; $handle->recv($connect_check,255); $nickname= "NICK" . " $nickname"; $handle->send($nickname); $handle->recv($nick_check,255); #die "can't fork: $!" unless defined( $kidpid = fork() ); # the if{} block runs only in the parent process #if ($kidpid) { # copy the socket to standard output # while ( defined( $line = <$handle> ) ) { # print STDOUT $line; # } #kill( "TERM", $kidpid ); # send SIGTERM to child #} # the else{} block runs only in the child process #else { # # copy standard input to the socket # while ( defined( $line = <STDIN> ) ) { # print $handle "$line"; # } #}
    My server code
    #!/usr/bin/perl -w use strict; use warnings; use IO::Socket; use IO::Select; # auto-flush on socket $| = 1; my $server_ip; my $server_port; @ARGV == 2 or die "IP and Portnumber needed"; ($server_ip,$server_port)=@ARGV; # creating a listening socket my $tcp_socket = new IO::Socket::INET( LocalHost => $server_ip, LocalPort => $server_port, Proto => 'tcp', Listen => 5, Reuse => 1 ); die "cannot create socket $!\n" unless $tcp_socket; print "server waiting for client connection on port $server_port\n"; #Create write and readhandlers with select my $read_select = IO::Select->new(); $read_select->add($tcp_socket); #version send to server my $Version ="Hello <VERSION>\n"; #hash for client my %clients; while(1) { my $new_readable; my $nick; my $message_check=0; ($new_readable)=IO::Select->select($read_select,undef,undef,0); foreach my $read(@$new_readable) { if($read==$tcp_socket) { my $new_connection = $read->accept(); $new_connection->send($Version); $read_select->add($new_connection); } else { my $buf; my $msg; $buf=<$read>; if($buf) { my $check_message = index($buf,"MSG"); #Hanndle nick my $nick_check; my $content = "NICK"; my $found_content = index($buf,$content); if ($found_content == 0) { $nick_check="OK\n"; $read->send($nick_check); print "Ok sent\n"; } else { $nick_check="Error <txt>\n"; $read->send($nick_check); } if ($found_content == 0) { $nick = substr $buf, 5; $clients{"$read"}="$nick"; } my $nick_name=""; my $message = "MSG"; my $header=""; my $buffer; if($buf =~m/^MSG/) { while (my ($key,$value)= each %clients){ if ($key eq $read) { $nick_name = $value; chomp $nick_name; } } } if ($nick_name) { $header = $message." $nick_name"; substr($buf,0,3) = $header; } my @sockets = $read_select->can_write(); #returns a array of handl$ foreach my $sck(@sockets) { if ($sck!=$read && $buf !~m/^NICK/ && $message_check ! +=1) { $sck->send("$buf\n"); } } } else { $read_select->remove($read); } } } } $tcp_socket->close();
Is there a term for this?
7 direct replies — Read more / Contribute
by mw
on Oct 16, 2019 at 10:28

    Dwellers of the Monastery, allow me to de-lurk, partly from a desire to commune with fellow Monks, partly from a regular frustration in on-line discussions.

    Sometimes, one must run Perl scripts on machines that one does not own, has no administrative rights on, and most significantly, one is not allowed to install any software on. In general, one is lucky if /usr/bin/perl -v returns something containing a 5. A prime example is an inventory script. One copies it to one's home directory, one runs it, and one removes it. One might be grateful that even this is allowed.

    Not all of these restrictions are even unreasonable. One does not want to upgrade a seemingly innocuous package, only to find that this upgrade breaks something important. One wishes to tread lightly, at least this one does.

    For this reason, I have on occasion been forced to write modules for which a beautiful module already exists on CPAN. I have had to produce my very own XML parser, because XML::LibXML was not installed everywhere, and I was forbidden from installing it, or anything else. People pointing out how wonderful XML::LibXML is, and that I am stupidly re-inventing a perfectly good wheel, only succeed in rubbing salt into the wound and bring me no closer to a solution.

    Does this resonate with anyone in this group? If so, is there a concise term for this kind of situation? For instance:

    • Hi, I'd like to know how to fork off several subprocesses in a controlled manner, but fork() isn't returning what I expect it to.
    • There's a great module for that on CPAN! Why don't you use that? It'll save you so much hassle!
    • I know. Can't. I'm in a nature reserve. Take nothing but logs, leave nothing but wtmp entries.

    Or something in that spirit.

[implementation specific to windows] writing a proper batch file for terminal start-up
2 direct replies — Read more / Contribute
by Aldebaran
on Oct 15, 2019 at 18:11

    I have been wanting to post more about scrabble-esque games, but I have an implementation problem that I am still struggling with. I've installed Strawberry perl, as well as git for windows, as well as a batch file from git that is to start Strawberry perl correctly which, unfortunately, I lack a reference for. The one I am going to post here has been altered from the original so as to be more verbose, at least until I get this ironed out.

    As I keep working with this script, I keep commenting out the things that I don't need. After grinding this out for a few weeks, more of it is commented out than not:

    I would try to use my script for generating the tags for a write-up and would get this:

    C:\Users\tblaz\Documents\evelyn>perl 8.monktag.pl INC is C:/Strawberry/perl/site/lib C:/Strawberry/perl/vendor/lib C:/St +rawberry/perl/lib Can't locate Win32.pm in @INC (you may need to install the Win32 modul +e) (@INC contains: C:/Strawberry/perl/site/lib C:/Strawberry/perl/ven +dor/lib C:/Strawberry/perl/lib) at C:/Strawberry/perl/lib/Cwd.pm line + 605. BEGIN failed--compilation aborted.

    Then I noticed that one of the directories that had this was given my the directory in PERL5LIB. Indeed, when I comment the line out that resets this, then the script works. So I have results, yay, ...but, I would like to pause to ask whether I am doing something stupid that I will regret.

    Is PERL5LIB set to null for a reason? Am I setting up my system to be playing a tug-of-war? I have 3 different perls on this system. Should they have a single common place for new modules? In the past (distant), it's seemed like the mingw install starts to fight with strawberry perl and eventually wins, leaving the strawberry perl install outmoded and dusty.

    Is being the directory in PERL5LIB how this value entered @INC? Also, am I doing anything useful with the following commands?

    C:\Strawberry>set TERM=dumb C:\Strawberry>set PERL_JSON_BACKEND=JSON::XS C:\Strawberry>set PERL_YAML_BACKEND=YAML C:\Strawberry>rem avoid collisions with other perl stuff on your syste +m C:\Strawberry>rem set PERL5LIB= C:\Strawberry>set PERL5OPT= C:\Strawberry>set PERL_MM_OPT= C:\Strawberry>set PERL_MB_OPT=

    I hope to get back into less implementation-specific issues soon.

    Thanks for your comment,

Encoding all HTML entities except < and >
1 direct reply — Read more / Contribute
by TrixieTang
on Oct 15, 2019 at 15:09

    How would one go about encoding all HTML entities in a string except for < and >? I've tried using HTML::Entities, but it ends up encoding < and > and breaking the HTML. I've also tried tinkering with the unsafe_characters parameter in HTML::Entities but I still can't seem to get it to allow < and >.

Error in coverage module
2 direct replies — Read more / Contribute
by ovedpo15
on Oct 15, 2019 at 11:14
    I'm trying to run the Devel::Cover module on a testing tool. I get the following error:
    unexpected OP_RKEYS at /usr/pkgs/perl/5.14.1/lib64/5.14.1/B/Deparse.pm + line 1113. unexpected OP_RKEYS at /usr/pkgs/perl/5.14.1/lib64/5.14.1/B/Deparse.pm + line 1113. unexpected OP_RKEYS at /usr/pkgs/perl/5.14.1/lib64/5.14.1/B/Deparse.pm + line 1113. unexpected OP_RKEYS at /usr/pkgs/perl/5.14.1/lib64/5.14.1/B/Deparse.pm + line 1113. Possible unintended interpolation of @runs in string at /usr/pkgs/per +l/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line +808. Possible unintended interpolation of @runs in string at /usr/pkgs/per +l/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line +808. Bareword found where operator expected at /usr/pkgs/perl/5.14.1/lib64 +/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 836, near "$fi +le =~ q|/Devel" (Might be a runaway multi-line // string starting on line 808) Global symbol "@runs" requires explicit package name at /usr/pkgs/per +l/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line +808. Global symbol "$run" requires explicit package name at /usr/pkgs/perl +/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 8 +08. Global symbol "@runs" requires explicit package name at /usr/pkgs/per +l/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line +808. Global symbol "$st" requires explicit package name at /usr/pkgs/perl/ +5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 80 +8. Global symbol "$r" requires explicit package name at /usr/pkgs/perl/5 +.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 808 +. Global symbol "$run" requires explicit package name at /usr/pkgs/perl +/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 8 +08. Global symbol "$r" requires explicit package name at /usr/pkgs/perl/5 +.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 808 +. Global symbol "$r" requires explicit package name at /usr/pkgs/perl/5 +.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 808 +. Global symbol "$st" requires explicit package name at /usr/pkgs/perl/ +5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 80 +8. Global symbol "$r" requires explicit package name at /usr/pkgs/perl/5 +.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 808 +. Global symbol "$count" requires explicit package name at /usr/pkgs/pe +rl/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line + 808. Global symbol "$r" requires explicit package name at /usr/pkgs/perl/5 +.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 808 +. Global symbol "$run" requires explicit package name at /usr/pkgs/perl +/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 8 +08. Global symbol "$count" requires explicit package name at /usr/pkgs/pe +rl/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line + 808. Global symbol "$file" requires explicit package name at /usr/pkgs/per +l/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line +808. Global symbol "$f" requires explicit package name at /usr/pkgs/perl/5 +.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 808 +. Global symbol "$count" requires explicit package name at /usr/pkgs/pe +rl/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line + 808. Global symbol "$digest" requires explicit package name at /usr/pkgs/p +erl/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm lin +e 808. Global symbol "$r" requires explicit package name at /usr/pkgs/perl/5 +.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line 808 +. Global symbol "$file" requires explicit package name at /usr/pkgs/per +l/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line +808. Global symbol "$digest" requires explicit package name at /usr/pkgs/p +erl/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm lin +e 808. Global symbol "$file" requires explicit package name at /usr/pkgs/per +l/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line +808. Global symbol "$file" requires explicit package name at /usr/pkgs/per +l/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line +808. Global symbol "$file" requires explicit package name at /usr/pkgs/per +l/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cover/DB.pm line +808. /usr/pkgs/perl/5.14.1/lib64/5.14.1/Devel--Cover-master/lib/Devel/Cove +r/DB.pm has too many errors. Compilation failed in require at /usr/pkgs/perl/5.14.1/lib64/5.14.1/D +evel--Cover-master/lib/Devel/Cover.pm line 21. BEGIN failed--compilation aborted at /usr/pkgs/perl/5.14.1/lib64/5.14 +.1/Devel--Cover-master/lib/Devel/Cover.pm line 21. Compilation failed in require. BEGIN failed--compilation aborted.
    I guess it has to do with the following comment:
    # Sometimes the start value is undefined. It's not yet clear why, + but it # probably has something to do with the code under test forking. +We'll # just try to cope with that here.
    From the module (link: https://github.com/pjcj/Devel--Cover/commit/39ee220b1c04c442e1ab9077fea7309e2ebc9769). Have someone already had this issue and knows how to solve it?
Getting 403 from cpan.org when installing packages
1 direct reply — Read more / Contribute
by TieUpYourCamel
on Oct 14, 2019 at 16:22
    I'm attempting to install HTML::Template with cpanm in perlbrew... cpan.org is responding with a 403.
    myuser@spangled:~$ which cpanm /home/myuser/perl5/perlbrew/bin/cpanm myuser@spangled:~$ cpanm install HTML::Template --> Working on install Fetching http://www.cpan.org/authors/id/D/DA/DAGOLDEN/install-0.01.tar +.gz ... OK Configuring install-0.01 ... OK Building and testing install-0.01 ... OK Successfully installed install-0.01 --> Working on HTML::Template Fetching http://www.cpan.org/authors/id/S/SA/SAMTREGAR/HTML-Template-2 +.97.tar.gz ... OK Configuring HTML-Template-2.97 ... OK ==> Found dependencies: Test::Pod, CGI --> Working on Test::Pod Fetching http://www.cpan.org/authors/id/E/ET/ETHER/Test-Pod-1.52.tar.g +z ... OK Configuring Test-Pod-1.52 ... OK Building and testing Test-Pod-1.52 ... OK Successfully installed Test-Pod-1.52 --> Working on CGI Fetching http://www.cpan.org/authors/id/L/LE/LEEJO/CGI-4.44.tar.gz ... + OK gzip: stdin: unexpected end of file /bin/tar: Child returned status 1 /bin/tar: Error is not recoverable: exiting now ! Failed to unpack CGI-4.44.tar.gz: no directory ! Failed to fetch distribution CGI-4.44 ! Installing the dependencies failed: Module 'CGI' is not installed ! Bailing out the installation for HTML-Template-2.97. 2 distributions installed myuser@spangled:~$ wget http://www.cpan.org/authors/id/L/LE/LEEJO/CGI- +4.44.tar.gz --2019-10-14 16:03:35-- http://www.cpan.org/authors/id/L/LE/LEEJO/CGI +-4.44.tar.gz Resolving www.cpan.org (www.cpan.org)... 151.101.194.217, 151.101.130. +217, 151.101.66.217, ... Connecting to www.cpan.org (www.cpan.org)|151.101.194.217|:80... conne +cted. HTTP request sent, awaiting response... 403 FORBIDDEN 2019-10-14 16:03:36 ERROR 403: FORBIDDEN. myuser@spangled:~$
    I've tried this on different days (also tried it last Friday) and on different platforms with different perls. I always get the same result. It can't download the tar file, so it can't unzip it. I can download and unzip the file just fine on my Chrome/Windows setup. Just not from cpanm in perlbrew.
RSA.xs:194: OpenSSL error: no start line
2 direct replies — Read more / Contribute
by cinergi
on Oct 14, 2019 at 14:29

    Hello,

    I'm trying to debug the following error in a perl script that manipulates X509 certificates.

    RSA.xs:194: OpenSSL error: no start line

    My research suggests this is usually due to the certificate files not having the correct text encoding for the platform (UTF-8 for Windows in my case). However, I can read the certificates just fine using "openssl x509 -text -inform PEM -in <certfile>" from the command line, so this doesn't seem to be the case. What other causes does this error have?

    In case it doesn't show, I'm a total perl newbie. Just trying to get a script running (I did not write it).

    Thanks!

    cinergi

Issue with @INC
5 direct replies — Read more / Contribute
by sidmuchrock
on Oct 14, 2019 at 12:26

    So I was a perl 4 person for years, played in 5, scared of 6. Went to do something smarter than the old way today with dates. Old way is I would have created a @months=('January', etc.) Not even a qw as I'm that old. Anyway decided I'd go a bit more modern and use Date::Manip.pm but its not there. Okay add it. Wait strict isn't there?! My @INC doesn't seem to be playing right. Thoughts? Lemme add more danger. Strict was there earlier today as I used cpan to add DateTime. but it wouldn't install unless I used sudo cspan DateTime. It/I borked my @INC somehow. Wise Monks, what you got?

    [SiDMBP2:~] urinine% perl dog2.pl Can't locate Date/Manip.pm: Permission denied at dog2.pl line 1. BEGIN failed--compilation aborted at dog2.pl line 1. [SiDMBP2:~] urinine% locate Manip.pm [SiDMBP2:~] urinine% cpan Manip.pm Can't locate strict.pm: Permission denied at /usr/bin/cpan5.18 line +7. BEGIN failed--compilation aborted at /usr/bin/cpan5.18 line 7. [SiDMBP2:~] urinine% locate strict.pm /System/Library/Perl/5.18/strict.pm [SiDMBP2:~] urinine% perl -e 'print(join("\n", @INC))' /Library/Perl/5.18/darwin-thread-multi-2level /Library/Perl/5.18 /Network/Library/Perl/5.18/darwin-thread-multi-2level /Network/Library/Perl/5.18 /Library/Perl/Updates/5.18.4 /System/Library/Perl/5.18/darwin-thread-multi-2level /System/Library/Perl/5.18 /System/Library/Perl/Extras/5.18/darwin-thread-multi-2level /System/Library/Perl/Extras/5.18 .

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.