Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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
Trim Two Characters On Line
2 direct replies — Read more / Contribute
by jalopez453
on Oct 22, 2020 at 18:55
    Would someone be able to assist me in regards to updating a line when it meets a certain criteria. I am looking for something along the lines of.
    if LINE contains "SUM" then remove last two characters in line ELSE #do nothing END IF
    I have read many forums but I can't seem to find something of what I am trying to complete.
Any reason to use the Paws module over aws cli commands in perl script?
5 direct replies — Read more / Contribute
by nysus
on Oct 21, 2020 at 09:25

    What are the arguments for using the Paws module instead of just embedding commands like `aws ec2 run-isntances --image-id ....` into my perl script? I feel dirty just executing command line commands in my perl script and it doesn't feel very professional, but what's the harm, really?

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

BEGIN Win32::Daemon
1 direct reply — Read more / Contribute
by PerlAddict42
on Oct 21, 2020 at 04:41
    Using Perl for over 20 years, today I got surprised by something I don't understand. On Windows 10, using Strawberry Perl 5.22, I run the following one-line script from which I expect no output:

    use Win32::Daemon;

    But, surprise, this is the output:

    BEGIN Win32::Daemon ... require Exporter; require DynaLoader; bootstrap Win32::Daemon; done END Win32::Daemon ... DONE Win32::Daemon ...

    This output goes to STDOUT, not to STDERR, and I would like to get rid of it. What am I missing?
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
4 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
3 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();

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 chilling in the Monastery: (3)
    As of 2020-10-23 00:26 GMT
    Find Nodes?
      Voting Booth?
      My favourite web site is:

      Results (234 votes). Check out past polls.