Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
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
text alignment
3 direct replies — Read more / Contribute
by Anonymous Monk
on Apr 21, 2017 at 13:13

    Hi I am new to Perl and am using Padre IDE. How do I align the text in a print statement

    print "How do I align this statement so that it does not look like a r +un on sentence and take up the whole screen? \n";
Method for reducing tedium of transferring object properties to scalars
6 direct replies — Read more / Contribute
by nysus
on Apr 21, 2017 at 12:01

    Greetings fellow Monks. It's been a few weeks. I'm happy to report I'm making good progress on a fairly extensive project of mine thanks to all the kind help found here.

    So one thing that is annoying me right now is the process of populating scalars with object properties to make code more readable. For example:

    sub function { my $self = shift; my $val1 = $self->get_value1; my $val2 = $self->get_value2; my $val3 = $self->get_value3; print "Values: $val1, $val2, $val3"; }

    For me, I find transferring object properties to scalars is less error prone and makes code easier to read than something like: print "Values: " . $self->get_value1 . ', ' . $self->get_value2 . ', ' . ', ' . $self->get_value3"; or printf "Values: %s, %s, %s", $self->get_value1, $self->get_value2, $self->get_value3";

    I know it's possible to do print "${\$self->get_value}" but that is just god awful.

    Using scalars is also useful if I have to use a property several times in the same block of code or if there are many object properties to keep track of. It saves a lot of typing and, again, makes the code easier to read. But it still can be super annoying to create these scalars. I'm wondering if there might be some useful trick out there that will spare me the oh so tedium of creating scalars for my object properties. Thanks.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

Transferring hash keys to array... Need help sorting
5 direct replies — Read more / Contribute
by DARK SCIENTIST
on Apr 20, 2017 at 22:38

    Hello fellow Monks, So I'm working on a program this week, and I'm having some trouble with sorting... I have a hash, and I want to transfer the keys to this hash to an array called "labels". I want to transfer the values of the hash to an array called "content". This works fine. My problem is that nothing is ordered. The labels consist of "title 1", "title 2" etc The array references should match one other. So for instance... $labels[0] and $content[0] should correspond to one another but they don't because the hash keys and values are going into the array elements in a random order every time I run my code. I tried using something like "@labels = sort keys %hash" but this doesn't work. Suggestions or insight are much appreciated. -DARK SCIENTIST

Can't get XML::RSS working on a new computer
1 direct reply — Read more / Contribute
by wintermute115
on Apr 20, 2017 at 20:37
    (This is a continuation of this thread) I have a script I've been using to pull down podcasts for several years, and I've just gotten a new computer and I'm having difficulty getting it to work again. I went from Ubuntu 14.04 with perl 5.18 to Ubuntu 17.04 with perl 5.24. I think the only problem I have remaining is that XML::RSS doesn't seem to want to run. cpan says it's installed and up to date, but when the script runs, it outputs:
    $ ./scripts/podcasts/podcasts.pl Use of uninitialized value in subroutine entry at /usr/local/share/per +l/5.24.1/Role/Tiny.pm line 461. Can't call method "_attrs" on an undefined value at /usr/local/share/p +erl/5.24.1/Specio/OO.pm line 168. Compilation failed in require at /usr/local/share/perl/5.24.1/Specio/D +eclare.pm line 11. BEGIN failed--compilation aborted at /usr/local/share/perl/5.24.1/Spec +io/Declare.pm line 11. Compilation failed in require at /usr/local/lib/x86_64-linux-gnu/perl/ +5.24.1/DateTime/Types.pm line 12. BEGIN failed--compilation aborted at /usr/local/lib/x86_64-linux-gnu/p +erl/5.24.1/DateTime/Types.pm line 12. Compilation failed in require at /usr/local/lib/x86_64-linux-gnu/perl/ +5.24.1/DateTime/Duration.pm line 12. BEGIN failed--compilation aborted at /usr/local/lib/x86_64-linux-gnu/p +erl/5.24.1/DateTime/Duration.pm line 12. Compilation failed in require at /usr/local/lib/x86_64-linux-gnu/perl/ +5.24.1/DateTime.pm line 14. BEGIN failed--compilation aborted at /usr/local/lib/x86_64-linux-gnu/p +erl/5.24.1/DateTime.pm line 14. Compilation failed in require at /usr/local/share/perl/5.24.1/DateTime +/Format/Mail.pm line 7. BEGIN failed--compilation aborted at /usr/local/share/perl/5.24.1/Date +Time/Format/Mail.pm line 7. Compilation failed in require at /usr/local/share/perl/5.24.1/XML/RSS/ +Private/Output/Base.pm line 9. BEGIN failed--compilation aborted at /usr/local/share/perl/5.24.1/XML/ +RSS/Private/Output/Base.pm line 9. Compilation failed in require at /usr/local/share/perl/5.24.1/XML/RSS. +pm line 9. BEGIN failed--compilation aborted at /usr/local/share/perl/5.24.1/XML/ +RSS.pm line 9. Compilation failed in require at ./scripts/podcasts/podcasts.pl line 1 +8. BEGIN failed--compilation aborted at ./scripts/podcasts/podcasts.pl li +ne 18.

    Line 18, referenced there at the end is:

    use XML::RSS;

    I've searched online and not found anything that looks like this, and I don't know where to go from here. Does anyone have any ideas?

    I've tried using XML::FEED::PARSER::LITE, but it doesn't seem to do what I need, but in principle I have no objections to using another module if you can recommend one

searching and replacing strings in a multi line variable
2 direct replies — Read more / Contribute
by victorz22
on Apr 20, 2017 at 18:29

    Hello Monks, I am want to create a function to replace strings in a multi-line variable. The part I am stuck on is setting up the parse to go line by line. Thank you monks!

    #Function Call my $newText = replaceText($inputText, $regex, $replacement); #Function sub replaceText{ my($inputText, my $regex, my $replacement)= @_; my $replacedText; if($inputText =~ s/$regex/$replacement/g){ $replacedText = $1; } return $replacedText; }
Split a large text file by columns
3 direct replies — Read more / Contribute
by tc
on Apr 20, 2017 at 17:51
    I have a question regarding perl that I have been working on the past week. I have text file with 200 columns and the same number of rows per column. The first column is the names of each row. I would like to split the file every 3 columns and export each to a new file. The caveat is, the first column(name) need to be merged to each file. Is it possible to do this in perl?

    I can split the file using awk in bash but i'm unable to merge the first column to each new file. I know perl has a split function but that splits rows not columns. I have tried a few perl scripts but they are dependent on specific column numbers, so as ideas has dried up, I turn to you, Monks.

Expect OpenSSH sudo login is failing
3 direct replies — Read more / Contribute
by archeman2
on Apr 20, 2017 at 15:37

    I am kind of new to both OpenSSH and Expect, but it appears that most advise to use these libraries for remote sudo command execution.

    My script takes 5 arguments:

    1. remoteHostname
    2. nonRootUsername
    3. nonRootPassword
    4. pathToLocalOuput
    5. RootPassword

    The script is supposed to connect as nonRootUser and then use 'sudo' to execute a sequence of commands. The connection and login appears to work as expected, however the sudo login fails and the only thing that appears in the output file is the RootPassword. The root password contains spaces and those spaces do appear to be getting escaped correctly according to the output file

    Here is the perl script:

    #!/usr/bin/perl -w use strict; use Net::OpenSSH; use Expect; $Expect::Exp_Internal = 1; my $hostname = shift; my $username = shift; my $password = shift; my $path = shift; my $rootpassword = shift; # Lets Connect First my $ssh = Net::OpenSSH->new(host => $hostname, user => $username, pass +word => $password); $ssh->error and die "Unable to connect to remote host: $hostname as $u +sername " . $ssh->error; print "Username: $username Password: $password\n"; # Figure Out What Command To Run my ( $pty, $pid ) = $ssh->open2pty({stderr_to_stdout => 1}, '/usr/bin/ +sudo', -p => $rootpassword, 'su', '-') or return "failed to attempt su: $!\n"; my $expect = Expect->init($pty); $expect->log_file("$path/expect_pm.log", "w"); my @cmdlist =( "ls -l", "pwd", "ls", "who am i", "id", "whoami" ); foreach my $expect_cmd (@cmdlist){ $expect->expect(2, [ qr/$rootpassword/ => sub { shift->send("$password\n");} ], [ qr/Sorry/ => sub { die "Login failed" } ], [ qr/#/ => sub { shift->send("$expect_cmd \n");}] ) or die "___Timeout!"; } $expect->expect(2);

    The command line output looks like this:

    archeman@host_target8:~/workdir/auto/scripts$ perl ./TestConnect.pl 1 +0.10.1.1 nonRoot p44s\!4t3Th15 /tmp/output root\ pass\ word\ here WARNING Access and use of this device is restricted to authorized personnel of + xxx Corporation for the purpose of fulfilling obligations to a specific customer. All other access is illegal and therefore str +ictly prohibited. Access and activity of this device may be monitored, and a + record of access and activity may be retained by in accordance with its recor +ds retention policy. Username: nonRoot Password: p44s!4t3Th15 Starting EXPECT pattern matching... at /usr/local/share/perl/5.14.2/Expect.pm line 597. Expect::expect(Expect=GLOB(0x87789c4), 2, ARRAY(0x8778d0c), AR +RAY(0x8778c94), ARRAY(0x8778e74)) called at ./TestConnect.pl line 33 handle id(5): list of patterns: #1: -re `(?^:root pass word here)' #2: -re `(?^:Sorry)' #3: -re `(?^:#)' handle id(5): Does `' match: pattern #1: -re `(?^:root pass word here)'? No. pattern #2: -re `(?^:Sorry)'? No. pattern #3: -re `(?^:#)'? No. Waiting for new data (2 seconds)... handle id(5): Does `root pass word here' match: pattern #1: -re `(?^:root pass word here)'? YES!! Before match string: `' Match string: `root pass word here' After match string: `' Matchlist: () Calling hook CODE(0x8778b40)... Sending '3sc4!4t3Th15\n' to handle id(5) at /usr/local/share/perl/5.14.2/Expect.pm line 1421. Expect::print(Expect=GLOB(0x87789c4), "p44s!4t3Th15\x{a}") cal +led at ./TestConnect.pl line 31 main::__ANON__(Expect=GLOB(0x87789c4)) called at /usr/local/sh +are/perl/5.14.2/Expect.pm line 825 Expect::_multi_expect(2, undef, ARRAY(0x877ba74)) called at /u +sr/local/share/perl/5.14.2/Expect.pm line 602 Expect::expect(Expect=GLOB(0x87789c4), 2, ARRAY(0x8778d0c), AR +RAY(0x8778c94), ARRAY(0x8778e74)) called at ./TestConnect.pl line 33 Returning from expect successfully. Starting EXPECT pattern matching... at /usr/local/share/perl/5.14.2/Expect.pm line 597. Expect::expect(Expect=GLOB(0x87789c4), 2, ARRAY(0x8778d48), AR +RAY(0x8778d98), ARRAY(0x8778eb0)) called at ./TestConnect.pl line 33 handle id(5): list of patterns: #1: -re `(?^:root pass word here)' #2: -re `(?^:Sorry)' #3: -re `(?^:#)' handle id(5): Does `' match: pattern #1: -re `(?^:root pass word here)'? No. pattern #2: -re `(?^:Sorry)'? No. pattern #3: -re `(?^:#)'? No. Waiting for new data (2 seconds)... handle id(5): Does `\r\n' match: pattern #1: -re `(?^:root pass word here)'? No. pattern #2: -re `(?^:Sorry)'? No. pattern #3: -re `(?^:#)'? No. Waiting for new data (2 seconds)... handle id(5): Does `\r\nSorry, try again.\r\nroot pass word here' match: pattern #1: -re `(?^:root pass word here)'? YES!! Before match string: `\r\nSorry, try again.\r\n' Match string: `root pass word here' After match string: `' Matchlist: () Calling hook CODE(0x8778b68)... Sending '3sc4!4t3Th15\n' to handle id(5) at /usr/local/share/perl/5.14.2/Expect.pm line 1421. Expect::print(Expect=GLOB(0x87789c4), "p44s!4t3Th15\x{a}") cal +led at ./TestConnect.pl line 31 main::__ANON__(Expect=GLOB(0x87789c4)) called at /usr/local/sh +are/perl/5.14.2/Expect.pm line 825 Expect::_multi_expect(2, undef, ARRAY(0x877bccc)) called at /u +sr/local/share/perl/5.14.2/Expect.pm line 602 Expect::expect(Expect=GLOB(0x87789c4), 2, ARRAY(0x8778d48), AR +RAY(0x8778d98), ARRAY(0x8778eb0)) called at ./TestConnect.pl line 33 Returning from expect successfully. Starting EXPECT pattern matching... at /usr/local/share/perl/5.14.2/Expect.pm line 597. Expect::expect(Expect=GLOB(0x87789c4), 2) called at ./TestConn +ect.pl line 36 handle id(5): list of patterns: handle id(5): Does `' match: Waiting for new data (2 seconds)... handle id(5): Does `\r\n' match: Waiting for new data (2 seconds)... handle id(5): Does `\r\nSorry, try again.\r\nroot pass word here' match: Waiting for new data (2 seconds)... TIMEOUT Returning from expect with TIMEOUT or EOF rlong@host_target8:~/workdir/auto/scripts$

    When I examine the output in the file /tmp/output/expect_pm.log file, it contains only the rootPassword:

    rlong@host_target8:~/workdir/auto/scripts$ cat /tmp/output/expect_pm.l +og root pass word here Sorry, try again. root pass word here Sorry, try again.

    So the password response to sudo appears to be formed correctly but I'm not sure why it is failing

add css to html code
3 direct replies — Read more / Contribute
by Anonymous Monk
on Apr 20, 2017 at 15:03
    I've spent the day trying to incorporate CSS into a simple HTML file. Would someone please add some CSS code to this sample?
    # # PROGRAM: popup_menu.cgi # # PURPOSE: Demonstrate (1) how to create a popup_menu form and # (2) how to determine the value selected by the user. # # Created by alvin alexander, devdaily.com. # #-----------------------------------# # 1. Create a new Perl CGI object # #-----------------------------------# use CGI; $query = new CGI; #----------------------------------# # 2. Print the doctype statement # #----------------------------------# <style> body { background-image: url("img_tree.png"); background-repeat: no-repeat; background-position: right top; margin-right: 200px; } </style> </body> print $query->header; print $query->h3("x"); #----------------------------------------------------# # 3. Start the HTML doc, and give the page a title # print $query->start_html('My popup_menu.cgi program'); my @arr = qw(pasta hamburger salad pierogi chicken_cutlet); if (!$query->param) { print $query->start_form; print $query->h4('Select a dinner entree:'); print $query->popup_menu(-name=>'entrees', -values=>[@arr], -default=>'Veggies'); } print $query->end_form; my @arr = qw(cheese_cake cannoli apple_pie); if (!$query->param) { print $query->start_form; print $query->h3('Select a desert entree:'); print $query->popup_menu(-name=>'deserts', -values=>[@arr], -default=>'Veggies'); } print $query->end_form; print $query->end_html;
How to convert RGB decimal to RGB hex and vise versa in Excel sheets?
2 direct replies — Read more / Contribute
by thanos1983
on Apr 20, 2017 at 14:00

    Hello Monks,

    My question is related to Getting the background color of a cell in an existing Excel spreadsheet. I was working on resolving the question and although that it looks that the question is resolved I got stuck.

    My problem is that when I define the color as decimal int coming from parsed Excel sheet from module Spreadsheet::ParseExcel works just fine, sample of code:

    If I use the methods:

    I can convert the int to RGB format (e.g. ffff00), but when I try to convert it back to int using this method set_custom_color($index, $red, $green, $blue):

    But the output is not as source Excel sheet in all of my attempts, unless if I use directly parsed int.

    Sample of code that replicates my problem, and printout of expect output.

    The reason that I am trying to convert RGB int to hex RGB and back to RGB in is because I was told that different Excel sheets can have different output, this is why we need to apply hex RGB.

    Anyone has encounter this problem before?

    Thanks in advance for your time and effort.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Google::Search overload arg invalid
2 direct replies — Read more / Contribute
by cormanaz
on Apr 20, 2017 at 13:40
    Good day bros. I am trying to use mod Google::Search. When I run it, using
    #!/usr/bin/perl -w use strict; use Google::Search; my $search = Google::Search->Web( key => '***', query => "rock" ); while ( my $result = $search->next ) { print $result->rank, " ", $result->uri, "\n"; }
    it yields error overload arg '' is invalid at c:/Perl64/site/lib/Google/Search/Error.pm line 9. but seems to run anyway, returning a data structure that is empty. The docs say if you provide an API key you need to provide a valid referrer but the Custom Search page doesn't seem to say anything about this, so I'm not sure what to give it.

    I'm thinking maybe this mod was developed for an older version of the API. Is there a way to get it to work, or a better alternative?

    "The whole age of, you know, computer has made it where nobody knows exactly what's going on." --D. Trump


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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (5)
    As of 2017-04-24 05:49 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      I'm a fool:











      Results (433 votes). Check out past polls.