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
Help converting to a Windows service
1 direct reply — Read more / Contribute
by bajangerry
on Mar 23, 2017 at 14:42

    Hello Monks

    EDIT: Ok so I may have been misleading everyone. I have been able to install the script as a service but for some reason I get an error when running it as a service. What runs perfectly from a command line fails as a service with the below error:

    Error 1053: The service did not respond to the start or control request in a timely fashion

    I have been trying to wrap my head around using Win32::Daemon to have a perl script and/or pp converted executable of the same script run as a service and I can't seem to figure out how this is done. I have tried using the Windows "sc create..." command with the .exe file but that does not work so I am obviously missing the Win32::Daemon portion in the script

    The script simply monitors a network stream and based on the output it will write to a file, see below

    #!/usr/bin/perl use strict; use IO::Socket; use POSIX; use warnings; use Config::Simple; use Win32::Daemon; sub logMonitor{ #subrouting to connect to the PBX log and monitor for +the Emergency calls my ($HOST, $PORT)= @_; OUTER: if (my $sock = new IO::Socket::INET(PeerAddr => $HOST, Peer +Port => $PORT,Proto => "tcp",)) { while (<$sock>) { s/^\0+//; # Remove leading null characters chomp ($_); my $data = substr($_, 1,17); my $event = substr ($data, 2,1); my $hr = substr ($data, 3,2); my $min = substr ($data,5,2); my $year = substr($data, 9,4); my $mon = substr($data, 13,2); my $day = substr ($data,15,2); #print "$hr:$min on $year-$mon-$day \n"; if ($event eq "A") { my $agent = substr($_, 17,4); # print "\n Agent $agent logged in \n" ; my $output = "Agent $agent logged in at $hr:$min on ex +t $year"; filePrint($output); } } else { print "Failed to connect to $HOST on $PORT. Will retry in a mi +nute.\n"; sleep 60; goto OUTER; } } #End of monitor subroutine sub filePrint{ #write data to file with date and time stamp my ($DATA)= @_; print "$DATA\n"; my $dateStamp = strftime '%Y-%m-%d', localtime; my $file = "$dateStamp.log"; my $timeStamp = strftime '%H:%M:%S', localtime; if (-f $file){ open (my $fh,'>>', $file); print $fh "$timeStamp | $DATA\n"; close $file; } else { open (my $fh,'>', $file); print $fh "$timeStamp | $DATA\n"; close $file; } }# End of filePrint routine my $cfg = new Config::Simple(); $cfg->read('config.ini'); my $HOST = $cfg->param("pbx"); my $PORT = $cfg->param("port"); logMonitor($HOST, $PORT); #Open the log monitoring subroutine

    Can anyone explain to me how to use Win32::Daemon with this script to create a service from it? Right now it will run as a command line tool

    many thanks!

DateTime::Format::Flexible; for Log Parse with multiple formatted lines
2 direct replies — Read more / Contribute
by TCLion
on Mar 23, 2017 at 13:54

    Basic Info:Log is in text lines. I am combining all lines and breaking it up to separate tags/lines. The Log files are copies and not being updated currently for development.

    Trying to figure out the best way to get my time from lines in log files. Parsing the line for date time error and error message to output to csv. This process is working on some of my log files but one in particular has 2 different time/date formats. For this reason the code is not picking up the lines not formatted the specific way.

    This line is ok format:
    2017-02-20T09:30:53.177000 20[] 0000000000000000 Error Description

    Second (problem)line
    Mon Feb 20 09:31:25 2017 INFO AGENTEXEC Error Description

    Since I now have 2 formats in 1 log I now have installed DateTime::Format::Flexible. Hoping this will fix my issue and convert one format to match the other.

    #Original Working code prior to 2nd date format #!/usr/bin/perl use IO::File; my $file = 'file.location.log'; open(MYLOG, "<$file") or die "Can't open $file: " . $!; my @mylog = <MYLOG>; close(MYLOG); my $myfixedlog = join("ENDOFLINE", @mylog); # Date 1 ,T 2, Tim +e 3 , 4, 5 , 6 , 7 , + 8 , 9 while ($myfixedlog =~ /([0-9]{4}-[0-9][0-9]-[0-9][0-9])(T)([0-9][0-9]: +[0-9][0-9]:[0-9][0-9])(.\d+\s\d+.\d+.\s)(.{16}\s)(\[\w+\])(\w+\S\s+)( +.+?)(ENDOFLINE)/smg) { $date = $1; $severity = $7; $timestamp = $3; $errormsg = $6; chomp($errormsg); print "New Error Found...\n"; print "0 $0\n"; print "1 $1\n"; print "2 $2\n"; print "3 $3\n"; print "4 $4\n"; print "5 $5\n"; print "6 $6\n"; print "7 $7\n"; print "8 $8\n"; print "9 $9\n"; }

    That was to get the positions for the output.
    Then to get the desired output using this code.

    use 5.18.0; use warnings; use IO::File; use Time::Piece; my $servername = "Server1"; # This is the Location of the Original Log File my $OLF = '\\\server\logs\server.log'; # This is the location of the file parsed my $file = 'server.log'; open(MYLOG, "<$file") or die "Can't open $file: " . $!; my @mylog = <MYLOG>; close(MYLOG); # This is the Location\Name of the file Being Created my $CSVLOG = 'output.csv'; open(OUTLOG, ">>$CSVLOG") ; my @outlog = <OUTLOG>; my $runtimestamp = localtime(time); my $myfixedlog = join("ENDOFLINE", @mylog); print OUTLOG "$OLF\n"; print OUTLOG "Server, Date, TimeStamp, Severity, ErrorMsg, \n"; # Date 1 ,T 2, Tim +e 3 , 4, 5 , 6 , 7 , 8 + , 9 while ($myfixedlog =~ /([0-9]{4}-[0-9][0-9]-[0-9][0-9])(T)([0-9][0-9]: +[0-9][0-9]:[0-9][0-9])(.\d+\s\d+.\d+.\s)(.{16}\s)(\[\w+\])(\w+\S)(.+? +)(ENDOFLINE)/smg) { my $date = $1; my $timestamp = $3; my $severity = $7; my $errormsg = join "",$6, $7, $8; chomp($errormsg); if ($errormsg =~ /DfException/ || $severity eq "error +:" || $errormsg =~ /started in/){ foreach ($errormsg =~ /DfException/ || $seve +rity eq "error:" || $errormsg =~ /started in/) {print OUTLOG "$servername, $date, $timestam +p, $severity, $errormsg \n"}}; # print OUTLOG "$servername, $da +te, $timestamp, $severity, $errormsg, \n" } print OUTLOG @outlog; close(OUTLOG); print "CSV - File Created Log File: $CSVLOG";

    So now trying to add and use new code but cant figure out how to set the desired format or how to have it collect and identify properly as before from original code. This is the added code I have found so far to the code to find locations for $myfixedlog

    DateTime::Format::Flexible; my $dt = DateTime::Format::Flexible->parse_datetime( $date, lang => [' +en'], );

    Ok so I guess the questions are:
    How to declare $date if its already being used (in code i am not yet familiar with)?
    If this code pulls out the date would it have a position or just be placed by $dt per line?
    If the date is pulled and formatted correctly would I still break up my line the same way?

    Looking at the preview it looks like i might have grabbed code for 2 different log files but the basics are the same.
    Any Assistance would be appreciated. And no I did not write the original code but I have modified these listed to work. I am Still new to Perl but it is getting easier the more I do.
    Hopefully this makes sense to someone.

Any reason NV is not marked as POK when accessed as string?
2 direct replies — Read more / Contribute
by vr
on Mar 23, 2017 at 07:22

    Just curious, not a 'problem' which requires solution. Maybe developers thought real numbers stringification is usually approximate, that's why? Or simply nobody cares?

    C:\>perl -MDevel::Peek -e "$x = 0.5; qq/$x/; Dump $x" SV = PVNV(0x38bb4) at 0xd217cc REFCNT = 1 FLAGS = (NOK,pNOK) IV = 0 NV = 0.5 PV = 0xd16014 "0.5"\0 CUR = 3 LEN = 28 C:\>perl -MDevel::Peek -e "$x = 0; qq/$x/; Dump $x" SV = PVIV(0xd200c4) at 0xd2685c REFCNT = 1 FLAGS = (IOK,POK,pIOK,pPOK) IV = 0 PV = 0xd16274 "0"\0 CUR = 1 LEN = 10

    Hence, 'double' is converted to string any time it is required. E.g.:

    use Benchmark qw/ cmpthese /; $x = 42.0; %h = (); cmpthese( -1, { F => sub { $h{ pack 'F', $x } = 1 }, s => sub { $h{ $x } = 1 }, });
    Rate s F s 344025/s -- -86% F 2490475/s 624% --

    I found that one my little application, which maintains kind of 'seen' hash, gets nice boost if hash keys are packed as above, instead of 'just used as they are'. Actually, keys are pixels coordinates, they became real numbers because library I use returns them so. Pixels are usually aplenty and hash is accessed a lot, therefore speed gain was significant. But of course it would be even faster if PV was added on first access and POK was set, i.e. without packing.

Parse string and extract word after specific pattern
5 direct replies — Read more / Contribute
by rinkish85
on Mar 23, 2017 at 06:49

    Hi, I need to parse the below string


    I am intrested only in part ROLLBACK.check

    Basically I am looking at what appears after "ROLLBACK" in above string.

    for e.g in this case its "check" .... that could be any word string like check2, check3, migration1, migration2.

    Thank you. Please suggest.
Perl Script execution while system in locked.
2 direct replies — Read more / Contribute
by 9mohit2
on Mar 23, 2017 at 06:46

    Can someone guide me whether all Perl scripts can be executed via Task Scheduler when the system is in locked state. I faced some issue with the scripts involving Win32::OLE use.

    Anyone having experience in this thing??Please comment
Search hash keys using values from array
8 direct replies — Read more / Contribute
by AhmedABdo
on Mar 23, 2017 at 06:36

    Hi dear all
    Assuming I have this array and hash.

    My @array = qw(world today is nice); my %hash = (0 => "Hello", 1=> "world" , 2=> "today" , 3=> "is" , 4=> " +nice" );
    I want to search thought the hash values using the array elements as values and then print the hash keys for each searched value from the array in a new array.
    It should print 1 for world, 2 for today, so on which will be saved in the new array
    I tried to use lookup with loop thought the array but it did not work.
    I used grep also, but did not hot what I want
    Any idea about how I can do it???
    Thanks in advance

Unable to use if condition inside foreach loop using perl?
3 direct replies — Read more / Contribute
by finddata
on Mar 23, 2017 at 05:38
    open my $project_file, '<', $input_dir; chomp(my @store_files = <$project_file>); #print @store_files; close $project_file; foreach my $temp_line(@store_files) { if(my $pattern =~ /^REVISION_LOCATION:/){ my @fetch_path=split (':',$temp_line); my $new_path=$fetch_path[1]; print $new_path; last; } } <c> Error: Use of uninitialized value in pattern match (m//)
SOAP sign more elements
No replies — Read more | Post response
by makita
on Mar 23, 2017 at 04:56
    Hello, does heve anybode experience how to create a signature of more elements via XML::Compile::WSS ::Signature? This is my code:
    my $wsa = XML::Compile::SOAP::WSA->new(version => '1.0'); my $wsdl = XML::Compile::WSDL11->new("some.wsdl"); my $wss = XML::Compile::SOAP::WSS->new(schema => $wsdl); my $cert_obj = Crypt::OpenSSL::X509->new_from_file($cert); my $token = XML::Compile::WSS::SecToken::X509v3->new ( certificate => + $cert_obj,); my $sig = $wss->signature( schema => $wsdl, token => $token, sign_types=>['wsa:Action','wsa:To', 'wsu:Timestamp', 'wsa: +MessageID','SOAP-ENV:Body'], signer=>DSIG_RSA_SHA1, public_key => $cert, private_key =>$cert_pk, );
    My problem is that only 'SOAP-ENV:Body' is always signed as default even i set more types. Inside of source code I've found more parameters like sign_when and sign_put. Don;t know how to use it and documentation is very poor
url get with string
2 direct replies — Read more / Contribute
by bigup401
on Mar 23, 2017 at 04:34

    why if i post like this it doesn't work

    $name = 'john'; my $req = HTTP::Request->new(GET => 'https://www.google.com/?name=$nam +e'); $req->content_type('application/json');

    but it works when no $name string in url

    my $req = HTTP::Request->new(GET => 'https://www.google.com/?name=john +'); $req->content_type('application/json');
Unable to connect to HTTPS servers
3 direct replies — Read more / Contribute
by prathap keerthipati
on Mar 23, 2017 at 02:57

    Hi Perl Monks,

    I have a perlscript which is connecting to urls and downloading the contents of that.The script we are running in unix environment connecting through the proxy server.For http urls it is running fine, but when i gave https url its throwing error like "500 cannot connect to url". I am using WWW::Mechanize module and LWP 6.04v in the script. Can anybody suggest the solution for this? and what are the possible ways to get 500 connection issue?

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.