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.
²) I've been asked what I mean with "sliding window", please see this sliding window description. There I start from the beginning, but it's often favorable to start from the end. (choroba++ for pm'ing me)
Hi all, I am trying to sort an array of arrays, but i am stuck. I have red some sources on the internet, but it is not helping me enough. I hope someone can tell me why i am failing. I am trying to sort this array on the second element. Here is the code:
I often have the case when reading a binary value from a file that it also has a textual representation. So I can use the dualvar functionality of Scalar::Util to solve this issue.
But that's not enough for me. Usually I have a corresponding table which contains the valid values and its representations. To avoid doing the same checks so often in code, I decided to tie the read scalar variable to a package which is doing these checks for me. Additionally on a change it shall always update the numeric and string context.
Here an example:
my %table = ( 1 => 'NORTH',
2 => 'SOUTH',
3 => 'EAST',
4 => 'WEST' );
# Case 1: Table num -> str
print "Case 1: Dualvar via num2str table\n\n";
my $direction = 2;
$direction = 4;
$direction = 'NORTH';
# Case 2: Table str -> num
print "Case 2: Dualvar via str2num table\n\n";
my $direction = 'SOUTH';
My::DualVar->tie($direction, reverse %table);
$direction = 4;
$direction = 'NORTH';
print "as num: " . ($_+0) . "\n";
print "as str: $_\n";
This code is working. But I would be interested in your opinion. What could I do better?
And the second thing. Currently my code would work randomly if the values of the given table (hash) are not unique. Is there an efficient way to check whether the values of a hash are unique? Then I would reject such a hash
Or would there be a solution to return several values if the hash is not unique, e.g. key 2 and 5 would have value 'SOUTH'?
In numeric context it would not work. If I would set a dualvar variable to 'SOUTH', then I would have to return 2 or 5 in the FETCH-method. Perhaps in numeric context the smaller value should be returned.
In string context I think it is possible. Because I could give back a concatenated string and I still would have a scalar.
I have an Autosys triggered Perl script that does a load of stuff and then sleeps for 15 minutes before doing it all again. I need to find a way to cleanly exit the program but I can't get it to respond to any signal handlers other than KILL and ALRM - both cause an immediate termination. If I add an ALRM signal handler, the ALRM stops working i.e. program keeps running!!!!
As the title says: on Linux, if you assign anything to $0 (with the intent to change the program's name as displayed by ps et al.), not just the the program's name and arguments are changed, but the environment (as shown in /proc/PID/environ) is cleared as well, or more precisely, filled with spaces.
The perlvar entry for $0 contains a paragraph that vaguely alludes to this:
In some platforms there may be arbitrary amount of padding, for
example space characters, after the modified name as shown by
"ps". In some platforms this padding may extend all the way to
the original length of the argument area, no matter what you do
(this is the case for example with Linux 2.2).
So I kind of understand what happens here and why, I just find it rude.
It is somewhat more concerning that the effect persists even if you localize $0.
When you run the program above, and watch a process list in a different terminal, you can observe that the apparent process name changes to 'changed' after 10 seconds, then changed back again, but if you watch the contents of /proc/PID/environ at the same time, you can see that it gets filled with spaces, then doesn't change back.
Two additional things to note:
The fake process name (as assigned to $0) spills over the memory that formerly contained the environment, so if you do something like $0 = 'changed' x 10000, /proc/PID/environ will contain something like "angedchangedchanged...changed\x00 ...".
When the localized $0 goes out of scope, Perl only makes a weak attempt to change the process name back to its original. So if you originally ran the program with arguments, the output of ps contained something like "perl foo.pl -a -b -c", but after restoration it will just be "foo.pl".
I think that an argument could be made that Perl should try to preserve the contents of /proc/PID/environ when changing $0, and do a more thorough job of restoring the original command line if $0 is localized.
Getting error in perl version 5.16.3
wpnInterfaceSoapServer25215:000 catch(): wpnSoapForkingDaemon::clientSoapRequestHandler(): Bad arg length for Socket::sockaddr_family, length is 0, should be at least 2 at /usr/share/perl5/vendor_perl/IO/So
cket/IP.pm line 694