Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
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
Forking multiple processes at once, but limiting how many at a time
2 direct replies — Read more / Contribute
by jLinux
on Dec 19, 2014 at 14:28
    Im writing a script that will execute a command on a list of servers, and was planning on using fork to do so, however I cant have it go to every server at once, so I was going to have it go to $max amount of child processes, then wait for those to finish. It basically doesnt seem to halt at $max, it just keeps going and forks off all at once. Heres my broken code..
    my $max = 5; while (my ($ip, $hostname) = each(%{$hosts->{'all'}})){ $i++; $c++; forkHost($ip); if($c == $max) { $c = 0; 1 while (wait() != -1); } } sub forkHost { my $target = shift; my $pid; next if $pid = fork; # Parent goes to next server. die "fork failed: $!" unless defined $pid; if (not $pid) { open COMMAND, "/execute/this/command |"; # Do some magic.. close COMMAND; exit; } }
    Thanks for any help!
Tests on Windows for Unix-only module
2 direct replies — Read more / Contribute
by mikosullivan
on Dec 19, 2014 at 13:26
    I am developing a module that is specifically for Unixish systems. It will have no applicability to Windows. When I release the module on CPAN, how should I go about allowing for tests on Windows machines? Should I just let them fail, or should I do some kind of OS detection? If I do OS detection, should I just output a bunch of meaningless ok's so that it "passes"?

    I hate seeing any fails in the tests section of my modules on CPAN, so I'd rather have the module "pass" on windows if that makes sense to do so.

Sending a hash reference to a subroutine, and retain its values after returning from that subroutine
1 direct reply — Read more / Contribute
by estreb
on Dec 19, 2014 at 10:56
    Hello Monks, I am trying to create a hash, pass that hash as an argument to a subroutine, and then after returning from that subroutine, keep any of the changes that might have been made by that hash. Here is my code:
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %records = (); my $ref = \%records; print "Before call to DoStuff\n"; print "Hash reference: '$ref'\n"; print Dumper(\%records); DoStuff(\%records); print "Back from call to DoStuff\n"; print Dumper(\%records); exit(0); sub DoStuff { my $hash_ref = shift; print("In DoStuff: "); print "Hash reference: '$hash_ref'\n"; my %temp_records = %{ $hash_ref }; $temp_records{"foo"} = "bar"; print Dumper(\%temp_records); return; }
    And here is the output:
    Before call to DoStuff Hash reference: 'HASH(0x1da6e78)' $VAR1 = {}; In DoStuff: Hash reference: 'HASH(0x1da6e78)' $VAR1 = { 'foo' => 'bar' }; Back from call to DoStuff $VAR1 = {};
    After the call to DoStuff(), why is the "foo" key gone? How do I fix this?
Edit XML through XML Compile
1 direct reply — Read more / Contribute
by chenino
on Dec 19, 2014 at 10:38
    Hey Folks and at first Hi there, I am pretty new to Perl and have somehow managed to get around with it (also thanks to you).

    My actual Problem at the time is, i have to generate and edit some XML. I used XML::Twig up to now, but in my current Project i am forced to use XML::LibXML and XML::Compile. XML::Compile::Cache is not there for use.

    So far i used XML::Compile::Schema, as an .xsd was given to me. My Problem now is, after generating the file for the first time and saving everything, I have to update it with some entries lateron. I can't find methods for this in the Doc and so i am asking for your whisdom.
XML::Simple XMLout
2 direct replies — Read more / Contribute
by totorototo
on Dec 19, 2014 at 10:09
    Hello, I'm trying to use XML::Simple to serialize datas into xml file. (XMLOut) my data structure:
    my $TestRun= { 'SuccessfulTests' => [ { 'Test' => { 'name' => "toto" } }, { 'Test' => { 'name' => "titi" } } ], 'FailedTests' => [ { 'Test' => { 'name' => "tutu" } }, { 'Test' => { 'name' => "tete" } } ] };
    And the xml structure I would like to have:
    <TestRun> <SuccessfulTests> <Test> <Name>toto</Name> </Test> <Test> <Name>titi</Name> </Test> </SuccessfulTests> <FailedTests> <Test> <Name>tutu</Name> </Test> <Test> <Name>tete</Name> </Test> </FailedTests> </TestRun>
    my $xml = new XML::Simple; my $dec = '<?xml version="1.0" encoding="utf-8"?>'; print $xml->XMLout($VAR2, XMLDecl => $dec, keyattr => [], RootName => +'TestRun');
    result:
    <?xml version="1.0" encoding="utf-8"?> <TestRun> <SuccessfulTests> <Test name="toto"/> </SuccessfulTests> <SuccessfulTests> <Test name="titi"/> </SuccessfulTests> <FailedTests> <Test name="tutu"/> </FailedTests> <FailedTests> <Test name="tete"/> </FailedTests> </TestRun>
    Unfortunatley, I did not succeeded. I do not want SuccessfulTests and FailedTests to be repeated. Any help would be appreciated. Thx, Totorototo.
Creating Hyperlink with a Excel Sheet
1 direct reply — Read more / Contribute
by DVCHAL
on Dec 19, 2014 at 05:07
    Hi monks, I wanted to know how to create/Set hyperlinks within the Excel Sheet using Win32::OLE. To navigate from one to sheet to other sheet in the same xlsx. I understand the Syntax would be,
    $worksheet->Hyperlinks->Add({Anchor => $range, Address => +$adr, TextToDisplay => $txt, ScreenTip => $tip1,

    But what would be Value for Address to set the link to the same Excel?

How to sort filename based on time via FTP
2 direct replies — Read more / Contribute
by bhhensem
on Dec 19, 2014 at 04:53

    HI...

    Actually I want to get latest file in remote machine via FTP.

    I have tried to use mdtm to sort the filename based on timestamp and get the latest filename.

    Unfortunately, the script display blank for the mdtm and i cannot get the latest filename. I do not know why this is happened.

    Is there any solution i can used to sort the filename ?. The filename start from 1 to 9999 recursively.

    My code as below

    my @list = $ftp->ls(); if ($cur_seq == 9999) { $ck_current = 1; } else { $ck_current = $cur_seq+1; } foreach my $name (@list) { #($pattern = $pattern) =~ s/\.*//g; if ($name =~ /^$file_pattern$/) { ($ckseq = substr($name,6,6)) =~ s/^0+//g; $list_filename{$ckseq} = $name; my $mdtm = $ftp->mdtm($name); if ($mdtm > $lasttm) { $latestfile = $name; $lasttm = $mdtm; } } } (my $lastseq = substr($latestfile,6,6)) =~ s/^0+//g;

    Thank you

displaying all data received on a web script
2 direct replies — Read more / Contribute
by RenardBleu
on Dec 19, 2014 at 04:26
    Hi
    It is probably a very basic question but it seems i cant figure out a simple way to do it.
    I d like to have a tiny CGI script for debug issues that would output the variable he receives. I used
    #!/usr/bin/perl -w use strict; use CGI::Lite; my $cgi = new CGI::Lite; my %in = $cgi->parse_form_data; my $text_value = ''; print "Content-type: text/html\n\n"; foreach my $k (keys %in) { $text_value .= "$k => $in{$k}\n\n"; } my $fichier = '../public_html/debug.html'; open (FICHIER, '>', $fichier) || die ("Writing $fichier failed"); print FICHIER $text_value; close (FICHIER);

    But this script would need to be updated when more complex type of data are required
    Is there a simple way to do what i want ?
    thanks in advance
html not displaying
1 direct reply — Read more / Contribute
by bigup401
on Dec 19, 2014 at 03:00

    it does not show html to browser. bt runs well in commandline

    #!"C:\xampp\perl\bin\perl.exe" use Socket; use CGI; use CGI::Carp 'fatalsToBrowser'; $cgi = CGI->new(); print "Content-Type: text/html\n\n"; my $hostname = $cgi->param('host'); my $port = 80; my $proto = getprotobyname('tcp'); my($socket); socket($socket, AF_INET, SOCK_STREAM, $proto) or die $!; my $inet = inet_aton($hostname) or die $!; my $dest = sockaddr_in($port, $inet); if ($dest) { connect($socket, $dest) or die $!; print "CONNECTED TO $hostname ON PORT $port\n"; } close($socket); exit(0); print <<START_HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Untitled Document</title> </head> <form method="post" action=""> <body> <p>IP/HOSTNAME <input type="text" name="host"/> </p> <p> <input type="submit" name="button" value="Submit" /> </p> </body> </form> </body> </html> START_HTML
replace some emoticons - search pattern needed
1 direct reply — Read more / Contribute
by Anonymous Monk
on Dec 19, 2014 at 01:48

    Hello all!

    I need to replace all emoticons in a large LaTeX-file with some icons. I put up a little file with:

    😘 🚕

    and made a perl-filter to replace the first one with an icon. But I am unable to find the right unicode search-pattern.

    Could somebody be so kind and help with this?

    #!/usr/bin/perl use strict; use warnings; use utf8; use charnames ':full'; # while(<>) { # s!\x{D83D}\x{DE18}!\\includegraphics[height=15pt]{/Users/mstep/Doc +uments/private_txt/anette/pix/emo01.png}!g # } # while(<>) { # foreach (/\x{0020}/g) {print "hurray!\n"}; # this is working! (Searc +hes for a space!) # } while(<>) { foreach (/\N{FACE THROWING A KISS}/g) {print "hurray! And here is your + emoticon!\n"}; # this is not working! (Searches for the emoticon &#1 +28536; !) } # while(<>) { # foreach (/&#128536;/g) {print "hurray! And here is your emoticon!\n" +}; # this is not working! (Searches for the emoticon &#128536; !) # } # while(<>) { # foreach (/\x{D83D}\x{DE18}/g) {print "hurray! And here is your emoti +con!\n"}; # this is not working! (Searches for the emoticon &#128536; + !) # } # while(<>) { # foreach (/\N{U+D83DDE18}/g) {print "hurray! And here is your emotico +n!\n"}; # this is not working! (Searches for the emoticon &#128536; ! +) # } # while(<>) { # foreach (/\N{U+D83D}\N{U+DE18}/g) {print "hurray! And here is your e +moticon!\n"}; # this is not working! (Searches for the emoticon &#128 +536; !) # } # in BBEdit the search pattern \x{D83D}\x{DE18} for the emoticon was w +orking # hexdump is giving U+D83D U+DE18

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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 making s'mores by the fire in the courtyard of the Monastery: (7)
    As of 2014-12-20 06:47 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (95 votes), past polls