Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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
Tk add icon to popup menu
No replies — Read more | Post response
by IB2017
on Jan 19, 2020 at 04:48

    Hello monks

    this is surely a naive question, but me and Menus in Tk are not really good friends. How do I add an icon to a pop-menu contructed like this?

    #use strict; use warnings; use Tk; my $mw = tkinit(); my $CkAutomaticBackUp; my $b1 = $mw->Button( -text => "Create pop-up", ); $b1->grid(-row => 0, -column => 1,); Popup($mw, $b1); $mw->MainLoop; sub Popup{ my ($mw, $obj) = @_; my $menu = $mw->Menu(-tearoff=>0, -menuitems=>[ [command=>"Something", -command=>[sub {print "Hello"}, $obj,]], ]); $obj->menu($menu); $obj->bind('<ButtonPress>', ['PostPopupMenu', Ev('X'), Ev('Y'), ]) +; return $obj; }
Processing an encoded file backwards
2 direct replies — Read more / Contribute
by LanX
on Jan 18, 2020 at 13:47
    Hi

    lets say I wanted a sliding window to search a file from end to start.

    Could do this with seek and read in a loop.

    Now is seek operating on byte boundaries ,but read depends on the encryption layer.

    What's the best way than to read an encoded file, like in UTF-8, backwards ?

    Is read fail-proof when accidentally starting inside a wide character after a seek?

    Or is it better to open :raw and to search the next character (or line) boundary manually and to decode with Encode then?

    I'm aware of File::ReadBackwards , but want to understand the mechanisms better and operate on windows and not lines.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

    updates
    )
    DB<1> open $fh,'<','input2' DB<2> read $fh => $in, 100 DB<3> p tell $fh 114 # surprise DB<4> open $fh,'<:raw','input2' DB<5> p tell $fh 0 DB<6> read $fh => $in, 100 DB<7> p tell $fh 100 DB<8>

    ) 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)

sorting array of arrays
2 direct replies — Read more / Contribute
by Anonymous Monk
on Jan 18, 2020 at 12:23

    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:

    @ar = ([1,12],[8,3],[4,57],[22,5]); @sort = sort{$a[1]<=>$b[1]}@ar; foreach (@sort){ print "@$_\n";}

    The desired output is: 8,3 22,5 1,12 4,57. But all i'm getting is the same sorting as i started with. Tx in advance.

When aliasing sub arguments to @_ elements, PADTMP, READONLY flags are copied inconsistently
1 direct reply — Read more / Contribute
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
11 direct replies — Read more / Contribute
by ravi_perl_monks
on Jan 17, 2020 at 14:31
    PATTERN1 SOME INFO TEXT1 TEXT2 TEXT3 PATTERN2 SOME INFO PATTERN1 SOME INFO TEXT4 TEXT5 TEXT6 PATTERN3 SOME INFO

    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

    PATTERN1 SOME INFO TEXT4 TEXT5 TEXT6 PATTERN3 SOME INFO

    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
3 direct replies — 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 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.

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/IP.pm line 694 Please help

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

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

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (5)
    As of 2020-01-19 16:56 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?