Adding a new key-value pair to a nested anonymous hash
4 direct replies — Read more / Contribute
|
by Intrepid
on Jan 15, 2025 at 21:22
|
|
|
I'm looking for help understanding dereferencing with a complex data structure. I have an array of references to anonymous arrays which in turn contain a scalar as a the first item and a reference to an anonymous hash as the second item:
push @lecshun, [$barename , {package=>$gem, index=>$dexi}];
For some items in the array I want to add another key-value pair to the hash, but I just cannot figure out how to do that. I have only gotten so far:
$lecshun[$dexi - 1] ##? <<-- what to do here ?## = inv($gem);
The subroutine inv is this, although it could be anything:
sub inv {
my $pkg = $_[0];
my @pki = qx{cygcheck -i --inst $pkg};
for my $ln (@pki) {
next unless $ln=~/^Version\s+:\s(\S+)/;
say "Pkg ".$1;
return (datum => $1);
}
}
Maybe I am making this too complicated for myself.
Jan 16, 2025 at 02:07 UTC
A just machine to make big decisions
Programmed by fellows (and gals) with compassion and vision
We'll be clean when their work is done
We'll be eternally free yes, and eternally young
Donald Fagen -> I.G.Y.
(Slightly modified for inclusiveness)
|
Viewing the CPAN name space hierarchy
1 direct reply — Read more / Contribute
|
by RonW
on Jan 15, 2025 at 13:40
|
|
|
cpan.org used to have a link to the root of the name space hierarchy. I went there to look, but I couldn't find any way to view the hierarchy.
It was pretty obvious, before, but now it's either gone or well obfuscated.
Anyone know what changed?
|
Naming a module that extracts subsets of a Linux system
3 direct replies — Read more / Contribute
|
by NERDVANA
on Jan 15, 2025 at 01:08
|
|
|
Once again, I have a difficult-to-name module.
Given a fully installed Linux (or BSD?) file tree, this module lets you select the files, directories, symlinks, device nodes, etc which you want to become part of a reduced system image. The purpose is to generate Linux initrd, or docker images, or portable chroots, or embedded system images. The cool features are:
- It inspects the 'ldd' output on ELF binaries to determine which libs need added
- It preserves permissions and mtimes of files and the directories that contain them (unlike mkdir -p)
- It can move executables to new locations (like moving them to /newprefix/bin/)
- It can rewrite interpreter paths of scripts to match their new location
- It can rewrite the lib paths of ELF files so that they can live alongside an incompatible libc ecosystem
I'm actually not even sure it belongs on CPAN, because while I keep writing code like this, it's always fairly specialized and I have no idea if my hacks and special cases will be useful for a different environment.
I'm leaning toward "Linux::SystemSlicer" (i.e. like a slice of an array) but other phrases that come to mind are "Filesystem Subset Extractor", "Root Image Collector", "Initrd Builder", or "System Minifier". I'm only planning to ever use it on Linux, but I think it could theoretically be applied to BSD? There are probably a huge number of additional special cases that would be needed for anything other than Linux.
|
Module to open and close browser tabs
3 direct replies — Read more / Contribute
|
by justin423
on Jan 10, 2025 at 11:48
|
|
|
I want to do a quickie workflow tool to quickly scan web pages. (on both an Ubuntu and a windows machine)
I use Open::Browser on the Linux machine, and a system (start, $url) on the Windows machine.
Is there an extension that will open a new tab, then has a command to close the tab it just opened? (And just the tab, not any others after keeping it open for a pre-determined number of seconds using the sleep function)
It does not seem that any function or perl module will close a tab after opening it
|
[OT] German language resources for neologisms
3 direct replies — Read more / Contribute
|
by Discipulus
on Jan 10, 2025 at 02:48
|
|
|
Hello there!
A special friend of mine is tasked to spot 700 neologisms intruduced in German during last ten years. Being Perl so good at data and language processing, being this community populated also by some linguist and a bounch of german ones I hope I can get some useful tip from your part.
In detail, to be helpful in their research using Perl I'd like to be aware of:
- free online resources for German language, like dictionaries, modern and updated
- freely accesible corpora to scan, this should be indexed by time of the source
- similar studies made in this field recently
- german linguistic online resource in general
I supposed that ideally if I can access a decent corpus of last ten years and decent vocabulary issued 10 years ago I can be able to spit out some difference. Ok no so trivial but feasible.
As last option if 10 german monks want to put here 70 different neologism each one.. I'm done :)
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
|
Sorting path names with Sort::Key
2 direct replies — Read more / Contribute
|
by Anonymous Monk
on Jan 09, 2025 at 21:46
|
|
|
use List::Util qw(min);
my @path = qw(
/abc
/abc/1/2/a/b/c
/abc/2
/abc/def/1/2/a/b/c
/abc!def/1/2/a/b/c
/abc-def/1/2/a/b
/abcdef/1/2/a/b/c
/usr/a
);
say 'Wanted path order:';
say ' 'x4, $_ for @path;
my @sorted = sort @path;
say 'Lexical order (default sort):';
say ' 'x4, $_ for @sorted;
say 'Ordered by dirname, then basename:';
@sorted = map { $_->[0] }
sort { $a->[1] cmp $b->[1] or $a->[2] cmp $b->[2] }
map { [$_, m{^(.*/)?(.*)}s ] }
@path;
say ' 'x4, $_ for @sorted;
say 'Ordered by path components:';
@sorted = sort bypath @path;
say ' 'x4, $_ for @sorted;
say 'Ordered by path components (with caching):';
@sorted = map { $_->[0] }
sort {
for my $i (0 .. min $#{$a->[1]}, $#{$b->[1]}) {
return $a->[1][$i] cmp $b->[1][$i] if $a->[1][$i] ne $b->[
+1][$i];
}
return $#{$a->[1]} <=> $#{$b->[1]};
}
map { [$_, [split '/'] ] }
@path;
say ' 'x4, $_ for @sorted;
exit;
# https://stackoverflow.com/questions/5124994/sorting-directory-filena
+mes-with-perl/19506331#19506331
sub bypath {
my @a = split m'/', $a;
my @b = split m'/', $b;
for (my $i = 0; $i <= $#a; $i++) {
last if $i > $#b;
return $a[$i] cmp $b[$i] if $a[$i] cmp $b[$i];
}
return $#a <=> $#b;
}
|
PerlTk, LINUX, GNOME3: how NOT to affect clipboard by selection from list?
1 direct reply — Read more / Contribute
|
by kaza_perl_ip
on Jan 09, 2025 at 00:47
|
|
|
Hello!
I've wrote a PerlTk script which opens and brings to front an existing (or minimized) editor window
which I select from a list. The values shown in the list are paths of files.
I noticed that immediately after opening a window in such a way, the clipboard contents
are the path of the file which I selected. Since many times I would like to select some text in one
editor window, then open another window and paste the selected text there, I would prefare that
making selection from a list would not affect the clipboard at all. I attempted googling
but it goes astray to various ways of manipulating the clipboard while I need the exact opposite:
totally NOT affecting the clipboard by my selection.
How to do it?
TIA!
|
Does perl have a builtin limit to the size of shared memory segments I can write to?
1 direct reply — Read more / Contribute
|
by Anonymous Monk
on Jan 07, 2025 at 12:52
|
|
|
Dear Perl Monks,
I am willing to use shared memory to load in a large (4 Gb) segment of shared memory a set of ~ 4 billion packed integers.
I would like to do so, because later I want several parallel workers to be able to read each nth packed integer and use it, so that from each worker, I should be able to access the 1000000th packed entry just by:
my $packed;
my $n =1000000-1;
my $offset = $n*4;
my $success = shmread($id, $packed, $offset, 4)
The rationale here is sharing a very large index where each packed integer refers to a specific item. ~ 3000000000 items, thanks to a simple bijective hashing function, can be mapped to integers in the range (0..4000000000) so that each worker will easily know at which offset it should look into the shared memory to retrieve the corresponding packed piece of information for each item. About ~1000000000 empty fields (because the item corresponding to that position is missing) contain just 4 nul bytes (i.e. pack("N",0)).
However, when in the parent process I try just to create the shared memory object, by iteratively reading 1 million bytes from the index file and copying them into the shared memory, like this:
use warnings;
use strict;
use IPC::SysV qw(IPC_PRIVATE IPC_RMID S_IRUSR S_IWUSR);
open(my $idx, "<", "$ARGV[0].idx") || die "cannot open data file\n $!"
+;
my $idx_size = (split(' ',`wc -c $ARGV[0].idx`))[0];
my $idx_id = shmget(IPC_PRIVATE , $idx_size, S_IRUSR | S_IWUSR) || die
+ "shmfet $!";
my $offset = 0;
foreach my $i (0..$idx_size/1000000) {
my $n="";
read($idx,$n,1000000);
shmwrite($idx_id, $n, $offset, 1000000) || die "shmwrite: $!";
$offset +=1000000;
}
shmctl($idx_id, IPC_RMID, 0) || die "shmctl: $!";
close $idx;
exit;
I always get an error "shmwrite: Bad address ". This happens always when writing the 2^32 th byte. So it looks like the shared memory segment perl can handle is limited to 2 Gb.
however, running
$ ipcs -m
shows that perl actually reserved a shared memory segment much larger than that (actually, the expected 4 Gb):
------ Shared Memory Segments --------
key shmid owner perms bytes nattch stat
+us
0x00000000 8454205 valerio 600 4294967296 0
I am running perl v 5.34 on an ubuntu 22.04 w/ 32 Gb RAM.
perl here should be a 64-bit process:
$ perl -V:archname
archname='x86_64-linux-gnu-thread-multi';
Is this a builtin limit of perl shared memory, or is there anything that I am missing?
Thanks for your wisdom,
Valerio
|
How to swap columns in a csv file ?
1 direct reply — Read more / Contribute
|
by xuo
on Jan 05, 2025 at 09:05
|
|
|
Hello Perl's Monks,
I've got the following issue :
I've got a csv file (where fields are separated with , (commas)) and that can have , (commas) inside the fields.
This is the reason why I've used the Text::CSV module.
My goal is to remove some unused fields (columns) and to swap some of them.
Input file :
column1,column2,column3,column4,column5,column6
datar1c1,datar1c2,datar1c3,datar1c4,datar1c5,datar1c6
datar2c1,"data,r2c2",datar2c3,datar2c4,datar2c5,datar2c6
datar3c1,datar3c2,"data, r3c3",datar3c4,datar3c5,datar3c6
Expected output file :
column2,column1,column4,column3
datar1c2,datar1c1,datar1c4,datar1c3
datar2c2,datar2c1,datar2c4,datar2c3
datar3c2,datar3c1,datar3c4,"data, r3c3"
In this example, I want to remove the 2 last columns and swap columns 1 and 2 and columns 3 and 4.
The following code removes the columns 5 and 6 but I don't know how to swap the columns.
#!/usr/bin/perl
use strict ;
use warnings ;
use Text::CSV ;
use Getopt::Long qw(GetOptions) ;
my $inputFile = '' ;
my $outputFile = '' ;
my $csv = Text::CSV->new ();
GetOptions( 'file:s' => \$inputFile,
'out:s' => \$outputFile,
) or die("Error in command line arguments\n") ;
open(my $data, '<:encoding(utf8)', $inputFile) or die "Could not open
+'$inputFile' \n" ;
open(my $out, '>:encoding(utf8)', $outputFile) or die "Could not open
+'$outputFile' \n" ;
while (my $fields = $csv->getline( $data ))
{
splice @$fields, 4, 2 ;
$csv->print($out, $fields) ;
print $out "\n" ;
}
close $data ;
close $out ;
Could you help me ?
Regards.
Xuo.
|
variable import from package fails even though package apparently loads
2 direct replies — Read more / Contribute
|
by spoonervt
on Jan 03, 2025 at 21:44
|
|
|
I know that exporting variables/functions is bad style possibly leading to collisions, but I expected it to work.
I have a scenario where I am placing a package (w.pm) in a subdirectory and when doing so the export/import seems to fail.
when I load the package with:
use w; # the package lives in one of the directories in @INC
everything works as expected -> exported variables and functions are available in (and pollute) main's namespace.
When I load the package with:
use A::w; # ./A is not in @INC
the package loads fine (the BEGIN{} startup message is issued) and I can use variables/functions when specifying the package name with w::, but not without it.
Am I misreading how to use Exporter, @EXPORT, or is my understanding of how to create packages in a folder hierarchy wrong?
Any insight is appreciated.
-Steffen (using v5.8.8)
main:
#!/tool/pandora64/bin/perl -w -I ./
use strict;
use warnings;
use v; # variables/funcs work without v::, the package code is a
+lmost the same)
#use w; # This makes variables/funcs work without w::
use A::w; # this requires w:: to identify the package
sub_in_v();
w::sub_in_w();
$var_in_v = "v-variable";
$w::var_in_w = "w-variable"; # works only because of the w:: when use-
+ing A::w
printf("%s\n", $var_in_v);
printf("%s\n", $w::var_in_w); # works only because of the w:: when us
+e-ing A::w
print(join("\n", @INC));
1;
package w:
package w;
# in ./A
use Data::Dumper;
use Exporter;
our $var_in_w;
our @EXPORT= ('$var_in_w', 'sub_in_w');
our @ISA = qw(Exporter);
sub sub_in_w{
print("this is from a sub in package SRAM::w!\n");
}
BEGIN{
print('this is w\'s BEGIN, @ISA = ', Dumper @ISA, "\n");
1;
}
END{
print("w ending now.\n");
}
1;
|
|