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
strategy for tracking down old version of "use List::Utils"
3 direct replies — Read more / Contribute
by Anonymous Monk
on Mar 03, 2015 at 18:09

    Hi PerlMonks,

    I need your assistance here. I'm trying to figure out which loaded module (in a large project) is including an old version of List::Util (v1.18) - i.e. the version that is included with core perl 5.8. I am trying the following:

    From a breakpoint in my code, I'm running

    use File::Grep qw(fgrep) x fgrep { /list\:\:util/i } values %INC

    I still haven't had success yet locating the specific module, but it does find the correct inclusions. The problem here is the chain of includes, so at the moment i'm only finding the top level. Is this the best/only way of doing this or maybe there are utilities that I'm unaware that can help me?

    With so many perl module versions floating around, this seems like it might be a common task.

    Thank you for your time.

    Michael

Have perl replace a bunch of text in lots of files
1 direct reply — Read more / Contribute
by symgryph
on Mar 03, 2015 at 16:15

    I have a bunch of text files that need some 'post processing' performed on them. Additionally, I want to name the file according to a specific regex contained within each file itself. I have started programming, but can't get filehandles to be turned into an array (which I then use to replace text).

    Here is an example of an input file

    get system status: Version: FortiGate-60D v5.0,build4459,140410 (GA) Virus-DB: 23.00950(2015-03-02 19:54) Extended DB: 23.00950(2015-03-02 19:53) IPS-DB: 5.00616(2015-02-26 01:09) IPS-ETDB: 0.00000(2001-01-01 00:00) Serial-Number: FGT60D46234234234 Botnet DB: 1.00000(2012-05-28 22:51) BIOS version: 04000023 System Part-Number: P14482-03 Log hard disk: Available Internal Switch mode: interface Hostname: HOSTNAME Operation Mode: NAT Current virtual domain: root Max number of virtual domains: 10 Virtual domains status: 1 in NAT mode, 0 in TP mode Virtual domain configuration: disable FIPS-CC mode: disable Current HA mode: a-p, master Branch point: 271 Release Version Information: GA System time: Tue Mar 3 12:38:43 2015 -------------------------------------------------------------:

    Here is what I hope it will output as:

    HOSTNAME # get system status Version: FortiGate-60D v5.0,build4459,140410 (GA) Virus-DB: 23.00950(2015-03-02 19:54) Extended DB: 23.00950(2015-03-02 19:53) IPS-DB: 5.00616(2015-02-26 01:09) IPS-ETDB: 0.00000(2001-01-01 00:00) Serial-Number: FGT60D46234234234 Botnet DB: 1.00000(2012-05-28 22:51) BIOS version: 04000023 System Part-Number: P14482-03 Log hard disk: Available Internal Switch mode: interface Hostname: HOSTNAME Operation Mode: NAT Current virtual domain: root Max number of virtual domains: 10 Virtual domains status: 1 in NAT mode, 0 in TP mode Virtual domain configuration: disable FIPS-CC mode: disable Current HA mode: a-p, master Branch point: 271 Release Version Information: GA System time: Tue Mar 3 12:38:43 2015 #HOSTNAME -----------------------------------:

    Finally I would like it to rename each file from its original name to the hostname matched by the 'hostname' match within the program.

    #!/usr/bin/perl -w open DATA, "*.txt"; @file=<DATA>; while (@file) { if (/Hostname\:\s(\S+)/) { $hostname=$1; print "$hostname\n" } if (/\:$/) { print "# $hostname \n $_"; } }

    I can't get the system to even print successfully I get loads of errors. I do know that th regexes are right, and have tested these independently. I also know that I can use a 'rename' function to rename the files, but am not sure how to get the variables to keep state between all the files.

    "Two Wheels good, Four wheels bad."
Recommendations for efficient data reduction/substitution application
5 direct replies — Read more / Contribute
by atcroft
on Mar 03, 2015 at 13:54

    Can anyone recommend the most efficient method of applying a large number of regular expression substitutions (> 100) to a relatively large number of input data lines (> 10_000_000)?

    In a recent project at $work, I wrote a script to generate a report of logged problem occurrences. In this process, I have to simplify the data by removing variations that are "noise" relative to the issues I am looking at (such as a process ID, an amount of free memory when a memory use threshold is crossed, or a part of a message with more detail than needed in this report). Once I've broken the record into a few basic parts, I currently have a list of 100+ regexen, replacement strings, and order of application to use (currently stored in a DB table), and a simple for loop to apply them to each record:

    foreach my $regex (@conversions) { if ( $entry{$k} =~ s/$regex->{from}/$regex->{to}/g ) { $regex->{count}++; # Count of applications used to determine if # a particular substitution is warranted. } }
    I have the nagging feeling, however, that there is a more efficient way of dealing with this. (Also, this processing takes up the largest percentage of the processing time in the script, which often takes hours to run.) Any thoughts/suggestions?

    Thank you for your time and attention, and any direction you may provide.

minimal match regulara expression
4 direct replies — Read more / Contribute
by fionbarr
on Mar 03, 2015 at 11:12
    I have a string that looks like:
    wppwd01a0099_NET_basp_virtual_adapter
    I want to capture the chars before the first '_' but what I end up with is: <code> wppwd01a0099_NET_basp_virtual <code> The regular expression I'm trying is: <code> if ( $this_server =~ m/^(\w+)_/ ) { $this_server = $1; } <\code> what am I doing wrong?
Exchange Heuristic and Swapping Code
1 direct reply — Read more / Contribute
by nat47
on Mar 03, 2015 at 10:54
    I posted a thread the other day and I didn't include any code. What I'm trying to do is: while the ideal state is still better than our current state, exchange 1 item for a different item in a leftovers array. So, the firs thing to do is check if the item to exchange fits, then check if it is a better $current_best. Right now I'm just trying to drop by order, pick first, loop through replacing the remaining items, then go back to the main loop change to second item. If state gets 'better' then the state is kept and looped through with that state. When the ideal state is met, it should stop.

    This is the code I've started to write, it doesn't work and isn't done in Perl fashion...

    @solution has the current set of items, @leftovers has the possible items that aren't inside @solution

    while($ideal_state > $current_best) { my $coun = 0; for(my $a = 0; $a < scalar @solution; $a++) { for(my $b = 0; $b < scalar @leftovers; $b++) { my $hold_previous = $solution[$a]; $solution[$a] = $leftovers[$b]; if($max_weight > totalweight(@solution)) { if(totalscore(@solution)>=$current_best) { $leftovers[$b] = $hold_previous; } } else { $solution[$a] = $hold_previous; } } #print "Main loop"; } }
Perl try { } catch(e) { }
2 direct replies — Read more / Contribute
by Superfox il Volpone
on Mar 03, 2015 at 09:16
    Hi there,
    I am looking for a way to implement the construct:
    try { die ("Hello world"); } catch(e) { print("Exception: " $e); }
    I know this snippet from Programming Perl 4th ed,pp 329, and it works using locals:
    sub try(&$) { my ($try, $catch) = @_; eval { # perl try &$try }; if($@){ # perl catch local $_ = $@; &$catch(); } } sub catch(&){ $_[0]; }
    And this works as try { ... } catch { print("Exception caught: $_"); };
    I'd like to refer the exception with something like catch(e){ ... }.

    I would prefer to avoid using any external module such as TryCatch for portability reasons.

    Thanks
    s.fox
Changing from http call to https using SOAP::Lite
1 direct reply — Read more / Contribute
by allwynpoikavila
on Mar 03, 2015 at 04:57
    Hello Monks, We are migrating our perl script to call https websites instead of http ones, but when we change the syntax of site inside the script from http to https to get the web data, we are getting error. what all changes needs to be done in the script so that it can get https websites data? our code uses SOAP::Lite for connecting to sever. can someone please help by posting the code snippet?, its urgent...
FTPSSL Bad file number
2 direct replies — Read more / Contribute
by Bettan
on Mar 03, 2015 at 03:49
    Hi, I'm not a perl programmer but I'm trying to modify an existing script from Net::FTP to NET::FTPSSL. Now there are problem to get the files. I got this error: syswrite() on closed filehandle GEN0 at Can't write command on socket: Bad file number This is part of the code:
    # GET FILE LIST if ( !$error_flag ) { if ( !$debug ) { @files = $ftp->list() or $error_flag = + 1; $ftp->quit if $error_flag; } else { opendir( DIR, "$debug_ftp_directory/$d +irectory_local" ) or $error_flag = 1; @files = readdir(DIR); closedir(DIR); } $report = $report . " ERROR: Can't get file l +ist $!\n" if $error_flag; $error_cnt++ if $error_flag; } # GET ALL FILES (*.enc) TO LOCAL DIRECTORY, DELETE THE +M ON SERVER foreach $file (@files) { if ( !$error_flag && $file =~ m/\.enc$/ ) { $report = $report . " Retrieving file + $file\n"; if ( !$debug ) { $ftp->get($file) or $error_fla +g = 1; $ftp->quit if $error_flag; $ftp->delete($file) or $error_ +flag = 1; $ftp->quit if $error_flag; } else { move( "$debug_ftp_directory/$d +irectory_local/$file", '.' ) or $error_flag = 1; } if ( !$error_flag ) { $downloaded_cnt++; } else { $report = $report . " ERROR: Can't re +trieve file $file $!\n"; $error_cnt++; } } } if ( ( !$error_flag ) && ( !$debug ) ) { $ftp->quit; }
    Would be greatful if someone can help me. This worked out with Net::FTP Thanks in advance Bettan
Calling a script from a script with GetOpt::Long arguments
3 direct replies — Read more / Contribute
by jjap
on Mar 03, 2015 at 01:26
    Dear Monks,

    I wrote a Perl script which receives 2 arguments specified with GetOpt::Long which I call the following way:

    perl heavylifter.pl --file thisfile --db thisdatabase

    To save myself some time, I made a script which makes a system call to that script over a list of files. However I am at a loss to find out how to specify the arguments in long form.

    To get by, I modified my heavylifter script to receive the file as $ARGV[0] and hardcoded the db in it to avoid the --db argument so the calling script looks like the following:
    use strict; use warnings; my @listFiles = ("thisfile", "thatfile", "thisotherfile"); foreach my $i (@listFiles) { print "file passed: $i\n"; system ("perl.exe", "full_path_to_heavylifter.pl", $i) ; }

    This is clearly a step backward and I would appreciate any guidance on how to properly call a script from a script with GetOpt::Long arguments.

    Best regards and thanks!
Debating With Friends
4 direct replies — Read more / Contribute
by exquisitemb
on Mar 02, 2015 at 23:22
    Hi All -- A friend is doing some weird embedded programming in C, and getting some strange results, so he asked what perl did (just for the sake of asking). I wrote up the quick comparison, and I got interesting results, now I'm intrigued. How/why does this work? (Yes, I know this is really stupid code)
    #!/usr/bin/perl $x; if(($x=1) eq ($x=2)) { print "equal\n"; } else { print "not equal\n"; }
    Result is that it gives is "equal". Why?

Add your question
Title:
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
  • Outside of code tags, you may need to use entities for some characters:
            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.