Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
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
A Perl Tk::NoteBook question
1 direct reply — Read more / Contribute
by Anonymous Monk
on Jul 09, 2014 at 07:05

    Hello Perl Folks,

    I'm interested to know,

    (A) if it's possible in Perl Tk to change the font of the text string displayed on page-tab of Tk::NoteBook whenever that particular page is raised. i.e. in comparison to other page tabs the currently raised page should have it's text string displayed in bold as well as underlined.

    (B) Also is it possible to change the foreground color of this text string (to say 'blue') only when raised ?

    Here's a sample code I've written.

    I guess the problem I'm facing is, the NoteBook object is of type Tk::Widget while the page tabs are of type Tk::Frame and the text string in page tab is not a Tk::Label object so that I can change it's font and color. The text string in the page tab is created via the -label option in Tk::NoteBook widget and I'm unable to get a handle on it so that I can change it's font and color.
    Any solution or workaround will be helpful.
    PS: I'm using Tk version: 804.032 on Windows XP

    use Tk; use Tk::NoteBook; my $mw = MainWindow->new; $mw->geometry('300x200'); my $page_tab_default_font = [-family =>'Linotype Birka', -size => 15, -weight => 'normal', -underline => 0]; my $page_tab_select_font = [-family =>'Linotype Birka', -size => 15, -weight => 'bold', -underline => 1]; my $nb_widget = $mw->NoteBook(-font=>$page_tab_default_font) ->pack(-expand=>1, -fill=>'both'); my $nb_page_01= $nb_widget->add("page1", -label=>'PAGE 1', -raisecmd=>\&raised_handler); my $nb_page_02=$nb_widget->add("page2", -label=>'PAGE 2', -raisecmd=>\&raised_handler); MainLoop; sub raised_handler { my $raised_page = $nb_widget->page_widget($nb_widget->raised()); # # HOW DO I PROCEED FURTHER ????? # }

Managing an asynchronous neverending external program
1 direct reply — Read more / Contribute
by Porax
on Jul 09, 2014 at 05:36

    Hi Monks,

    I am really new to Perl and having some issues I don't really know how to handle. I work in a Windows environment.

    I made a father script that loops to gather configuration information and provide data files to another program. No real problem here, this task was a good first contact with Perl (XML reading and writing, regex...).

    My problem is that this program doesn't end on its own, it has to run for a certain duration(which I get in the conf files) so the standards functions like open(), exec(), or system() can't help here as I need to ask the program to stop (in a clean way) and those will just have my script start to wait. I could have set a timeout but the duration set in the files may change while the program is already running so I have to update it.

    Also I need to start several instances of the external program as new conf files become available and all this may get even more complicated after the first prototype development phase as the instances of the external program will probably have to be distributed across several servers (it is a quite memory consuming app), this will be a problem to be adressed later but it may influence the choice here.

    I have looked for ideas and found some modules that look like they could help me here but I feel a bit overwhelmed by all this information :p

    - Win32::Job

    - POE

    - IPC::Cmd

    Or I could make a "double fork", one to start the program, the other one to process its output files and close it when the time has come (I would need to find the right cmd window (probably thanks to its title) and emulate a click on the cross or a Ctrl-C to end it properly).

    Which one of the above approaches seems the best suited fo my needs (and being reasonably easy to put to use for the confused beginner I am) ? Do you know other modules that could do the job? Do you have any general advice/examples for this situation ?

    Thank you in advance for your wisdom !

    Porax

Table Extract Header Match
1 direct reply — Read more / Contribute
by perlmuser
on Jul 09, 2014 at 05:22
    I have a simple HTML file .. the contents off which i have included below : ###############
    <table class="gridtable" summary="RegTable"> <tr><th>Address</th><th>Register</th><th>7</th><th>6</th><th>5</th><th +>4</th><th>3</th><th>2</th><th>1</th><th>0</th><th>Reset</th><th>Desc +ription</th></tr> <tr><td>0x00000001</td><td><a href="#RegisterMap:REG0000">REG0000</a>< +/td><td align=center colspan=6> TEMP </td><td align=center > STOP </t +d><td align=center > START </td><td>'h14</td><td>TEMPORARY REG.</td>< +/tr> </table> <table class="gridtable" summary="RegTable"> <tr><th>Address</th><th>Register</th><th>15</th><th>14</th><th>13</th> +<th>12</th><th>11</th><th>10</th><th>9</th><th>8</th><th>7</th><th>6< +/th><th>5</th><th>4</th><th>3</th><th>2</th><th>1</th><th>0</th><th>R +eset</th><th>Description</th></tr> <tr><td>0x00000100</td><td><a href="#FuseMap:FUSE0">FUSE0</a></td><td +align=center colspan=8> F_1 </td><td align=center colspan=8> F_0 </td +><td>'h0000</td><td>FUSE0.</td></tr> </table>
    ########### I basically has two tables I wrote the following perl script to extract the table based on a header match: ###########
    use HTML::TableExtract; my $file = 'temp.html'; @headers = qw( Address Register 15 14 13 12 11 10 9 8 7 6 5); print " \n h:@headers:\n"; $te = new HTML::TableExtract( keep_html=>1,headers => \@headers); $te->parse_file($file); @tcount1 = $te->counts(0); print " tcount1 : @tcount1:\n";
    ######## Basically i could like to extract the second table, but for some reasons the extraction does not seem to work .. If however i remove the last entry in the header list i.e. if i have the header as just
    @headers = qw( Address Register 15 14 13 12 11 10 9 8 7 6);
    It works fine .. but with the header as :
    @headers = qw( Address Register 15 14 13 12 11 10 9 8 7 6 5);
    It does not work .. Not sure if i have done something wrong here .. but can someone help me out .. I could like to have the header as
    @headers = qw( Address Register 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 +);
    for some reasons and not a truncated header .. Any suggestions could be great .. Perl version 5.14.2
Join with x operator
5 direct replies — Read more / Contribute
by sneaky
on Jul 09, 2014 at 04:57
    Hello, currently in learning and I'm following this tutorial: http://www.perl-begin.org/tutorials/perl-for-newbies/part1/ At the bottom of the page there is the following example:
    print "Test 4:\n"; print join("\n", (("hello") x 5)), "\n\n"; print "Test 5:\n"; print join("\n", ("hello" x 5)), "\n\n"; >> Test 4: hello hello hello hello hello Test 5: hellohellohellohellohello
    I do not understand what is happening here. In my mind these should behave the opposite. I have also tried:
    print ("hello" x 5); print "\n"; print (("hello") x 5); >> hellohellohellohellohello hellohellohellohellohello
    So can someone please help me understand the difference in the join operations above?
Command To Empty Recycle Bin With Perl?
2 direct replies — Read more / Contribute
by sphinxy09
on Jul 09, 2014 at 00:37
    I use Perl and WinZip's command line interface to automatically move ("-m" operator adds to ZIP and deletes from original location) folders / files to ZIP files. In PERL v5.6.1 the "move" would permanently delete the folders / files but in PERL v5.16.1 the "move" only moves the files to the Recycle Bin. My Recycle Bin is now constantly full, so is there a way to automatically empty the Recycle Bin via a Perl script? Or is there a way to have WinZip's "-m" operator for WZZIP permanently delete folders / files in Perl v5.16.1? Thank you for your help!
Help with setting up spamc
2 direct replies — Read more / Contribute
by SteveTheTechie
on Jul 09, 2014 at 00:26

    I may be overthinking this, but here goes.

    I am the current developer for a free template based website system used by over 9000 Toastmasters public speaking clubs worldwide. We handle over 300,000 emails in a given week for clubs using our system.

    All of our server code is in Perl including our email handler. Our email handler supports a wide variety of forwarding email addresses and distribution lists. Up till recently, our main email security approach was to verify club membership. We still want to do that, but we have added the use of SpamAssassin as an additional step targeted at the provided email addresses that are intended for public use.

    I set up SpamAssassin using Mail::SpamAssassin in the email handler, and it basically started dragging the server performance significantly (should have expected that).

    I am trying to get the spamc/spamd combo going for us. We have spamd set up. I am just stumbling over setting up the use of spamc in the email handler code.

    Current SpamAssassin call from email handler (@ line 479 of email handler--*lots* happening before this):
    #Spam Test with SpamAssassin... unless ($SpamChecked || $whitelisted) { my $trigger = $CLUBSITES{'spamthreshold'} || 5.0; my $spamtest = new Mail::SpamAssassin({ 'post_config_text' => "requi +red_score $trigger" }); my $status = $spamtest->check_message_text( $message_received ); if ( $status->is_spam() ) { my $score = $status->get_score(); my $threshold = $status->get_required_score(); my $hits = $status->get_names_of_tests_hit(); my $SpamLogMsg = "Score: $score / $threshold (trigger);\t Positiv +e Tests: $hits"; HandleError("SPAM", $SpamLogMsg, $message_received); } $status->finish(); $spamtest->finish(); $SpamChecked++; }

    I need to send $message_received to spamc and capture its output in a variable (preferably) so I can get the spam score. I know I can just back quote a system command to capture stdout to a variable, but how can I do both the stdout and the stdin handling here? This should be simple, but I am just missing it...

MySQL results inserted into Perl variable
2 direct replies — Read more / Contribute
by shake7
on Jul 08, 2014 at 19:10

    Hi everyone, I'm very new to Perl, and I was having a bit of an issue with MySQL and Perl. What I want to do is take the output result and assign it to a Perl variable. Here's what I have so far:

    use DBI; my $dsn = 'driver={SQL Server}; Server=SERVERNAMEHERE; database=DBNAMEHERE; uid=UIDHERE; pwd=PWHERE'; my $dbh = DBI->connect("dbi:OOOO:$dsn",{AutoCommit => 1}) or die "$DB +I::errstr\n"; my $sql = "select field1, field2 from location where restriction = 'RE +STRICTION NAME'"; my $sth = $dbh->prepare( $sql ); $sth->execute(); $sth->finish(); $dbh->disconnect();

    As you can see, I only show the actual SQL statements, I've looked at countless forum posts to help solve my problem but I don't think they answer my question accurately. For a little more information, the mySQL statement works fine and returns about 100+ results. I want to assign those results to Perl variables, and then pass those variables into another subroutine which utilizes SOAP::Lite If you'd like to take a look, here's the Perl code which I attempted to write thus far, but I only get one result which contains two fields, and not the additional hundred. Would I need to use an array as well?

    use DBI; my $dsn = 'driver={SQL Server}; Server=SERVERNAMEHERE; database=DBNAMEHERE; uid=UIDHERE; pwd=PWHERE'; # my %info = ( # field1 => "field1", # field2 => "field2" # ); my $dbh = DBI->connect("dbi:OOOO:$dsn",{AutoCommit => 1}) or die "$DB +I::errstr\n"; #my $ref = $dbh->selectall_arrayref("select field1, field2 from locati +on where restriction = 'RESTRICTION NAME'"); my $sql = "select field1, field2 from location where restriction = 'RE +STRICTION NAME'"; my $sth = $dbh->prepare( $sql ); $sth->execute(); my @record = $sth->fetchrow_array; #my $href = $dbh->selectall_hashref("select field1, field2 from locati +on where restriction = 'RESTRICTION NAME'"); #print "$_\n" for (keys %$href); # foreach (@record){ # print $_; # } print join(", ", @record); $sth->finish(); $dbh->disconnect();
Change permissions after file is created
4 direct replies — Read more / Contribute
by ddrew78
on Jul 08, 2014 at 16:23
    Greetings Monks, I currently have a perl script that generates a new file every time it is run. After it has completed, I want it to change permissions on that file. What I'm currently using is
    close $outfile; system 'chmod 777 $outfile';

    However, this gives me an error every time I execute the script, and it does not change the permissions

    chmod: missing operand after `777'

    Any ideas as to what I'm doing wrong here? Any and all help is, as always, highly appreciated.

Is it possible to print off Exel and Word documents in perl.
2 direct replies — Read more / Contribute
by john.tm
on Jul 08, 2014 at 16:03
    I have a code that will print of.txt documents but when i send spreadsheets or word documents to be printed, they print of 100's of pages in binary.Is there any way to print of these sorts of files?
    use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; use autodie; use File::Copy; my $errorprn1 = "\n! cannot find printer UK BAS 1ST FLOOR WEST PRN 1 o +n srv00086\n It may be out use, try using UK BAS 1ST FLOOR WEST PRN 2\n"; copy 'C://Temp//test.xls' , '//SRV00086//UK BAS 1ST FLOOR WEST PRN 1' +or die "$errorprn1" ; #my $Word = Win32::OLE->new('Word.Application', 'Quit'); #$Word->{'Visible'} = 1; # if you want to see what's going on #$Word->Documents->Open("C:\\temp\\prn1.doc") #|| die("Unable to open document ", Win32::OLE->LastError()); #$Word->ActiveDocument->PrintOut({ #Background => 0, #Append => 0, #Range => wdPrintAllDocument, #Item => wdPrintDocumentContent, #Copies => 1, #PageType => wdPrintAllPages,
Why don't file handles have sigils?
4 direct replies — Read more / Contribute
by 1s44c
on Jul 08, 2014 at 15:46

    I see a lot of references to how things would be easier if file handles had sigils. Is there some reason why they don't? Using references works great but mixing references with barewords for STDIN/OUT/ERR seems messy.

    I'm sure this has been asked before but I could not find anything in the site search or with google.


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 studying the Monastery: (3)
    As of 2014-07-10 03:35 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (198 votes), past polls