Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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
Module inconsitency
4 direct replies — Read more / Contribute
by keen2learn
on Oct 18, 2020 at 10:58
    Hi, I'm having to convert my dashboard in a hurry because I've moved to a new server and 'use lib' to locate external scripts for my html output, no longer works. Hence my quickly, to get to know modules. The module I have created, displays inconsistently. After I edit the output and clearing browser cache, it often shows old data from an earlier attempt, and rotates on 'refresh' between new an old content. I wonder if there is a caching aspect with modules (perhaps @EXPORT) that I am unaware of? So, would anyone please, be willing to troubleshoot and advise on this first attempt at a module. Main script
    #!/usr/bin/perl -w use strict; use warnings; use CGI; use Data::Dumper; my $cgi = new CGI; # add the library dir to @INC; use lib do { use Cwd 'realpath'; my ($dir) = __FILE__ =~ m{^(.*)/}; realpath("$dir/library"); }; use feature 'say'; use FindBin '$RealBin'; use lib $RealBin; use lib "$RealBin/library"; print $cgi->header; use HtmlHeader; my %doctypeAndTitle = header(); say $doctypeAndTitle{doctypeAndTitle}; #output inconsistent 1;
    Here's the module
    package HtmlHeader; use strict; use warnings; use parent 'Exporter'; our @EXPORT_OK = qw[ header ]; sub header { my $doctypeAndTitle = getDoctypeAndTitle(); my %doctypeAndTitle = ( doctypeAndTitle => $doctypeAndTitle ); return %doctypeAndTitle; } sub getDoctypeAndTitle{ my $doctype = "<!doctype html> <html> <head> <title>My Page Title</title> "; return $doctype; } 1;
Trouble with sending more than one attachment
2 direct replies — Read more / Contribute
by ytjPerl
on Oct 18, 2020 at 10:44

    Hi, I am able to send single attachment but not multiple attachments. Can Any one help? The following code only attached one file from the directory

    my $msg = MIME::Lite->new ( From => $msgFrom, To => $msgTo, Subject => $msgSubject, Type =>'multipart/alternative', ) or die "Error creating multipart container: $!\n"; # Create the HTML part my $html_part = MIME::Lite::->new( 'Type' => 'multipart/related', ); $html_part->attach( 'Type' => 'text/html', 'Data' => qq{ <body> <p>hello</p> </body> }, ); my $att_part; my @files = glob("F:/folder/*"); $att_part = MIME::Lite::->new( 'Type' => 'application/octet-stream', 'Encoding' => 'base64', 'Path' => shift @files, ); $msg->attach($html_part); $msg->attach($att_part); my $email = $msg->as_string(); my $smtp = Net::SMTP_auth->new($smtphost, Port=>$smtpport) or die "Can +'t connect"; $smtp->mail($msgFrom) or die "Error:".$smtp->message(); $smtp->recipient($msgTo) or die "Error:".$smtp->message(); $smtp->data() or die "Error:".$smtp->message(); $smtp->datasend($email) or die "Error:".$smtp->message(); $smtp->dataend() or die "Error:".$smtp->message(); $smtp->quit or die "Error:".$smtp->message();
Tk TableMatrix programmatically select row (color)
2 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 17, 2020 at 08:29

    Dear monks

    I want to programmatically select a row in a TableMatrix. I am doing this. It activates a cell, but it is okay in my scenario:


    The cell gets selected, but the normal highlighting you get when you click with the mouse is not shown. I need it to be shown. I added the following

    $t->tagRow('active', '0');

    This highlight the row/cell, but in a different color respect when I click on it with the mouse. Is there something build-in, or do I have to define a new Tag with same color, etc.?

Win32::SysTray Issue
3 direct replies — Read more / Contribute
by PilotinControl
on Oct 16, 2020 at 23:22

    Hello Monks!
    I have another issue that I am trying to wrap my head around the code is below:

    use strict; use Win32::SysTray; my $tray = new Win32::SysTray ( 'icon' => 'C:\images\logo.ico', 'single' => 1, ) or exit 0; $tray->setMenu ( "> &Test" => sub { print "Hello from the Tray\n"; }, ">-" => 0, "> E&xit" => sub { return -1 }, ); $tray->runApplication;

    The above code works as it should in standalone mode. However when I add it to a larger Win32::Console program only THIS code works:
    my $tray = new Win32::SysTray ( 'icon' => 'C:\images\logo.ico', 'single' => 1, ) or exit 0;

    The second part of the code never gets executed. If the second part of the code is added the program freezes. Any ideas? I believe it has to do with the $tray->runApplication; part conflicting with the Win32::Console. Thanks in advace!
Escape $ in JSON::XS decoded
4 direct replies — Read more / Contribute
by johnfl68
on Oct 16, 2020 at 16:27

    Hello, is there a any other way to escape the $ as a decoded JSON character?

    Google in their infinite wisdom decided to use the $ as part of value names:


    "updated":{ "$t":"2019-07-19T20:04:56.046Z"

    But DataDumper shows the current time:

    $VAR1 = { 'version' => '1.0', 'feed' => { 'xmlns' => '', 'updated' => { '$t' => '2020-10-16T19:55:33.294Z +' }, 'gs$rowCount' => { '$t' => '1000'

    I am trying to get the Feed Updated value, this gives me the current time, and not the time value of 2019-07-19 in this case:

    $updated    = $decoded_json->{feed}->{updated}->{'$t'};


    $updated    = $decoded_json->{feed}->{updated}->{'\$t'};

    Gives me: Use of uninitialized value $updated in concatenation (.) or string

    Normally I just escape the special character, but it seems to be interpreting it into something different, and I am not sure why? I don't usually have characters in JSON that need to be escaped like this.

    Everything else is working fine, except this one line. Am I missing something that I just haven't stumbled upon before? Any help would be appreciated as always.

    Thank you.

One liner is returning unexpected "1" on the output and I do not understand the mechanism at play.
1 direct reply — Read more / Contribute
by perlynewby
on Oct 16, 2020 at 12:49

    I am getting an unexpected output from a one-liner and I do not understand what it is I am doing wrong. the one-liner is outputting a "1" before the line number. I've tried to simplify the one-liner to try to understand the output but still eludes me but, maybe, someone here can part with some of the wisdom.


    name = Leah last_name = Pellegrini age = 32 profession = "artist" / "e +ngineer of all things" favorite_song = "I'm alive" performer = "Micha +el Franti" name = alberto last_name = Montelongo age = 32 profession = "cool + dude" favorite_song = "#41" performeer = "DMB" Name = Lucca last_name = montelongo age = 6 profession = "future Blue +Angel" favorite-song = "sunflower" performer = "Post Malone" Name = josh last_name = montelongo age = 26 profess +ion = "happy" favorite-song = "satellite" performer = "dmb" Name = josh last_name = montelongo age = 26 profession = "happy" favo +rite-song = "satellite" performer = "dmb"

    1) One-liner seeking to build up to do multiple things like only print odd lines, print lines with text only, OR print if regexp match. Then print line with no leading space, number of the lines, space out with a \tab.

     perl -lane 'print s/^\s+//,$., "\t", @F[0,1,2,],"\t",@F[3,4,5], if (($.%2) && /\w+/ || /cool dude/)' data.txt
    13 name=alberto last_name=Monte 5 Name=Lucca last_name=monte

    Please, notice the 1 before the odd line number 3, Why is that? Is my substitution not written correctly? I am replacing the space with no space. If I remove this substitution the output is fine but it is unexpected to me and I cannot understand the mechanism that is at work here with this substitution.

    2) Now, the same one-liner but printing the even lines.

    perl -lane 'print s/^\s+//,$., "\t", @F[0,1,2,],"\t",@F[3,4,5], if (($.%2==0) && /\w+/ || /cool dude/)' data.txt
    2 name=Marta last_name=Pellegrini 13 name=alberto last_name=Monte 16 Name=josh last_name=monte 8 Name=josh last_name=monte

    This is to illustrate the susbtitution in line 3 (satisfies OR condition) and 6(even lines). I'll appreciate and explanation why the substitution is outputing these ones.

    Additional challenge: count the repeating names in the data.txt. I did a ~tr/=// as a character count but unable to think of a counter for "name" to add to this one-liner ;-)

error dbi
2 direct replies — Read more / Contribute
by bigup401
on Oct 16, 2020 at 09:07

    i dont know what cause this error. coz i just checked in my apache error log and found alot such errors

    error DBI::db=HASH(0x564aeb4fdea8)->disconnect invalidates 5 active statemen +t handles (either destroy statement handles or call finish on them be +fore disconnecting)
    the layout of my db my $host = "localhost"; my $usr = "root"; my $pwd = "hell"; my $dbname = "helldb"; my $dbh = DBI->connect("DBI:mysql:$dbname:$host", $usr, $pwd, { RaiseError => 1, }) or die $DBI::errstr; my $select = $dbh->prepare("SELECT something FROM info"); $select->execute(); my $info = $select->fetchrow();
modernizing a perl util to upload a file through sftp
3 direct replies — Read more / Contribute
by Aldebaran
on Oct 16, 2020 at 02:40

    I present with a rough looking perl script that has found new and maybe interesting ways to fail. It has worked previously. It can very well be that this is a version that wasn't supposed to go forward but did. What has changed in the meantime is everything, and I'd like to see if we can cook up something better, but let me start with moderately-abridged output and source in readmore tags. Then I'll pop out some lines and ask questions.

    parameter array is /var/www/html/pmimage Net::SFTP::Foreign=HASH(0x55ecba31fb60) Couldn't create remote directory: Permission denied

    Q1) Is there a perl remedy for this? I seem to be able to upload files but not create directories.

    Q2) If the answer to Q1 is no, then, [OT] Is there a unix remedy?

    if ( fork() == 0 ) { # arm the alarm clock alarm(10); say "alarm set for ten in process $$ and pinging google"; # create a child process my $trial = system("ping"); say "trial is $trial"; exit(0); }

    The system call never relinquishes control. Q3) Is it said to be blocking?

    Q4) What do these indicate when I hit control-C?

    debug1: fd 0 clearing O_NONBLOCK debug1: fd 1 clearing O_NONBLOCK

    Thanks for your comment,

Disable Maximize Option
1 direct reply — Read more / Contribute
by PilotinControl
on Oct 15, 2020 at 17:51

    Hello Monks!
    Looking for the code to disable the Maximize option on the Win32 Console window as I am already creating a console size within the program. I can remove the X (Close) just fine I am running strawberry perl on windows 10. Thanks in advance! - Scott

Auto completing a form from and to a mysql database
3 direct replies — Read more / Contribute
by Dazz45X
on Oct 15, 2020 at 09:15
    Hello Monks

    Well it's been 3 years since I was here last as I don't really like asking if I can fathom the problems out for myself but this time I'm well and truly stumped. Probably because my perl is still p. poor (although I feel I'm a little better than last time I was here) and I've sort of gleaned from my research that I need Javascript, JQuery and Json to accomplish what I am trying to do?. All of which I am completely clueless about

    So basically my site works on a few perl scripts cobbled together and is a record selling platform very much like eBay but more specific. Users can sell their own records (45's) by completing a series of forms to enter (and post) the data. Seperately I have a mysql database which includes probably every 45 known of the particular genre about 60000 records.

    The top part of the form encourages the user to enter the Artist's name, the Record Label and the Title of the record. Usually after adding some more details the form is submitted and saved to a flat file. What I want to be able to do is autocomplete the Artist, Label,Title bit with data from the mysql database. So the user types say 3 letters of the Artists name and a dropdown gives a choice of Artist based on their criteria. The Label input will then produce a dropdown list of all of the Labels the Artist has recorded on for selection and lastly the Title dropdown will include a list of all the titles the Artist has recorded. If the record is not in the database, I would like it to be written back to the database

    So far the form obviously works to collect data for the flat file, the mysql connects and I have managed to extract Artist, Label and Title and assemble into a Json array, my question I suppose is what do I do now to autocomplete the form?

    Thank you in advance for any help, suggestions or code you may proffer Dazz

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!
  • 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?

    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 surveying the Monastery: (7)
    As of 2020-10-19 20:57 GMT
    Find Nodes?
      Voting Booth?
      My favourite web site is:

      Results (206 votes). Check out past polls.