Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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
Authenticate email
3 direct replies — Read more / Contribute
by dylinn
on Mar 04, 2015 at 11:29
    I am a non-Perl person. I was provided a script to install onto our host and am receiving the error: No unauthenticated relaying permitted. Instructions now are: You can use Net/SMTP.pm module. This article contains a list of Perl modules available on IPOWER's UNIX platform. http://www.ipower.com/knowledgebase/beta/article.bml?ArticleID=388 . Please check for the email module. Could someone provide me with an example string (if that is the right word)? Is there a specific place within the script that it needs to be? Any guidance greatly appreciated!!
CGI link to naked text
3 direct replies — Read more / Contribute
by Herbert37
on Mar 04, 2015 at 07:13

    How can I use CGI to create a clickable link to specific text in a web page that is not referenced by any tag?

    The equivalent manual action would be to go to the web page/link and hit Command F, enter the text to be searched for in the search box, and hit Return.

    Thank you in advance for any help you can provide!

Help me to understand increment / decrement operator behavior in perl
7 direct replies — Read more / Contribute
by sam_bakki
on Mar 04, 2015 at 00:55

    Hi Monks

    I have the following code, IMHO it should print "T1" but to my surprise, It print "T2". Then I compile the "C" version of the same code in gcc and executed, It also printed "T2" , Then the C version, I have compiled in Visual Studio, It prints "T1" "T2".

    I am confused now. Can some one please throw light?

    use strict; my $a = 10; #IMHO: $a-- should finish the current operation and then -1 if ($a == $a--) { print "\n T1"; } $a=10; #IMHO: --$a should -1 first and continute with current operation , i.e + if compare if ($a == --$a) { print "\n T2"; } #ENV: #Active Perl 5.20, Windows 7, x64 #Output # T2

    Thanks & Regards,
    Bakkiaraj M
    My Perl Gtk2 technology demo project - http://code.google.com/p/saaral-soft-search-spider/ , contributions are welcome.

Mechanize can't find a link with hidden text.
1 direct reply — Read more / Contribute
by efedotov
on Mar 04, 2015 at 00:31

    I'm new to Mechanize. I'm trying to scrape: http://www.penny-arcade.com/comic/1998/11/18

    Specifically, the next button that leads to the next comic.The link has the text "Next" but Perl is reporting the error that it can't find the link. My code:

    $agent->follow_link(text => 'Next');
Troubles with Unshift and Push
2 direct replies — Read more / Contribute
by nat47
on Mar 04, 2015 at 00:06
    This is probably a dumb question. I am trying to take a list of @num_weights and add one number from @leftovers to the front, save it in a @hold_generations array, for all @leftovers. The code is:
    my @num_weights = [36,264,188]; my @leftovers = [1,2,3,4]; my @hold_generations; my $i = 0; for(my $p = 0; $p < 1;$p++) #Go through all leftovers { my $pick = $leftovers[$p]; unshift @num_weights, $pick; #add item say Dumper(@num_weights); push @hold_generations, @num_weights; #store new array say Dumper(@hold_generations); splice @num_weights, 1, 1; #undo adding item say Dumper(@num_weights); }
    However, I am getting this result from Dumper()
    $VAR1 = [ 1, 2, 3, 4 ]; $VAR2 = [ 36, 264, 188 ]; $VAR1 = [ 1, 2, 3, 4 ]; $VAR2 = [ 36, 264, 188 ]; $VAR1 = [ 1, 2, 3, 4 ];
    Shouldn't Var1 be 1,36,264,188? That's whay I'm trying to get!

    I'm sure there is a better way to do this, but I'd really just like to know whats going on.

    My goal is to use bigger data sets and later on check @hold_generations for specific instances and then use that instance to re-run this whole thing.

Globalize Filehandles and Visibility in Packages?
4 direct replies — Read more / Contribute
by smknjoe
on Mar 03, 2015 at 23:44

    Hi all,

    I've been using Perl for the last 14 months (where I was a heavy Awk user prior to that) and now want to graduate to making my own packages. Defining the package seems fairly straight-forward using the template from perlmod, but I'm having difficulty trying to globalize my filehandle as I would rather not pass the filehandle explicitly into each of my subroutines, as each one will write into the file (there are ~15 small subroutines.)

    Main code:

    #!/usr/bin/env perl use strict; use warnings; #use diagnostics; use MyPkgDirectory::MyPkg; my $wmode = '>'; my $rmode = '<'; my $filename = "zz_file.txt"; #my $FH_text_W; # this doesn't work, lexical? our $FH_text_W; # can I globalize my filehandle? open($FH_text_W, $wmode, $filename) || die "ERROR: cannot open $filena +me for writing $!"; print "Debug: Filehandle is: $FH_text_W\n"; &samplewrite(10, "foofoo"); # I don't want to pass in the FH! close ($FH_text_W);

    My package:

    package MyPkgDirectory::MyPkg; # assumes MyPkgDirectory/MyPkg.pm use strict; use warnings; BEGIN { require Exporter; our $VERSION = 1.00; our @ISA = qw(Exporter); our @EXPORT = qw(samplewrite); } my $FH_text_W = $main::FH_text_W; #my $FH_text_W; sub samplewrite { my ($ver, $sheet) = @_; print "Debug: Version $ver\n"; print "Debug: SHEET $sheet\n"; print $FH_text_W "Version $ver\n"; # Bombs out here print $FH_text_W "SHEET $sheet\n"; # and here too } END { } 1;

    The output when I run my testcase and the ensuing error message I'm getting is as follows:

    Debug: Filehandle is: GLOB(0x3f8dac)

    Debug: Version 10

    Debug: SHEET foofoo

    Can't use an undefined value as a symbol reference at MyPkgDirectory/MyPkg.pm line 21.

    Thanks in advance,

    Joe
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
3 direct replies — 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."
Text Editor Qeustion
6 direct replies — Read more / Contribute
by Anonymous Monk
on Mar 03, 2015 at 15:26

    Hi Monks

    I started using gVim on Windows to write some sys admin type Perl scripts that could be from a few to about 200+ lines. I was earlier using Komodo Edit. While I really like gVim, the more I try to add stuff like neocomplete/syntastic, the slower/quirckier it becomes. Using PerlCritic throws ups wierd errors in gVim. I've spent weeks trying to fix it, but now it's at a point where I'm fighting more with the plugins than using it for writing scripts. Is there anyone here who uses something like KomodoEdit or similar? Do you think using Vim (or Emacs perhaps) actually makes you a better Perl Programmer? Can one still use an IDE and be a good Perl Programmer? Apologies if my question appears superfluous, but would be thankful if you folks could answer.

Recommendations for efficient data reduction/substitution application
8 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.


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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (10)
    As of 2015-03-05 11:56 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When putting a smiley right before a closing parenthesis, do you:









      Results (141 votes), past polls