Speed comparison of foreach vs grep + map
6 direct replies — Read more / Contribute
|
by mldvx4
on May 25, 2025 at 13:57
|
|
|
Greetings, PerlMonks!
I'm trying to figure out if foreach is faster than grep with map. I've replaced grep and map in a large script with foreach and noticed a substantial speed improvement, so my guess is that it is faster. However, I'd like to verify that somehow.
With the two scripts below, am I comparing the same things and not apple to oranges? Or have I misunderstood grep and map?
#!/usr/bin/perl
use strict;
use warnings;
my @c = (1 .. 10000000);
my @d;
foreach my $dd (@c) {
push(@d, $dd % 2);
}
my @e;
foreach my $ee (@d) {
if (!$ee) {
push(@e, $ee);
}
}
exit(0);
The script above is much faster than the one below, according to time.
#!/usr/bin/perl
use strict;
use warnings;
my @c = (1 .. 10000000);
my @d = map( $_%2, @c);
my @e = grep(/0/, @d);
exit(0);
|
Key bindings in the Debugger
1 direct reply — Read more / Contribute
|
by hexcoder
on May 23, 2025 at 07:28
|
|
|
While debugging with StrawberryPerl when I enter a '§' character, I see that the previously entered line is deleted instead.
x %INC shows that modules Term::Readline, Term::ReadKey and Term::Cap are loaded.
Does anybody know, how to switch off this editing function binding?
Thanks!
|
Where is plain XS.pm?
6 direct replies — Read more / Contribute
|
by lyman
on May 22, 2025 at 16:26
|
|
|
Dear Perl Monks,
I'm an experienced perl programmer, and am having difficulty getting the module XS-Typemap::Typemap to work. It needs an XS.pm .
I have searched, searched, and pulled out a lot of hair, and I cannot find what I must install to get an XS.pm .
No, it's not something::XS or XS:something, just plain XS. I have installed Debian apts
libx11-xcb
libx11-xcb-dev
libx11-protocol-perl
libx11-protocol-other-perl
libx11-xcb-perl
xorg-dev
libxext-dev
libxfexes-dev
libx11proto-dev
libxmu-dev
libxmuu-dev
libxt-dev
xutils-dev
libperl-dev
I find on metacpan.org/search dozens of modules with "XS" in their names, but not plain XS.pm .
How do I install this XS.pm, so that XS-Typemap::Typemap will work?
Many thanks,
Lyman
|
Race when redirecting output.
1 direct reply — Read more / Contribute
|
by gnosti
on May 21, 2025 at 17:49
|
|
|
Experienced Monks!
I have a command-line app that parses commands and prints the output to the terminal. In search of new features, I'm migrating the terminal library from readline to tickit.
To simplify this changeover, my plan is to use open and select to direct the default output filehandle to a $variable, and then periodically dump the contents of $variable to a tickit widget.
I'm close, but my naive implementation loses every other line of output. Seems like I need to away to ensure that writes to $command_output are held up while printing and then deleting the contained text. Some kind of first-in-last-out buffer with synchronization.
Will be grateful for any hints on an easy way to accomplish this. Here is sample code demonstrating my conundrum:
|
Perl Expect Help
3 direct replies — Read more / Contribute
|
by Anonymous Monk
on May 21, 2025 at 16:01
|
|
|
I am having a problem with understanding expect. I have am trying to create an shh script. I have 3 different conditions:
- host is unknown it needs to be added to known host list,
- host needs password,
- host dose not need password as it is using ssh key.
$exp->expect($timeout,
[ qr/\(yes\/no\)\?/i, sub { my $self = shift;
$self->send("yes\r"); <----unk
+nown host
exp_continue; }],
[ qr/password: /i, sub { my $self = shift;
$self->send("$password\n"); <---
+--- needs password
exp_continue; }],
[ qr/#/i, sub { my $self = shift; <------
+-- ready to go.
$self->send("ls\n");
;
}],
);
So how do I, once I get through the 3 conditions continue using expect?
$exp->expect($timeout,
[ qr/#/i, sub { my $self = shift;
$self->send("ll\n"); <---- only
+ works when I have a line below (crazy that is how I know this is not
+ right)
;
}], un
+less ($exp->expect($timeout, -re , "~")){} ;
);
What I would like is to get past the three different ssh scenarios and keep going on.
#!/usr/bin/env perl
use strict;
use warnings;
use Expect;
#$Expect::Exp_Internal = 1;
my $command = "ssh";
my $user = "root";
my @ips = ("10.16.135.157");
my $cnt= 0+@ips;
my $password = "aristo1";
my $timeout = 10;
for (my $i =0; $i < $cnt; $i++) {
my $exp = Expect->spawn ($command, "$user"."@"."$ips[$i]");
#$exp->debug(2);
$exp->expect($timeout,
[ qr/\(yes\/no\)\?/i, sub { my $self = shift;
$self->send("yes\r");
exp_continue; }],
[ qr/password: /i, sub { my $self = shift;
$self->send("$password\n");
exp_continue; }],
[ qr/#/i, sub { my $self = shift;
$self->send("ls\n");
;
}],
);
$exp->expect($timeout,
[ qr/#/i, sub { my $self = shift;
$self->send("ll\n");
;
}],
);
unless ($exp->expect($timeout, -re , "~")){} ;
# $exp->send("time\n");
}
|
PLS (Perl Language Server) renaming support in NVIM
1 direct reply — Read more / Contribute
|
by igoryonya
on May 21, 2025 at 04:36
|
|
|
Hello, I've installed https://metacpan.org/dist/PLS module.
Configured it in NVIM with https://github.com/neovim/nvim-lspconfig plugin.
Auto completions work. It even shows documentation excerpts, when cursor is over some keyword or library module, but out of all issues, my biggest problem is that Language Server's renaming doesn't work.
When I try to rename some function or a variable, it gives me an error:
'[LSP] Rename, no matching language servers with rename capability.'
Does anybody know, if perl Language servers don't support such functionality or did I configure something wrong?
|
Dancer2 App Deployment through Apache Proxy
2 direct replies — Read more / Contribute
|
by choroba
on May 20, 2025 at 11:53
|
|
|
I wrote a Dancer2 application to be used by about 10 people from work. It ran fine on http://localhost, but the pages needed to be available from the outside world. Our IT admin told me the standard practice is to run it on a virtual host and use Apache's mod_proxy.
Dancer2::Manual::Deployment even mentions such a possibility, so I followed the instructions. I included
behind_proxy: 1
to config.yml, and the admin configured the proxy similarly as shown in the manual (see Using-Apache's-mod_proxy of Dancer2::Manual::Deployment.
I was given a prefix under which the app would be running, e.g. https://example.com/prefix/login.
But it doesn't work correctly. For example, the css files are ignored. Or rather, they can't be found.
The main.tt template was created with the following link (part of the initial scaffolding):
<link rel="stylesheet" href="<% request.uri_base %>/css/style.css">
But the request.uri_base doesn't expand to /prefix, it remains empty.
Similarly, I use Dancer2::Plugin::Auth::Tiny for user authentication. Again, I almost copied verbatim the synopsis code:
get '/' => needs login => sub {
# ...
};
get '/login' => sub {
template 'login'
};
post '/login' => sub {
my $user = body_parameters->get('uname');
if (is_valid($user, body_parameters->get('psw'))) {
session(user => $user);
redirect('/')
} else {
template index => {error => 'Wrong username or password'}
}
};
But again, when I try to open the page, the authentication plugin redirects the browser to /login instead of /prefix/login.
I was able to fix the problems by
Mentioning the prefix in the config definitely feels wrong. Hardcoding the prefix into the app? It also means the app can't be run locally on localhost for testing anymore.
How should I properly write the app, configure it, and configure Apache to make it work both locally and in production, without hardcoding the prefix anywhere in the app?
Interestingly, all Python flask and whatever apps written by other colleagues run as written without problems.
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
|
XML::Twig not finding an element's parent's text
1 direct reply — Read more / Contribute
|
by mldvx4
on May 18, 2025 at 13:28
|
|
|
Greetings, PerlMonks.
I have run into a puzzle with XML::Twig where I am looking for specific elements and then need to find the text adjacent to those elements. The code snippet below illustrates the puzzle with one such sought after element and adjacent text in its __DATA__ section.
#!/usr/bin/perl
use XML::Twig;
use strict;
use warnings;
my $xml = XML::Twig->new(
twig_handlers => { 'text:bookmark' => \&handler_bookmark } );
$xml->parse(\*DATA);
print qq(\n\n);
$xml->print;
exit(0);
sub handler_bookmark {
my ($twig, $bookmark)= @_;
$bookmark->parent->print;
}
__DATA__
<?xml version="1.0" encoding="UTF-8"?>
<text:h text:style-name="P900" text:outline-level="3">
<text:bookmark text:name="_asdfqwerzxcv"/>Foo bar
</text:h>
The two output items should be identical but are not. Specifically the string "Foo bar" is missing from the first output which has its origin in the handler_bookmark handler subroutine. I would expect that ->parent would still contain the text it started with, but it does not. Using ->parent->text does not retrieve the string either. Nor does using ->parent_text find it either.
What can be done using XML::Twig to find the text "next to" an element?
|
What's wrong with my pinto repository? Or with cpanm? (on CygwinPerl)
1 direct reply — Read more / Contribute
|
by Intrepid
on May 17, 2025 at 14:44
|
|
|
Hello impeccable denizens of Perlmonks. I've got a problem with cpanm not finding a module in the list of dependencies for Clipboard(.pm), in my newly created pinto repo. I've pasted the output here (a little bit trimmed of irrelevant stuff):
export REPO="/cygdrive/c/Users/somia/code-repositories/pinto/Clipboard"
$ cpanm --verbose --from "file://$REPO" --install Clipboard
cpanm (App::cpanminus) 1.7047 on perl 5.040002 built for x86_64-cygwin-threads-multi
Work directory is C:/Users/somia/.cpanm/work/1747447503.13337
Written by John Gilmore and Jay Fenlason.
Searching Clipboard on mirror file:///cygdrive/c/Users/somia/code-repositories/pinto/Clipboard ...
Downloading index file file:///cygdrive/c/Users/somia/code-repositories/pinto/Clipboard/modules/02packages.details.txt.gz ...
Uncompressing index file...
--> Working on Clipboard
Fetching file:///cygdrive/c/Users/somia/code-repositories/pinto/Clipboard/authors/id/K/KI/KING/Clipboard-0.13.tar.gz ... OK
Unpacking Clipboard-0.13.tar.gz
Clipboard-0.13/
Clipboard-0.13/scripts/
Clipboard-0.13/scripts/clipjoin
Clipboard-0.13/scripts/clipbrowse
Clipboard-0.13/scripts/clipaccumulate
Clipboard-0.13/scripts/clipedit
Clipboard-0.13/scripts/clipfilter
Clipboard-0.13/README
Clipboard-0.13/t/
Clipboard-0.13/t/mock.t
Clipboard-0.13/t/drivers.t
Clipboard-0.13/META.yml
Clipboard-0.13/inc/
Clipboard-0.13/inc/Module/
Clipboard-0.13/inc/Module/Install.pm
Clipboard-0.13/inc/Module/Install/
Clipboard-0.13/inc/Module/Install/Win32.pm
Clipboard-0.13/inc/Module/Install/Metadata.pm
Clipboard-0.13/inc/Module/Install/WriteAll.pm
Clipboard-0.13/inc/Module/Install/Can.pm
Clipboard-0.13/inc/Module/Install/Makefile.pm
Clipboard-0.13/inc/Module/Install/Fetch.pm
Clipboard-0.13/inc/Module/Install/Base.pm
Clipboard-0.13/INSTALL
Clipboard-0.13/at/
Clipboard-0.13/at/clipbrowse-test
Clipboard-0.13/at/clipfilter-test
Clipboard-0.13/at/pxclip
Clipboard-0.13/at/clipjoin-test
Clipboard-0.13/at/all-xclip-selections
Clipboard-0.13/at/all
Clipboard-0.13/at/run
Clipboard-0.13/lib/
Clipboard-0.13/lib/Clipboard/
Clipboard-0.13/lib/Clipboard/Win32.pm
Clipboard-0.13/lib/Clipboard/MacPasteboard.pm
Clipboard-0.13/lib/Clipboard/Xclip.pm
Clipboard-0.13/lib/Clipboard.pm
Clipboard-0.13/Makefile.PL
Clipboard-0.13/MANIFEST
Clipboard-0.13/Test/
Clipboard-0.13/Test/MockClipboard.pm
Clipboard-0.13/Test/Clipboard.pm
Clipboard-0.13/Changes
Entering Clipboard-0.13
Checking configure dependencies from META.yml
Running Makefile.PL
Configuring Clipboard-0.13 ... Checking if your kit is complete...
Looks good
Warning: prerequisite Win32::Clipboard 0 not found.
Generating a Unix-style Makefile
Writing Makefile for Clipboard
Writing MYMETA.yml and MYMETA.json
OK
Checking dependencies from MYMETA.json ...
Checking if you have ExtUtils::MakeMaker 0 ... Yes (7.74)
Checking if you have Win32::Clipboard 0 ... No
==> Found dependencies: Win32::Clipboard
Searching Win32::Clipboard on mirror file:///cygdrive/c/Users/somia/code-repositories/pinto/Clipboard ...
! Finding Win32::Clipboard (0) on mirror file:///cygdrive/c/Users/somia/code-repositories/pinto/Clipboard failed.
! Couldn't find module or a distribution Win32::Clipboard
! Installing the dependencies failed: Module 'Win32::Clipboard' is not installed
! Bailing out the installation for Clipboard-0.13.
So, the packages in my repository are as follows:
Edit
Investigation shows that Win32::Clipboard is packaged in Clipboard itself. Should this make a difference? Any ideas for how I can make this work as it's supposed to? Is there something about the way the Clipboard package is constructed that is abnormal or problematical?
The problem wasn't in Pinto or in cpanm."Clipboard::Win32" != "Win32::Clipboard", as caught by choroba below.
May 17, 2025 at 18:42 UTC
|
Largest integer in 64-bit perl
8 direct replies — Read more / Contribute
|
by harangzsolt33
on May 16, 2025 at 13:56
|
|
|
my $MAXQUAD = 18446744073709551615; # equals 0xffffffffffffffff
for (my $i = 0; $i <= $MAXQUAD; $i++)
{
printf("\n %f", $i);
}
exit;
Please ignore the fact that this will take forever to count from 0 to $MAXQUAD, but would it actually work? ...because in a 32-bit environment, this loop will never end! It will count from 0 to 9007199254740992, and it will not increment beyond that. It will just get stuck at that number.
|
|