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
Trying dirty trick with Imager and Inline C, but having difficulties. Can you help?
1 direct reply — Read more / Contribute
by vr
on Aug 01, 2015 at 04:00

    At some point in my program, it seems that easiest (fastest, etc.) thing to do is to directly replace raw image data with data from Perl scalar. Needless to say, Imager API (both Perl and C) don't provide for this, but I thought I'd get away with following subroutine (here it is with a test program that, of course, makes no sense):

    use strict; use warnings; use Imager; use Inline with => 'Imager'; use Inline C => << 'END_OF_C'; void _replace( Imager img, SV* str ) { unsigned char* ptr = SvPV_nolen( str ); myfree( img-> idata ); img-> idata = ptr; } END_OF_C my $i = Imager-> new( xsize => 99, ysize => 99 ); for ( 'A' .. 'F' ) { print "$_\n"; my $s; $i-> write( data => \$s, type => 'raw' ); my $j = $i-> copy(); # Here, it's rumored, some nasty things # happening, that involve $j and $s. # But maybe nothing happens. _replace( $i, $s ); } __END__ ~$ perl A B *** Error in `perl': double free or corruption (!prev): 0x0000000002d6 +7990 *** Aborted (core dumped)

    Unfortunately, my C is below-zero level. I understand I should prevent Perl from freeing memory that now is Imager responsibility, but don't know how. And, actually, replacement itself works OK. There's only error freeing memory, that happens just sometimes. This program seems to reproduce it always.

Difference between leftward and rightward list operators
1 direct reply — Read more / Contribute
by Firsov
on Aug 01, 2015 at 02:25


    I was reading perlop manual. And i don't understand one thing: What difference between rightward and leftward list operators?

    I guess that leftward list operator this is function that accept LIST (has higher precedence that comma operator). But what is a right ward list operator and why it has low precedence?

when i run a log file,will get some output in numbers say'12345' ,i would like to copy it in excel file?
2 direct replies — Read more / Contribute
by Skumar2015
on Jul 31, 2015 at 17:53
    #!/usr/local/bin/perl use strict; use warnings; my $filename = 'C:Desktop\data.log'; open(my $file1, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!"; while (my $row = <$file1>) { chomp $row; print "$row\n"; } when i run this file i will get some '12345'as an output. Now I need to copy the log file output into existing excel sheet day by day. please help me.
A regex question.
2 direct replies — Read more / Contribute
by FeistyLemur
on Jul 31, 2015 at 17:49

    This is my first post here, I have a question about something I can't explain.

    I'm pretty sure I know what I did wrong, I'm just not 100% sure why it was wrong and wanted to clarify to better my understanding of regex in perl, as I'm fairly inexperienced and looking to improve.

    I was searching for vlan ids for removal in the output of "ip addr" in the following way.

    my $ethernet=`ip addr`; if ($ethernet!~/eth1\.\d{4}\@/gm){ print "No vlans exist on device.\n"; exit; } while ($ethernet=~/(eth1\.\d{4})\@/gm) { print `ip link del $1\n"; print "Removed $1\n"; }

    Doing this as above mostly worked, but it would always miss the first entry for no reason I can explain. So if there were 25 vlan entries in the string from 1001 to 1025 it would match and delete 1002-1025 without fail, and miss 1001 every time.

    Changing line 2 to:

    if ($ethernet!~/eth1\.\d{4}\@/){

    Does what I intended to do.

    I had the same problem with a similar line I was using to scrub IP addresses off the vlan devices again missing the first match, I just don't understand why the while loop breaks in the way it does because of the /gm flag on the preceeding if statements match, and was hoping someone could explain. Thanks in advance.

How to Save a variable outside of a loop
6 direct replies — Read more / Contribute
by Calebros
on Jul 31, 2015 at 17:26
    Hello everyone, I am very new coder trying to filter duplicate line records out of a .csv file by comparing two values on the current line to the previous line; however my code is returning that the variable $prev_l is uninitialized. Could someone tell me why this is happening?
    #!/usr/bin/perl use warnings; use strict; while(<>){ my $hitlength; my $prevalence; my $prev_l; my $prev_p ; my $line = $_; chomp $line; if($line =~ /^[\d]/){ my @hitline = split(/,/ , $line); $hitlength = $hitline [4]; $prevalence = $hitline [65]; if($hitlength == $prev_l and $prevalence == $prev_l){ next; } else { print "$line\n"; } $prev_l=$hitlength; $prev_p=$prevalence; } }

    Here are a couple sample lines of data
    23750,57495,78362,xxxx,2853,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +,1,1,1,1,60,100 23751,57497,78364,xxxx,2853,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +,1,1,1,1,60,100 23752,57500,78367,xxxx,3114,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +,1,1,1,1,60,100 23753,57502,78369,xxxx,3114,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +,1,1,1,1,60,100 23754,57504,78371,xxxx,101,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,60,100
remote os
2 direct replies — Read more / Contribute
by bigup401
on Jul 31, 2015 at 16:17

    is there any sample code to check remote host OS

Perl Debugger rcfile on Windows
1 direct reply — Read more / Contribute
by VinsWorldcom
on Jul 31, 2015 at 15:14

    Windows Monks - has anyone gotten the Perl Debugger rcfile (.perldb / perldb.ini) to work on Windows? I can use the Perl debugger fine with 'perl -d <progfile> [args]', but if I have a .perldb (perldb.ini) file defined, I get mixed, unsatisfying results.

    I'm on Windows 7 x64 with Strawberry 5.18.1.

    Essentially I've tried .perldb and perldb.ini in both the current directory and my home directory. It seems only perldb.ini is recognized and when it finds it, regardless of location, I get the error:

    perldb: Must not source insecure rcfile ./perldb.ini. You or the superuser must be the owner, and it must not be writable by anyone but its owner.

    So I've tried to modify permissions of the perldb.ini file on Windows by breaking the parent inheritance on that file and making sure I'm the owner and the only one with "Full Permissions" - which still doesn't fix the problem.

    Anyone get this working successfully?

    UPDATE: I've narrowed it down in

    'use vars qw($rcfile);' does a check which for Windows will return 'perldb.ini' regardless explaining why '.perldb' has no effect, despite POD about running interactive vs non- mode.

    Then, that 'perldb.ini' file is (eventually) run through "sub is_safe_file" which does a 'stat()' and on Windows, is obviously not returning the proper value to create a false when bit-masked with 022. See the function in for more details as well as POSIX::S_ISDIR() with $stat->mode values from Windows vs. Linux, which has the mode value I'm getting "33206" on Windows.

    That being said, I could do all sorts of things to fix this by editing, but is there a way / argument /something to get this to work without me monkeying with a module's code?

Perl system command memory usage in threads
3 direct replies — Read more / Contribute
by rmahin
on Jul 31, 2015 at 14:24

    SOLVED Updated to perl 5.20.2 and all problems vanished.

    Hello! Been noticing some strange memory issues with a script I'm working on, and have come up with this to highlight the issues

    use strict; use warnings; use threads; use threads::shared; $|++; my $DONE :shared = 0; my $lock :shared; my $execMethod = $ARGV[0] || 0; if($execMethod !~ /[12345]/){ print "Must pass an exec method:\n"; print "1 = backticks\n"; print "2 = backticks synchronized\n"; print "3 = open\n"; print "4 = open synchronized\n"; print "5 = system\n"; exit 1; } sub execute{ my $cmd = shift; if($execMethod == 1){ `$cmd` }elsif($execMethod == 2){ lock $lock; `$cmd` }elsif($execMethod == 3){ open(my $fs, "-|", $cmd); foreach(<$fs>){}; close $fs; }elsif($execMethod == 4){ lock $lock; open(my $fs, "-|", $cmd); foreach(<$fs>){}; close $fs; }elsif($execMethod == 5){ system($cmd . ">nul"); } } sub worker{ while(!$DONE){ execute('echo hello world'); } } my @workers = map threads->create( \&worker), (1..30); print "Press <enter> to terminate\n"; <STDIN>; $DONE = 1; $_->join() for @workers;

    Script is invoked with <script>.pl executionMethod

    Question 1 relates to memory usage. Methods 2, 4, and 5 all exhibit a permanent memory creep. Is there a way to fix this and let this script run forever? If the solution is periodically join threads, this is not feasible. I have tried this a couple years ago and came across some bug but outlined in this node and changing to an approach like this would require far too much testing to ensure no other bugs occur. If there really is no solution, would be good to know :)

    Question 2: For methods 1 and 3, why do i need to synchronize them? If I do not, the script just hangs up usually after only a few seconds. Synchronizing this access kind of seems to defeat the purpose of running system commands in threads, is there something I can do differently?

    *I have been running this on Windows 2008 R2, using perl 5.18.2

    Thanks in advance for all your help

    UPDATE 1: Here is the log file from perfmon running test 5 for a little over a minute and eating roughly 150MB.

    UPDATE 2:

    perl -v output

    This is perl 5, version 18, subversion 1 (v5.18.1) built for MSWin32-x +64-multi-thread (with 1 registered patch, see perl -V for more detail) Copyright 1987-2013, Larry Wall Binary build 1800 [297570] provided by ActiveState http://www.ActiveSt Built Sep 20 2013 15:07:17 Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at, the Perl Home Pa +ge.


    Host Name: VCLOUD291 OS Name: Microsoft Windows Server 2008 R2 Enterprise OS Version: 6.1.7601 Service Pack 1 Build 7601 OS Manufacturer: Microsoft Corporation OS Configuration: Standalone Server OS Build Type: Multiprocessor Free Registered Owner: Windows User Registered Organization: Product ID: 55041-507-5915375-84291 Original Install Date: 4/23/2014, 12:10:03 PM System Boot Time: 7/31/2015, 1:49:06 PM System Manufacturer: Microsoft Corporation System Model: Virtual Machine System Type: x64-based PC Processor(s): 1 Processor(s) Installed. [01]: Intel64 Family 6 Model 46 Stepping 6 +GenuineIntel ~1862 M hz BIOS Version: American Megatrends Inc. 090004 , 3/19/2009 Windows Directory: C:\Windows System Directory: C:\Windows\system32 Boot Device: \Device\HarddiskVolume1 System Locale: en-us;English (United States) Input Locale: en-us;English (United States) Time Zone: (UTC-07:00) Arizona Total Physical Memory: 4,096 MB Available Physical Memory: 1,635 MB Virtual Memory: Max Size: 6,143 MB Virtual Memory: Available: 3,364 MB Virtual Memory: In Use: 2,779 MB Page File Location(s): C:\pagefile.sys Domain: WORKGROUP Logon Server: \\VCLOUD291 Hotfix(s): 46 Hotfix(s) Installed.
Output Queue from Multiple Threads
3 direct replies — Read more / Contribute
by bigbot
on Jul 31, 2015 at 09:56

    Hi monks, I had a question about implementing an output queue from multiple threads, using Thread::Queue. It's "working" without warnings or errors, but the memory usage gets way out of control, and it's also a lot slower than using locks and shared variables (which I assume it cause I screwed something up). I understand the memory usage would go up if the output queue was getting more than it could handle. In any case I can't seem to find many examples so any advice would be appreciated.

    The reason I wanted to use an output queue is that the worker queue is processing very large files on different computers (so it made sense to use threads). The output needs to be serialized. Previously I used a lock as I said. I heard that using an output queue would be better design. So I want to pass the output from the worker threads to the output queue and print the data there.

    sub runSearch { my $outputQueue = Thread::Queue->new(); my $outputThread = threads->create(\&outputSub,$outputQueue); my $workerQueue = Thread::Queue->new(); my @workerThreads = map threads->create(\&workerSub,$workerQueue,$ou +tputQueue), 1..$numThreads; $workerQueue->engueue($_) foreach @computer; $workerQueue->enqueue(undef) for 1..$numThreads; $outputQueue->enqueue(undef); $_->join for @workerThreads; $outputThread->join; } sub workerSub { my ($workerQueue,$outputQueue) = @_; while (my $computer = $workerQueue->dequeue()) { ## SSH command retrieving grep results and processing $outputQueue->enqueue($results); } } sub outputSub { my $outputQueue = $_; while (my $packet = $outputQueue->dequeue()) { print $results; } }
execute csh file from perl script
4 direct replies — Read more / Contribute
by argha
on Jul 31, 2015 at 07:43
    hello folks, i want to execute run_dynamic.csh file from my perl script. also my cshell file consist of environment variables command like setenv. i tried system("csh","file pointer"); but not working. please throw some light on it. thanks, argha

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!
  • 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 about the Monastery: (6)
    As of 2015-08-01 11:08 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found
      past polls