http://www.perlmonks.org?node_id=479

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
Mojolicious global variable
1 direct reply — Read more / Contribute
by bartrad
on Dec 13, 2018 at 14:53

    Evening monks, I'm after some of your wisdom.

    I have a Mojolicious lite app that works as I would expect for a single user. However, if two users are accessing it at the same time, the value of $router is changing depending on who submitted the request last.

    For example, if user A submits the form to query 'RouterA' then the information is displayed for that router. However, when user B then submits the form to query 'RouterB', the session for user A and user B displays the information for 'RouterB'. I figure it must be since I am declaring $router (and other things) globally but I'm not sure how to store this and pass it around the different routes I have...

    my ( $router, $cfg, $ip ); get '/' => sub { my $c = shift; $c->render( template => 'index' ); }; post '/' => sub { my $c = shift; ($router) = $c->param('router'); app->log->info("Router $router"); $cfg = get_config($router); $ip = $cfg->get_ip(); $c->render( template => 'result', router => $router, ip => $ip, ); } => 'index'; get '/info' => sub { my $c = shift; $c->render( template => 'result', router => $router, ip => $ip, ); }; get '/cards' => sub { my $c = shift; my %cards = $cfg->get_cards(); $c->render( template => 'cards', router => $router, cards => \%cards + ); }; app->start;

    The reason I have globally declared the variables is so I can pass them between the different routes and have the value of $router preserved and displayed on each resultant HTML page. Any help greatly appreciated as I'm scratching my head with this one!

Net::SSH::Perl and Net::SSH::Perl::Agent
1 direct reply — Read more / Contribute
by arc444
on Dec 13, 2018 at 14:16

    Hi. Can anyone advise how I can get my Net::SSH::Perl object to use a particular key I have identified in my Net::SSH::Perl::Agent object ?

    I have deliberatley simplified down - I will actually iterate over all available keys until I find the one I want ( based on 'comment' ) and then wish to use that particular key

    Im unable to try all keys as my target host only allows a few attempts, and I have many keys loaded into the agent ( reducing these available keys is not an option )

    Many Thanks !

    # Ensure the invoking process has a valid ssh-agent running, and is lo +aded with an appropriate key, or keys use warnings; use strict; use Net::SSH::Perl::Key; use Net::SSH::Perl::Agent; my $agent = Net::SSH::Perl::Agent->new(2); my ($key, $comment) = $agent->first_identity; use Net::SSH::Perl; my $ssh_user = 'myuser'; my $ssh_host = 'myhost'; ## How to use the above referenced $key in the following ssh connectio +n ?? ## my %ssh_params = ( debug => '1', options => [ "StrictHostKeyChecking n +o", "UserKnownHostsFile /dev/null" ] ); my $ssh = Net::SSH::Perl->new( $ssh_host, %ssh_params ); my $result = $ssh->login("$ssh_user"); if ( $result ) { print "Logged in as ${ssh_user} on host ${ssh_host}\n"; } else { print "Failed to login in as ${ssh_user} on host ${ssh_host}\n"; }
Perl launching process
5 direct replies — Read more / Contribute
by pwagyi
on Dec 13, 2018 at 05:17

    Greeting monks! I have been using perl system, backtick for some time to invoke other executables. But then I came across IPC::Cmd run, run_forked, I realize that it's somewhat better in a sense that I can easily access stdout, stderr and also exit code. So the question is when is it appropriate to use each system, backtick, or other modules? Is there best practice?

Net::SSH2 error - Username/PublicKey combination invalid
1 direct reply — Read more / Contribute
by BernieC
on Dec 12, 2018 at 21:05
    I'm back playing with Net::SSH2 and running into trouble again. My code is still the very very simple
    my $ssh2 = Net::SSH2->new() ; $ssh2->connect(HOST) or $ssh2->die_with_error ; $ssh2->check_hostkey(tofu => HOSTKEY) or $ssh2->die_with_error ; $ssh2->auth_publickey(USER, PUBLICKEY, PRIVATEKEY) or $ssh2->die_with_error ; $ssh2->auth_ok() ;

    Here's what's happening: I have a real SSH client that uses a single SSH keyprofile for two different unix systems. I've double checked and the and authorized.keys files on both servers are identical. I have the two constants: HOST and USER. when I changed the HOST and USER to be the new system and the username to use on that system, I get

    Username/PublicKey combination invalid (-18 LIBSSH2_ERROR_PUBLICKEY_UN +RECOGNIZED ) at D:\Perl\ondreamhost.pl line 28.
    I have no idea what it is trying to tell me. What do usernames have to do with SSH keys?
folding Data::Dumper output
3 direct replies — Read more / Contribute
by morgon
on Dec 12, 2018 at 18:04
    Hi,

    sometimes I have the problem of having to look at large Data::Dumper or json dumps.

    With json I can simply load them into vim and with "foldmethod=syntax" I can easily fold them and so navigate easily the hierarchies of large documents.

    Unfortunately I have yet to figure out how to fold Data::Dumper output in vim in a similar way.

    Does someone have a tip here?

    Many thanks and sorry for posting a question that may more pertain to vim than to perl...

Killer byte tripping up Perl?
4 direct replies — Read more / Contribute
by echo5
on Dec 12, 2018 at 17:22

    I have a simple script that appears to show that some byte in the output is causing Perl to behave strangely.

    The script calls a command that coughs up 150 bytes or so of binary data. One stream of returned data behaves as expected. Another stream causes chaos.

    The script: $cmd = "/usr/local/bin/mycmd"; print "The cmd to be run is: $cmd \n"; open(CMD, "$cmd |" ) or die "Can't run '$cmd'\n$!\n"; while (<CMD>) { chomp; print "My raw output is: $_ \n"; $dataout = $_; print "My DATAOUT is: $dataout \n"; }

    When I run the above script in a "good" scenario I get the below output:

    The cmd to be run is: /usr/local/bin/mycmd My raw output is: &#65533; ` /11&#65533;%_$&#65533;%f$&#65533;&c$&#65 +533;%a$&#65533;%\$&c$&^$&#65533;&i$ My DATAOUT is: &#65533; ` /11&#65533;%_$&#65533;%f$&#65533;&c$&#65533; +%a$&#65533;%\$&c$&^$&#65533;&i$

    When I run the above script in a "bad" scenario I get the below output:

    The cmd to be run is: /usr/local/bin/mycmd My raw output is: My DATAOUT is: My raw output is: &#65533;0]&#65533;c &#65533;&#65533; My DATAOUT is: &#65533;0]&#65533;c &#65533;&#65533;

    Ultimately the goal is to ingest the data and process it using unpack but that was failing as $_ didn't contain data is should contain to process. The "bad" behavior above seems to show that there is some sort of "killer byte" being output from mycmd that throws a wrench into things. Below is the good and bad output in hex form via xxd. Is there a byte in there tripping up Perl?

    Good data stream: 0000000: 0202 00d0 0000 0000 0000 0000 0100 0000 ................ 0000010: 0100 0000 0100 0000 0100 0000 0100 0000 ................ 0000020: 0100 0000 0100 0000 0500 0000 0500 0000 ................ 0000030: 0500 0000 0500 0000 0500 0020 0100 0000 ........... .... 0000040: 0100 0000 0100 0000 0100 0000 0100 0000 ................ 0000050: 0100 0000 0100 0000 0500 0000 0500 0000 ................ 0000060: 0500 0000 0100 0000 0100 0000 0000 0000 ................ 0000070: 0200 0060 0100 0020 0000 0000 0100 2d00 ...`... ......-. 0000080: 0100 2f00 0100 3000 0000 0000 0100 0000 ../...0......... 0000090: 0000 0000 0102 ca25 0102 4a24 0102 bd25 .......%..J$...% 00000a0: 0102 5024 0102 da25 0102 4c24 0102 d525 ..P$...%..L$...% 00000b0: 0102 4c24 0102 c325 0102 4624 0102 e025 ..L$...%..F$...% 00000c0: 0102 4e24 0102 e225 0102 4824 0102 dd25 ..N$...%..H$...% 00000d0: 0102 5224 ..R$
    Bad data stream: 0000000: 020a 009c 0000 0000 0000 0000 0100 0000 ................ 0000010: 0100 0000 0100 0000 0100 0000 0100 0000 ................ 0000020: 0100 0000 0100 0000 0100 0000 0500 0000 ................ 0000030: 0500 0000 0500 0000 0500 0000 0500 0000 ................ 0000040: 0500 0000 0500 0000 0500 0000 0500 0000 ................ 0000050: 0500 0000 0500 0000 0500 0000 0500 0000 ................ 0000060: 0500 0000 0500 0000 0100 0000 0000 0000 ................ 0000070: 0100 3000 0000 0000 0100 005c 0100 00b4 ..0........\.... 0000080: 0000 0000 0100 0000 0000 0000 0200 0063 ...............c 0000090: 0100 0020 0000 0000 0103 9206 0103 8506 ... ............
Preventing Use of uninitialized value.
4 direct replies — Read more / Contribute
by Anonymous Monk
on Dec 12, 2018 at 13:50
    Hi Monks

    I am reading the last second line from a log file, my question is how to avoid "Use of uninitialized value" when the file I am reading/open is empty.

    Here is some code to show where its happening :
    ... # Read the last 2 lines of a log file my $log = "log.log"; # if this file is still empty by the time the co +de runs. my $line = do { open my $line, '-|', tail => -2, $log or die "Can't spawn tail: $!\n"; <$line>; }; $line =~ s/[\r\n]+$//; my $show = ''; # <<<< happens here, uninitial +ized value if ($line =~ /(\bTest\b\s+.+\s+\baccount\b)?/) { #<<<< happens here, + uninitialized value $show = $1 || ''; } ...

    Thanks for looking!
grep beginning of string
2 direct replies — Read more / Contribute
by IB2017
on Dec 12, 2018 at 12:40

    Hello monks,

    dumb question, but I am trying to change the following grep:

    my @tags = split /\t/, $line; my @Definition=("NN", "NNS"); next unless (grep {$_ eq $tags[ 1 ]} @Definition);

    in order that it matches only at the beginning of the string. I thought that the following would do it, but I was wrong...:

    next unless grep($_ =~ /^$tags[1]/, @Definition);
dbicdump selected tables
1 direct reply — Read more / Contribute
by Ea
on Dec 12, 2018 at 11:05
    Hi all,

    I'm trying to dbicdump an Oracle database and I get a fatal error on a table that I'm not even interested in. ORA-24345: A Truncation or null fetch error occurred (DBD ERROR: ORA-01406 error on field 1 of 1, ora_type 8,LongReadLen too small and/or LongTruncOk not set

    How can I tell dbicdump that I only want certain tables or views? I'm not seeing anything likely using the -o switches.

    Many thanks,

    Edit: looking at constraints and trying to get them to work.

    Ea

    Sometimes I can think of 6 impossible LDAP attributes before breakfast.

    YAPC::Europe::2018 — Hmmm, need to talk to work about sending me there or to Mojoconf.

Grep Pattern
3 direct replies — Read more / Contribute
by GotToBTru
on Dec 12, 2018 at 09:29

    I want to apply a F T T F repeating pattern as a filter to an array or list. Here is what I came up with:

    $i = 0; @result = grep { $i = 0 unless ($i<4); $i++%3 ? 1 : 0; } 0..12

    @result = 1,2,5,6,9,10

    It works but looks clunky. Any more elegant options?

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)


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.