Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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
CGI Server Insert XML Data to Mysql
1 direct reply — Read more / Contribute
by bendon
on Oct 22, 2014 at 16:56

    Hello Monks,

    I have been trying to make this code work for days but failed.

    This is the CGI code reciving the XML from a client.

    I would like to insert the messageId and text to MySql DB.

    #!/usr/bin/perl -w use strict; use Time::Piece; use warnings FATAL => 'all'; use CGI qw(); use POSIX qw/strftime/; use LWP::UserAgent; use HTTP::Request; use XML::Simple; use SOAP::WSDL; use SOAP::WSDL::Deserializer::Hash; use SOAP::Lite; use Time::Piece; use XML::LibXML; use XML::Twig; use Data::Dumper; use DBI; my $cgi = CGI->new; print $cgi->header(-type => "text/xml", -charset => "utf-8"); my $xml = $cgi->param("POSTDATA"); my $dbh = DBI->connect("DBI:mysql:smscenter", "smscenter", "smsc1234", +{ RaiseError => 1, AutoCommit => 1 }); $dbh->trace(2); my $sql1 = 'INSERT INTO test(test,test1) VALUES(?,?)'; my $sth1 = $dbh->prepare($sql1); my $twig = new XML::Twig( twig_handlers => { ServiceException => \&ServiceException, } ); $twig->parse($xml); sub ServiceException { my ( $twig, $ServiceException ) = @_; my @f = ( $ServiceException->field('messageId'), $ServiceException->field('text'), ); print $f[0], "\n"; print $f[1], "\n"; $sth1->execute( @f ) or die $DBI::errstr; }

    The XML being sent is this

    <?xml version=\"1.0\" encoding=\"utf-8\"?> <soapenv:Envelope xmlns:soapenv=\" +lope/\" xmlns:xsi=\"\"> <soapenv:Body> <soapenv:Fault> <faultcode>SVC0008</faultcode> <faultstring>Duplicate SMS notification, criteria is default criteria, + centralCode is 20245</faultstring> <detail> <ns1:ServiceException xmlns:ns1=\" +common/v2_1\"> <messageId>SVC0008</messageId> <text>Duplicate SMS notification, criteria is default criteria, centra +lCode is 20245</text> <variables>default criteria</variables> <variables>20245</variables> </ns1:ServiceException> </detail> </soapenv:Fault> </soapenv:Body> </soapenv:Envelope>

    Where am I going wrong.

    Really need your assistance.

    Thank you.

Tk Button state tied to variable
3 direct replies — Read more / Contribute
by glenn
on Oct 22, 2014 at 16:45

    How to get TK widget option to accept variable reference? I need to use this in more than one place and it's not an option to store the button in the hashref as this hashref will be updated by other clients.

wxperl Installer Script Logging
2 direct replies — Read more / Contribute
by jmlynesjr
on Oct 22, 2014 at 15:04

    While this is somewhat off topic, I am sure that among the Perl Monks are also some Shell Monks...

    In updating/re-testing my wxPerl installer script for Ubuntu 14.04LTS based on some good feedback to my eariler post, I discovered that my suggested invocation statement   sh -v > ~Perl/wxPerl-Installer.log   was hanging up when something inside the process issued a prompt that wasn't visible on the screen(being hidden in the log file). Other than losing the prompt the mix of screen display vs. log file output is good.

    I'm currently using   sh -v 2>&1 | tee wxPerl-Installer.log  , but this command sends the mass amount of compiler info(+/- 2MB) to the screen as well as to the log file.

    So, can I have it both ways? The display provided by command one without losing the prompts while sending the mass of compiler output only to the log file.

    Thanks for your assistance.


    There's never enough time to do it right, but always enough time to do it over...

blocking call maybe??
No replies — Read more | Post response
by amboxer21
on Oct 22, 2014 at 13:39
    Delete please
Semaphores leaks with IPC::Shareable
2 direct replies — Read more / Contribute
by losgrandes
on Oct 22, 2014 at 09:32
    my $h; my $handle = tie $h, 'IPC::Shareable', { destroy => 'Yes' }; # start remote command for each host my $pm = new Parallel::ForkManager(scalar @hosts_to_run); $pm->run_on_finish( sub { my ($pid,$exit_value,$ident) = @_; print "finished run on $ident\n" if $verbose; } ); foreach my $host (@hosts_to_run) { my $pid = $pm->start($host) and next; my $result = #some calculations $handle->shlock(); $h->{$host}{output} = $result; $handle->shunlock(); $pm->finish(0); } $pm->wait_all_children; IPC::Shareable->clean_up_all;
    Above code works perfectly in terms of populating $h with data, but leaves semaphores in system after execution. Any ideas where the problem is?
Array of buttons
3 direct replies — Read more / Contribute
by GayathriBalu
on Oct 22, 2014 at 07:41

    hi all, i want to create an array of buttons and handle them when clicked. in my code the last button in the array is handled even if any other button is clicked. pls help. thanks

    my @buttons; for my $i(0..5){ push @buttons, $mw->Button(-text => $i, -command => sub{ print "pressed $i\n"; # or use the widget: print "pressed ", $buttons[$i]->cget('-text'), "\n"; })->pack; }
direct data to a socket
2 direct replies — Read more / Contribute
by demoralizer
on Oct 22, 2014 at 04:14
    Hi there,

    looking at CPAN module IO:All I found some syntax I don't understand, maybe sb. here can help me with that:
    perl -MIO::All -e 'io(":8080")->fork->accept->(sub { $_[0] < io(-x $1 ? "./$1 |" : $1) if /^GET \/(.*) / })'

    What's driving me crazy is the "<" after $_[0].

    If I dump $_[0] I get: bless( \*Symbol::GEN3, 'IO::All::Socket' );

    I've been searching through the Internet and perldoc but couldn't find any description what this "<" is doing exactly :(

    Thanks a lot for any help (even any keywords to search for will be highly welcomed)
Log Write position
5 direct replies — Read more / Contribute
by pgduke65
on Oct 22, 2014 at 01:13

    Hi Monks,

    I am wanting to write a program that creates a log entry for a process start and end. What I would like is to have 1 entry per process per execution.

    As an example:

    Logged Job Start End

    10/22/14 13:00:00 J1 10/22/14 13:00:00 10/22/14 13:05:00

    10/23/14 13:00:00 J1 10/23/14 13:00:00 10/23/14 13:05:00

    The question, I have is, If I write the start time at process start, how can I write the end time to the entry? Another process can write to the for its start. As a result, I can't just assume last entry in the file

    Any advice is appreciated.

DBI mysql substring_index syntax error
2 direct replies — Read more / Contribute
by Rodster001
on Oct 22, 2014 at 00:08
    This statement works fine from the mysql prompt:
      SELECT DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(path, "\\", -2), "\\", 1) AS dir FROM files
    But I get a "DBD::mysql::st execute failed" error when I run this:
    my $query = qq~ SELECT DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(path, "\\", -2), "\\ +", 1) AS dir FROM files ~; my $sth = $dbh->prepare($query); my $rv = $sth->execute;
    DBD::mysql::st execute failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\", 1) AS dir

    So, I believe that something is not escaped properly in my statement and I have tried several different ways but no luck.

    Any ideas?
Regular Expressions in a cookie
3 direct replies — Read more / Contribute
by rshoe
on Oct 21, 2014 at 19:05
    I do not know if this is possible but I am trying to create a form that sends information to a cgi file. That file creates a cookie. Then I have another cgi file that displays the information of the cookie. Most of the files work except the last one. I'm trying to use an if statement to tell if a certain radio button is used then print the information one way if not print the information another way. I'm using a regular expression in the if statement. The last file "membership.cgi" does not work correctly. If any one can help me I would appreciate it. Thank you for your time. Randy Here are my files: html file:
    <!doctype html> <html lang = "en"> <head> <title>Sign Up</title> </head> <body> <h2>Sign up and choose your options</h2> <form action="/cgi-bin/signup.cgi" method="post"> <table border="2" cellspacing="5" cellpadding="5"> <tr> <td align="center">Name</td> <td><input type="text" name="name" size="30"></td> </tr> <tr> <td align="center">Select MemberShip Type</td> <td><input type = "radio" name = "membership" value="0">Life <input type = "radio" name = "membership" value="1">Annual <input type = "radio" name = "membership" value="2">Fee Trial</td> </tr> <tr> <td align="center">Choose Background Color</td> <td><select name = "color"> <option value = "pk">Pink <option value ="cy">Cyan <option value = "ma">Magenta <option value ="wh">White <option value = "go">Gold <option value ="bl">Blue </select> </tr> <tr> <td colspan="2" align="center"><input type = "submit" value="Sign Up + and Set Options" </td> </tr> </form> </table> </body> </html>
    The cookie cgi file
    #!c:\Dwimperl\perl\bin\perl.exe use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); use strict; use warnings; my $name = param('name'); my $membership = param('membership'); my $color = param('color'); my @members = ('Life', 'Annual', 'Free Trial'); my %colors =(pk => 'Pink', cy => 'Cyan', ma => 'Magenta', wh => 'White', go => 'Gold', bl => 'Blue',); my @newmembers =($members[$membership], $name, $colors{$color}); my $mem = cookie(-name => "member", -value => "@newmembers", -path => "/", -expires => "+1M" ); print header( -cookie => [$mem] ), start_html, "<h2>Thank you. Your data has been recorded</h2>", "<a href='membership.cgi'>See member page</a>", end_html;
    The output cgi file
    #!c:\Dwimperl\perl\bin\perl.exe use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); use strict; use warnings; my @ara = split (/ /,cookie("member")); #### splits on spaces into ar +ray if($ara[0] =~ /Free Trial/){ print header, start_html; print "<body bgcolor ='".$ara[4]."'/>"; print "<h1>Welcome back, ".$ara[0]." ".$ara[1]." Member ".$ara[2]." ". +$ara[3]."</h1>"; print end_html; }else{ print header, start_html; print "<body bgcolor ='".$ara[3]."'/>"; print "<h1>Welcome back, ".$ara[0]." Member ".$ara[1]." ".$ara[2]."</h +1>"; print end_html; }

Add your question
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?

    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 musing on the Monastery: (6)
    As of 2014-10-22 23:05 GMT
    Find Nodes?
      Voting Booth?

      For retirement, I am banking on:

      Results (122 votes), past polls