Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
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 label align left not centered
1 direct reply — Read more / Contribute
by Anonymous Monk
on Jan 20, 2020 at 09:43

    Hoi, I just started with the Tk module and i am trying to get some text from an Entry widget to display on a label but it keeps being centered while i want it left aligned. I have been looking at internet sources but i can't bring it to work. Please help. Here is the code

    use strict; use warnings; use Tk; my $mw = MainWindow->new; my $entry = $mw->Entry(); my $label = $mw->Label(-justify=>'left'); my $but1 = $mw->Button(-text=>"go",-command=>\&go); $but1->place(-x=>10,-y=>10,-width=>50,-height=>20); $entry->place(-x=>10,-y=>40,-width=>50,-height=>20); $label->place(-x=>10,-y=>70,-width=>50,-height=>20); MainLoop; sub go{ my $text = $entry->get; $label->configure(-text=>$text)};
configuring mod_perl on Apache2.4 (conf & virtual host help)?
2 direct replies — Read more / Contribute
by knox
on Jan 20, 2020 at 00:20

    Hi, I need help configure my Apache with mod_perl.

    I have just installed mod_perl on via sudo apt-get install -y libapache2-mod-perl2 on Ubuntu 18. I am having a hard time figuring out what the next steps our to test perl script. I am looking at https://perl.apache.org/docs/2.0/user/handlers/http.html and trying to understand what would go in my virtual hosts file and what else I need to configure to get perl script working for Apach24 setup.

    I've tried a few virtual host directives such as the one found here (with my own settings), but with no luck: https://wiki.archlinux.org/index.php/Apache_HTTP_Server/mod_perl

    <VirtualHost perlwebtest:80> Servername perlwebtest DocumentRoot /srv/http/perlwebtest ErrorLog /var/log/httpd/perlwebtest-error.log CustomLog /var/log/httpd/perlwebtest-access.log combined <Directory /srv/http/perlwebtest> AddHandler perl-script .pl PerlResponseHandler ModPerl::Registry Options +ExecCGI PerlOptions +ParseHeaders AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost>

    Currently, its not rending any of Perl Script. I feel like I've missed an obvious step. Any ideas of what I should try next?

    .
Tk add icon to popup menu
1 direct reply — Read more / Contribute
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
2 direct replies — 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
9 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

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 cooling their heels in the Monastery: (9)
    As of 2020-01-20 16:59 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?