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
Error in mime::LIte
2 direct replies — Read more / Contribute
by Anonymous Monk
on Jun 24, 2016 at 12:40
    I am getting below error, while trying mime::lite in my local.

    'sendmail' is not recognized as an internal or external command, operable program or batch file.

    The below is simple code :

    #!C:\perl\bin\perl.exe -w
    use FindBin;
    use lib "$FindBin::Bin/..";
    use lib "c:\\perl\\site\\lib";

    use strict;
    use warnings;

    use MIME::Lite;
    my @body=('test');

    my $to = 'sou@rediffmail.com';
    my $from = 'sou@rediffmail.com';
    my $subject = 'Test Email';
    my $message = 'This is test email sent by Perl Script';

    my $msg = MIME::Lite->new(
    From => $from,
    To => $to,
    Subject => $subject,
    Data => $message
    any one help me in this?
Using map to populate array in for loop
1 direct reply — Read more / Contribute
by stroke
on Jun 24, 2016 at 10:47

    Trying to get a list of IP addresses from some defined IP ranges. Having found "a line of code" to enumerate each IP in the range and populate an array, I've found this doesn't work as I'd hope - only the IPs from the last range are in the array.

    Problem is that I don't really understand exactly what the inet_aton line is doing well enough to work out how to fix it - the peril of copying code, I guess!

    #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; use 5.010; use Socket 'inet_aton'; my @ips; my @ranges = qw( ); for my $range ( @ranges ) { my ( $start, $end ) = split /-/, $range; say "Found RANGE: $start - $end"; # Get each IP in the range @ips = map { sprintf "%vi", pack "N", $_ } unpack("N",inet_aton($s +tart)) .. unpack("N",inet_aton($end)); #print Dumper(@ips); # prints all IPs } print Dumper(@ips); # prints only last range of IPs # Do stuff with each IP ...

    Output from Dumper inside the for loop looks good:

    Found RANGE: - $VAR1 = ''; $VAR2 = ''; $VAR3 = ''; $VAR4 = ''; $VAR5 = ''; $VAR6 = ''; $VAR7 = ''; $VAR8 = ''; $VAR9 = ''; $VAR10 = ''; $VAR11 = ''; Found RANGE: - $VAR1 = ''; $VAR2 = ''; $VAR3 = ''; $VAR4 = ''; $VAR5 = ''; $VAR6 = ''; $VAR7 = ''; $VAR8 = ''; $VAR9 = ''; $VAR10 = ''; $VAR11 = '';

    Outside, not so - the array only has the last range IPs

    Found RANGE: - Found RANGE: - $VAR1 = ''; $VAR2 = ''; $VAR3 = ''; $VAR4 = ''; $VAR5 = ''; $VAR6 = ''; $VAR7 = ''; $VAR8 = ''; $VAR9 = ''; $VAR10 = ''; $VAR11 = '';


How to change colour of a Tkx button when pressed
1 direct reply — Read more / Contribute
by PerlCowboy
on Jun 24, 2016 at 10:32

    Hey there PerlMonks! Almost got it, but one thing I'm having issues with is I am trying to change the color of a button once it is pressed. I currently have a series of 18 buttons using the Tkx module and am wondering how I could change the color of a button once it is pressed. I know it's on the command line, but can you have two commands on a button? My code is listed below.

    use strict; use warnings; use Tkx; open(INFILE,"d1528235.txt") or die "could not open file for reading!\n +"; open(OUTFILE, '>forecast.txt') or die "Cannot open forecast.txt: $!"; while(<INFILE>){ if (m/^\s*$/) { next; } chomp $_; my @fields = split(/\ /,$_); my @output; foreach my $field(@fields){ if($field =~ /^\*?[ABMQRWY][A-Z0-9]{4}235 / ){ push @output,$field; } # print OUTFILE "$_\n"; } if (@output) { my $line = join('',@output); print "$line\n"; print OUTFILE "$line\n"; } } my $mw = Tkx::widget->new("."); $mw->g_wm_title("Jobs Not Forecasted"); $mw->g_wm_minsize(200, 200); Tkx::button(".b", -text => "18-Letters", -width => 11, -command => sub { print OUTFILE "18L\n"; -background =>'red'}, ); Tkx::pack(".b"); Tkx::button(".c", -text => "07-Orders", -width => 11, -command => sub { print OUTFILE "07O\n";}, ); Tkx::pack(".c"); Tkx::button(".d", -text => "36-Letters", -width => 11, -command => sub { print OUTFILE "36L\n";}, ); Tkx::pack(".d"); Tkx::button(".e", -text => "38-Letters", -width => 11, -command => sub { print OUTFILE "38L\n";}, ); Tkx::pack(".e"); Tkx::button(".f", -text => "11-Orders", -width => 11, -command => sub { print OUTFILE "11O\n";}, ); Tkx::pack(".f"); Tkx::button(".g", -text => "99-Letters", -width => 11, -command => sub { print OUTFILE "99L\n";}, ); Tkx::pack(".g"); Tkx::button(".h", -text => "21-Letters", -width => 11, -command => sub { print OUTFILE "21L\n";}, ); Tkx::pack(".h"); Tkx::button(".i", -text => "23-Letters", -width => 11, -command => sub { print OUTFILE "23L\n";}, ); Tkx::pack(".i"); Tkx::button(".j", -text => "Return to L&I", -width => 11, -command => sub { print OUTFILE "OOO\n";}, ); Tkx::pack(".j"); + Tkx::button(".k", -text => "EOSSS235", -width => 11, -command => sub { print OUTFILE "EOSSS235\n";}, ); Tkx::pack(".k"); Tkx::button(".l", -text => "M4020235", -width => 11, -command => sub { print OUTFILE "M4020235 20\n";}, ); Tkx::pack(".l"); Tkx::button(".m", -text => "PRTAR235", -width => 11, -command => sub { print OUTFILE "PRTAR235\n";}, ); Tkx::pack(".m"); Tkx::button(".n", -text => "WR115235", -width => 11, -command => sub { print OUTFILE "WR115235\nWR415235\n";}, ); Tkx::pack(".n"); Tkx::button(".o", -text => "X01RA235", -width => 11, -command => sub { print OUTFILE "X01RA235\n";}, ); Tkx::pack(".o"); Tkx::button(".p", -text => "X1514235", -width => 11, -command => sub { print OUTFILE "X1514235 14\n";}, ); Tkx::pack(".p"); Tkx::button(".q", -text => "X4020235", -width => 11, -command => sub { print OUTFILE "X4020235\n";}, ); Tkx::pack(".q"); + Tkx::button(".r", -text => "SICLIC", -width => 11, -command => sub { print OUTFILE "SICLIC\n";}, ); Tkx::pack(".r"); Tkx::button(".s", -text => "EXIT", -width => 11, -command => sub { Tkx::destroy("."); }, ); Tkx::pack(".s"); Tkx::MainLoop(); exec `crossref.pl`;
Regex : Return match elements
3 direct replies — Read more / Contribute
by ravi45722
on Jun 24, 2016 at 07:47

    I am trying this regex. But not reached. Help me in solving this

    my $str = <<EO_STR; must-column08:Submit & must-column10:Delivered must-column08:Submit & must-column10:Delivered One & must-column9:No_e +rror must-column08:See Here & must-column10:Delivered | must-column9:No_err +or EO_STR my @array = *some_regex*; foreach my $element (@array) { print "$element\n"; } Needed output: must-column08:Submit & must-column10:Delivered must-column08:Submit & must-column10:Delivered One & must-column9:No_error must-column08:See Here & must-column10:Delivered | must-column9:No_error

    My trails :

     my @matches = $str =~ /(\w+)-(\w+|[0-9]+):(\w+|[0-9]+)\s[&|\|](\w+)-(\w+|[0-9]+):(\w+|[0-9]+)\s[&|\|]/g;
ENMTools_1.4.4.pl on Ubuntu 15.04?
2 direct replies — Read more / Contribute
by Steve Research
on Jun 24, 2016 at 06:58
    Hi, First post; I'm a Perl virgin not even a noob!

    I am trying to get ENMTools_1.4.4.pl running on Ubuntu 15.04.

    According to the docs I need Tk+ and Perl. How do I:
    - Check the versions that i may already have?
    - update/install the latest versions?
    - launch the *.pl script?

    I tried:
    /usr/bin/perl ./ENMTools_1.4.4.pl

    I currently get:
    Can't locate Tkx.pm in @INC (you may need to install the Tkx module) ( +@INC contains: /etc/perl /usr/local/lib/i386-linux-gnu/perl/5.20.2 /u +sr/local/share/perl/5.20.2 /usr/lib/i386-linux-gnu/perl5/5.20 /usr/sh +are/perl5 /usr/lib/i386-linux-gnu/perl/5.20 /usr/share/perl/5.20 /usr +/local/lib/site_perl .) at ./ENMTools.pl line 7. BEGIN failed--compilation aborted at ./ENMTools.pl line 7
    Any ideas?
working with tables in OpenOffice::OODoc
1 direct reply — Read more / Contribute
by swathibandaru
on Jun 24, 2016 at 06:58

    I have created a table using

    $doc = odfDocument(file => 'test.odt', create => 'text'); doc->appendTable(tab1,5,3); doc->cellValue(tab1,0,0,"S No"); doc->cellValue(tab1,0,1,"Name"); doc->cellValue(tab1,0,2,"Value");

    In other rows & columns values are added using cellValue.

    Requirements: 1. When appendTable is used, table with default settings is created. How to change table Border to dark black with increased thickness 2. Table with all columns of same size is created. I want column 0,2 with less width, column 1 with large width. 3. I want to change Row 0 contents to Bold, as this indicates Heading to table contents.

    Unable to find any examples to do above. How to get above requirements. I am trying to create a table in openOffice word document using perl.

HTML code escaped from web page on browser
3 direct replies — Read more / Contribute
by sylph001
on Jun 24, 2016 at 05:26

    Hi Respective Monks,

    I'm having a script to print out a piece of HTML code in its output file which will be viewed.

    Meanwhile, the output file is grabbed by some mechanism and displayed on a web tool, which also will be viewed.

    The contrary is like, if I let it print out the original HTML code in the output file, the output file will look fine, but on the web tool page the HTML code will be trancated because of the '<' and '>' in it.

    To solve this, I let the script convert the '<' to '&#60' and '>' to '&#62' in the output file, then when the web tool page displays the content the whole lot of HTML code shows up right. However in this way, when people directly look at the output file itself, the '&#60' and '&#62' may look somewhat creepy.

    So monks, is there a way I can let the HTML code show up nicely on the plain output file as well as the web tool page from the web browser?


Handshake mechanism in perl
2 direct replies — Read more / Contribute
by t-rex
on Jun 24, 2016 at 01:51

    i am working for a project where i have implemented concurrent server , now i need to take back logs 9 in a hand shake mechanism without except module) the flow of my code is :

    main.pl { #try connecting to the server socket System(run perl_client.pl) or die “couldn’t run perl client.pl” If (fails) { Transfer files on server machine via scp Run server.pl Socket established Try connecting again to the server socket from client, (this time it w +ill connect) }
    My doubt now is how do I know whether everything is running , this code is a part of one big automation so I can’t use std libraries, maybe a log file , I know how to open and write to a file but a robust handshake mechanism is what I desire , anyone has any ideas for the same.

using File::Find to find recently-installed modules
2 direct replies — Read more / Contribute
by Datz_cozee75
on Jun 24, 2016 at 01:11

    Hello Monks,

    I'm working through the exercises in the alpaca book, to wit where the castaways have specified a local::lib and now seek to write a script that gives useful time data associated with these files. My needs are not exactly what the book calls for in any one exercise but range over several, so I used File::Find for the first time instead of relying on windows explorer to tell me where and what things are. (Uggh.)

    In the last couple weeks, I've torn out and replaced an overgrown perl install with this machine, so I wanted to pay some attention to make this install a bit more robust, and consequently have strawberry perl. I had a false start on local::lib, which I hope won't matter in the scheme of things, which I only state so that no one could say "why didn't you tell us you had a screwy, abortive, previous attempt at creating a local::lib?" It may matter, may not. That's part of what I'm learning. Here's what I have now:

    use strict; use warnings; use utf8; use 5.014; use File::Find; my @directories_to_search = ('.', 'C:\Users\Fred\Desktop'); my @files; find( \&find_module, @directories_to_search ); sub find_module { if ( $_ =~ m/.pm$/ ) { my $name = $File::Find::name; push( @files, $name ); } } for my $file (@files) { my $mtime = (stat $file)[9]; # mtime via slice my $when = localtime $mtime; print "$when: $file\n"; }


    ... Thu Apr 30 10:24:40 2015: ./alpaca/template_stuff/html1.pm Sun Jun 5 14:32:45 2016: ./alpaca/template_stuff/html2.pm Sun Jun 5 14:08:18 2016: ./alpaca/template_stuff/html3.pm Thu Apr 30 10:24:40 2015: ./alpaca/template_stuff/utils1.pm Wed Jun 9 08:59:19 2010: ./perl5/lib/perl5/HTML/ElementGlob.pm Wed Jun 9 08:24:01 2010: ./perl5/lib/perl5/HTML/ElementRaw.pm Wed Jun 9 13:56:58 2010: ./perl5/lib/perl5/HTML/ElementSuper.pm Wed Jun 9 16:17:31 2010: ./perl5/lib/perl5/HTML/ElementTable.pm Thu Feb 15 07:35:15 2007: ./perl5/lib/perl5/HTML/Extract.pm Thu May 21 09:22:21 2015: ./perl5/lib/perl5/HTML/TableExtract.pm Mon Nov 1 07:04:16 2010: ./perl5/lib/perl5/Prompt/Timeout.pm Wed Jun 24 04:07:26 2009: ./perl5/lib/perl5/WWW/Mechanize/GZip.pm

    It appears that cpan modules are getting stored in a place that seems to have 'perl5' in it twice. Hope that's okay. Scripts are able to find these so far. Otherwise, I see that, unlike the professor, mtime is not what I'm looking for, as the time I'm interested in is when they got saved to this locale. Is there a perl way to make sure I own these? (The dot is 'Documents', so I would think so indeed.)

    My quest for a "born-on" timestamp continued with reading up on stat, so I worked up versions of the same material using atime and ctime. I don't think I see the difference between the two, so I wonder which one works better as a "born-on" date in this context:

    use strict; use warnings; use utf8; use 5.014; use File::Find; my @directories_to_search = ('.', 'C:\Users\Fred\Desktop'); my @files; find( \&find_module, @directories_to_search ); my @time =localtime; say "local time is @time"; sub find_module { if ( $_ =~ m/.pm$/ ) { my $name = $File::Find::name; push( @files, $name ); } } for my $file (@files) { my $atime = (stat $file)[8]; # atime via slice my $when = localtime $atime; print "$when: $file\n"; }

    atime output:

    local time is 13 44 19 23 5 116 4 174 1 Mon Jun 13 19:12:19 2016: ./alpaca/template_stuff/html1.pm Mon Jun 13 19:12:19 2016: ./alpaca/template_stuff/html2.pm Mon Jun 13 19:12:19 2016: ./alpaca/template_stuff/html3.pm Mon Jun 13 19:12:19 2016: ./alpaca/template_stuff/utils1.pm ... Wed Jun 22 22:09:13 2016: ./perl5/lib/perl5/HTML/ElementGlob.pm Wed Jun 22 22:09:13 2016: ./perl5/lib/perl5/HTML/ElementRaw.pm Wed Jun 22 22:09:13 2016: ./perl5/lib/perl5/HTML/ElementSuper.pm Wed Jun 22 22:09:13 2016: ./perl5/lib/perl5/HTML/ElementTable.pm Wed Jun 22 22:07:17 2016: ./perl5/lib/perl5/HTML/Extract.pm Wed Jun 22 22:08:24 2016: ./perl5/lib/perl5/HTML/TableExtract.pm Wed Jun 22 22:12:21 2016: ./perl5/lib/perl5/Prompt/Timeout.pm Wed Jun 22 22:06:40 2016: ./perl5/lib/perl5/WWW/Mechanize/GZip.pm

    This has the useful information I need, and seems the same on inspection as the same script with the change of

     my $ctime = (stat $file)[10];  

    My intent is to make comparisons of time values part of the search criteria. I found many of these values astonishing and unexpected compared to the reading. Again my final question for moving forward is whether atime or ctime would serve as a better born-on date. Thank you for your comment.

Problem with using file handle in hash
2 direct replies — Read more / Contribute
by sophate
on Jun 24, 2016 at 00:23

    Hi, I am having problem with using a file handle in a hash. For example:

    ############################### my %FileHandles; my $Dir = '/tmp'; my $FileName = 'test'; open $FileHandles{$Dir}{$FileName}, "$Dir/$FileName" or die "Cannot op +en $Dir/$FileName: $!"; while (<$FileHandles{$Dir}{$FileName}>) { print $_; } $FileHandles{$Dir}{$FileName}->close; ###############################

    The script above doesnt' print the contents in the test file. I only get a line like "GLOB(0xdfa4440)" instead. Any idea?

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.