Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Seekers of Perl Wisdom

( #479=superdoc: print w/replies, xml ) Need Help??

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
When aliasing sub arguments to @_ elements, PADTMP, READONLY flags are copied inconsistently
No replies — Read more | Post response
by vr
on Jan 17, 2020 at 18:38

    I wrote a fragment of code dealing with passing pointers to pointers to data for some FFI calls. Reducing it now to SSCC(?)E: if written as

    pack 'P', pack 'P', $data

    then Perl warns "Attempt to pack pointer to temporary value", so it's easy to debug. But in my case, it was something like:

    sub foo { pack 'P', $_[0] } foo pack 'P', $data;

    -- no warnings, correct result for single call, but incorrect if return values are not used immediately but e.g. stored. Then I ran some tests:

    >perl -MDevel::Peek -we "Dump 1; sub foo{Dump $_[0];$_[0]=1} foo(1)" SV = IV(0x653478) at 0x653488 REFCNT = 1 FLAGS = (IOK,READONLY,PROTECT,pIOK) IV = 1 SV = IV(0x653358) at 0x653368 REFCNT = 1 FLAGS = (IOK,READONLY,PROTECT,pIOK) IV = 1 Modification of a read-only value attempted at -e line 1. >perl -MDevel::Peek -we "Dump 1+1; sub foo{Dump $_[0];$_[0]=1} foo(1+1 +)" SV = IV(0x2612cf8) at 0x2612d08 REFCNT = 1 FLAGS = (PADTMP,IOK,READONLY,PROTECT,pIOK) IV = 2 SV = IV(0xfcb140) at 0xfcb150 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 2

    Why is not PADTMP flag copied to $_[0], and why, if present, it also prevents copying READONLY?

command line perl command to get between lines with non greedy match
7 direct replies — Read more / Contribute
by ravi_perl_monks
on Jan 17, 2020 at 14:31

    I know the following code perl -ne 'print if (/PATTERN1/../PATTERN3/)' is a greedy match and prints everthing.

    What I want is to print the following output


    Note this is extremely large file and can't put the whole file into a string.

    Thanks, Ravi

Dualvar via table
4 direct replies — Read more / Contribute
by Dirk80
on Jan 17, 2020 at 12:21

    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:

    #!/usr/bin/perl use strict; use warnings; use My::DualVar; 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; My::DualVar->tie($direction, \%table); print_dualvar($direction); $direction = 4; print_dualvar($direction); $direction = 'NORTH'; print_dualvar($direction); } # Case 2: Table str -> num print "Case 2: Dualvar via str2num table\n\n"; { my $direction = 'SOUTH'; My::DualVar->tie($direction, reverse %table); print_dualvar($direction); $direction = 4; print_dualvar($direction); $direction = 'NORTH'; print_dualvar($direction); } sub print_dualvar { print "as num: " . ($_[0]+0) . "\n"; print "as str: $_[0]\n"; print "\n"; }

    And here my written package:

    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.

Signal to a sleeping Perl program
6 direct replies — Read more / Contribute
by jerryhone
on Jan 17, 2020 at 09:12
    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!!!! Any thoughts? Jerry
Text file manipulation
1 direct reply — Read more / Contribute
by Sajn_00
on Jan 16, 2020 at 22:26
    Hi ,

    I have two files, I have to update file 1, with the values from file2. for ex: in file1, ON_DIE_FILLER/filler_fuse should get value 0xa

    I am having issues in comparing the names.

    Please help.
    file 1: ibl_ss_fuse_c0_r0/MRA_PVT_Tuning = 0x94, ibl_ss_fuse_c0_r0/S3M_SPARE_1B7_3to7 = 0x0, ON_DIE_FILLER/filler_fuse = 0x0, ON_DIE_FILLER/filler_byte = 0x0, file 2: die0_IAX_fuse_dino_c1_r0, die0_IAX_fuse_dino_c1_r0_IAX_fuse_iax_wr_ass +ist_pulsewidth, 0x5, die0_ON_DIE_FILLER, die0_ON_DIE_FILLER_ON_DIE_FILLER_filler_byte, 0xb, + die0_ON_DIE_FILLER, die0_ON_DIE_FILLER_ON_DIE_FILLER_filler_fuse, 0xa
Setting $0 clears /proc/PID/environ too
3 direct replies — Read more / Contribute
by kikuchiyo
on Jan 16, 2020 at 12:52

    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.

    #!/usr/bin/perl sleep 10; { local $0 = 'changed'; sleep 10; } sleep 10;

    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 -a -b -c", but after restoration it will just be "".

    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.

sockaddr_family, length is 0, should be at least 2
1 direct reply — Read more / Contribute
by vshah
on Jan 16, 2020 at 07:08
    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/ line 694 Please help
Linux file handle not working
2 direct replies — Read more / Contribute
by pvfki
on Jan 15, 2020 at 19:46

    Hi, I am trying to run a perl command through linux console using filehandle ($_ sign) in order to indicate a file to use when running the command. The commands from the code snippet below are from

    For example, I have the following number in input.txt: 147416345806550029415781910597841
    Running with Windows:
    >perl -Mntheory=:all -nE "chomp; say is_prime($_);" input.txt 1 >perl -Mntheory=:all -nE "chomp; say length($_);" input.txt 33
    Running on Linux however I get:
    >perl -Mntheory=:all -nE "chomp; say is_prime($_);" input.txt Parameter 'inputtxt' must be a positive integer at -e line 1, <> line +1. >perl -Mntheory=:all -nE "chomp; say length($_);" input.txt Parameter 'inputtxt' must be a positive integer at -e line 1, <> line +1.
    Does anyone know how to fix this? I'm not sure how perl (shell) via Linux works? How do I get the same output as Windows? Thanks for help!
Architecture advice, proxy or rebroadcast websocket
3 direct replies — Read more / Contribute
by Your Mother
on Jan 15, 2020 at 17:41

    I got some great advice for putting together my websocket client here: Mixing asynchronous data feed with synchronous program flow control. And it’s working well.

    10,000 foot view: I am consuming a stock price stream. This is a paid service that only allows one connection and it is somewhat fragile due to a short timeout period, a dearth of documentation, and no built-in caching/reconnecting. None of my plans breach the user agreement/contract; that’s not me.

    I don’t want to write a cache/queue. I want to leave the stream alone and uncluttered with a bunch of synchronous calls that might throw exceptions or otherwise cause trouble; separation of concerns. I also want to test multiple versions of code against the single stream concurrently. I could record and play back data but that’s a pain, a disk hog, and it doesn’t address the fragility/complexity or any possible application of code against live data.

    My ideas have been–

    • proxy the websocket through nginx so multiple “muted” client connections still only share the single permissible connection to the real service (I’m not sure this is possible without a lot of code and it’s extra plumbing),
    • write a websocket server, in Mojo still, around the websocket client that lets an arbitrary number of local clients receive an echo of the master client’s stream,
    • write a simple streaming socket server in Perl that runs the websocket client and pushes out to any local socket clients,
    • publish the data stream to a local DB (this would be slower than everything else if it’s relational but also perhaps the most reliable with a built-in “cache/queue” only in need of pruning),
    • or come beg you guys for what is probably a better idea than I can summon.

    So… I guess the last one. :P

One to one file output idiom
8 direct replies — Read more / Contribute
by Eily
on Jan 15, 2020 at 12:41

    Hello monks. I was using perl to translate files from one format to another with something like the following: perl -p > file.out when I got lazy and decided that I should be able to just pass a list of input files, and let perl append .out (or replace the extension) to create an output file for each input file.

    There are obviously more than one way to do it and I settled for:

    for my $filename (@ARGV) { open my $input, '<', $filename or die "Couldn't open $filename: $!"; open my $output, '>', $filename.'.new' or die "Couldn't open $filena +me: $!"; while (<$input>) { say $output translate($_); } }

    Now I'm wondering, is there some simpler way to do it? This looks like a pretty common task, and it is very close to what -i does, except the two files are swapped: I want the output in the file with the added extension and the original unchanged. There doesn't seem to be an option for it, so do you know of some hack (bring out your secret operators, I'm asking out of curiosity so I want even the bad answers :D ) or some module that would let me write my code without having to write the explicit while(<>) loop on the data?

    NB: yup I already just asked that in the CB and got some answers. Like choroba's proposition to call the file inside a shell loop to handle the output files.

    Edit: perl -pE "open STDOUT, '>>', qq<$ARGV.txt>; s/(.*)/\U$1/;" FILES works once :P (inspired by choroba's proposition)

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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (4)
    As of 2020-01-18 03:05 GMT
    Find Nodes?
      Voting Booth?