Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

The Monastery Gates

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

Donations gladly accepted

If you're new here please read PerlMonks FAQ
and Create a new user.

New Questions
Eval/package question
4 direct replies — Read more / Contribute
by Ratazong
on Jan 21, 2020 at 05:13

    Dear Monks,

    I'm struggeling with packages and eval ... here is my short example:

    package items; use strict; use warnings; use Data::Dumper; use Exporter; my %items; 1; sub initItems { $items{1}{Name} = "Rusty Sword"; # fine $items{2}{Value} = 2; # fine eval ("$items{3}{Value} = 2;"); # ERROR: Use of uniniti +alized value in concatenation (.) or string at line 16. (th +is line) print( Data::Dumper->Dumpxs( [ \ %items ], [ qw{ *items } ] )); }
    When running it (calling items::initItems(); in my main program), I get the error mentioned above, and the output is:
    %items = ( '1' => { 'Name' => 'Rusty Sword' }, '3' => {}, '2' => { 'Value' => 2 } );
    What is wrong? Why does the eval somehow access my hash, but does not add the value? Is it something obvious and I'm just blind today?

    Thanks for help! Rata

Win32 wperl open Console when system
2 direct replies — Read more / Contribute
by fdesar
on Jan 21, 2020 at 04:02

    Dear Monks,

    I tried many things and I'm still stuck on a stupid problem with Window.

    I have a script running under wperl as I don't need any console window for my script, all its IO being handled by pipes.

    But I need to be able to call a cmd command to display a PDF file in the default viewer (ie explore).

    So I tried issuing the simple command :

    qx{start "$pdf" /b };

    and whatever I do (even using the /b option), it begins first by opening a console window before running the explorer.

    Do you think of a way to get rid of this silly unexpected console ?

    (Or any other way to display the PDF file...)

    Thanks for any advise,


Send password in Net::SSH::Expect
1 direct reply — Read more / Contribute
by gafaman
on Jan 21, 2020 at 02:59
    Dearest monks, I have been trying for some time now to create a script, using Net::SSH::Expect, to send the contents of the local file to another host's authorized_keys file. I thinkg I have managed to exec the command on the remote host properly, but I do not know how to send the password (which I must do once), stored in a variable, when it is is requested. This is my current code:
    #!/usr/bin/perl use strict; use warnings; use Net::SSH::Expect; print "Device IP: "; my $deviceIP = <STDIN>; chomp $deviceIP; my $user = "gafaman"; print "User password: "; my $pass = <STDIN>; chomp $pass; my $epass = "beholder"; my $login_timeout = 60; use POSIX qw(strftime); my $datestring = strftime "%F", localtime; my $ssh = Net::SSH::Expect->new( host => $deviceIP, user => $user, password => $pass, raw_pty => 1, no_terminal => 0, log_stdout => 1 ); $ssh->login(); #When the line below is executed, it ends with a "Password: " prompt $ssh->exec("cat \~\/\.ssh\/|ssh \'-o StrictHostKeyChecking=n +o\' bkpuser\@ \'cat \>\> \.ssh\/authorized_keys \&\& echo +\"KeyCopied\"\'",2); $ssh->waitfor('Password:\s*', 5) or die "prompt 'Password' not found a +fter 5 seconds"; $ssh->send("$epass"); $ssh->close();
    Could you please lend me some wisdom? Thanks! Gafaman
Application Performance Management (APM) for Modern Perl Web Apps
No replies — Read more | Post response
by silent11
on Jan 20, 2020 at 16:24
    APM tools like AppDynamics and Elastic APM support Java, PHP, Python, .NET and other various application languages out of the box.

    Using JavaScript includes (via script tags) from these APM tools I'm able to perform the RUM (Real user monitoring) of the front-end, however I'm looking for a way to instrument the actual execution on the server side to understand how long business logic is taking, or database calls, etc.. from a Mojolicious app, ideally within the same APM tool/suite so I can correlate metrics between the front-end, the application, and the host.

    Have any of you instrumented a "modern" Perl web app using either of these popular/commercial APM frameworks? (or even a different widely-used framework?) My goal is to correlate the RUM results with the APM metrics coming from the server, database, and other various tiers in this application stack.

    Thanks, silent11

extract the tail from a string (with new lines) containing a substring
4 direct replies — Read more / Contribute
by jjmoka
on Jan 20, 2020 at 12:36
    I have a file-A containing some logging info. Suppose 202 lines:
    <.... line 200 ....> <.... line 201 ....> <.... line 202 ....>
    These lines are the output of a periodical(ex. every 20min) grep (of a pattern xxx) on a certain live file-B If I just do every 20min something like
    grep -A 3 -B 3 xxx file-B >> file-A
    I could fill file-A with duplicates if following greps contain same lines (or parts) of previous greps. Every grep output is stored in a string
    $out = qx/grep ... / #note that $out contains new lines
    while the last line of file-A is obtained for example like:
    $last = qx/tail -1 $fileA/
    I'd need then to keep of $out only the <NEW> part (if any) I thought that
    if (($last =~ /\S/) && ($out =~/$last(.*)/)) { $out = $1 }
    should have done the trick, but it doesn't. Any help to fix my wrong logic is welcome.
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 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:

    <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; }
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?

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
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 browsing the Monastery: (5)
As of 2020-01-23 06:36 GMT
Find Nodes?
    Voting Booth?