Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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
Unable to install perl module Sendmail
2 direct replies — Read more / Contribute
by sravs448
on Sep 15, 2014 at 16:24
    I am not able to install perl sendmail module. Below are the steps I followed. Any help will be greatly appreciated.
    scmadmin@Linux-sdfdsf:<bin,28> perl -v This is perl, v5.8.8 built for x86_64-linux-thread-multi Copyright 1987-2006, Larry Wall 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.
    scmadmin@Linux-sdfdsf:<bin,29> /usr/bin/perl -MCPAN -e 'install Mail:: +Sendmail' Sorry, we have to rerun the configuration dialog for due to some missing parameters... The following questions are intended to help you with the configuration. The CPAN module needs a directory of its own to cache important index files and maybe keep a temporary mirror of CPAN files. This may be a site-wide or a personal directory. I see you already have a directory /home/scmadmin/.cpan Shall we use it as the general CPAN build and cache directory? <cpan_home> CPAN build and cache directory? [/home/scmadmin/.cpan] /home/scmadmin/ +.cpan Unless you are accessing the CPAN on your filesystem via a file: URL, needs to keep the source files it downloads somewhere. Please supply a directory where the downloaded files are to be kept. <keep_source_where> Download target directory? [/home/scmadmin/.cpan/sources] /home/scmadm +in/.cpan/sources <build_dir> Directory where the build process takes place? [/home/scmadmin/.cpan/b +uild] /home/scmadmin/.cpan/build Normally keeps config variables in memory and changes need to be saved in a separate 'o conf commit' command to make them permanent between sessions. If you set the 'auto_commit' option to true, changes to a config variable are always automatically committed to disk. <auto_commit> Always commit changes to config variables to disk? [no] no can limit the size of the disk area for keeping the build directories with all the intermediate files. <build_cache> Cache size for build directory (in MB)? [100] 100 The CPAN indexes are usually rebuilt once or twice per hour, but the typical CPAN mirror mirrors only once or twice per day. Depending on the quality of your mirror and your desire to be on the bleeding edge, you may want to set the following value to more or less than one day (which is the default). It determines after how many days downloads new indexes. <index_expire> Let the index expire after how many days? [1] 1 By default, each time the CPAN module is started, cache scanning is performed to keep the cache size in sync. To prevent this, answer 'never'. <scan_cache> Perform cache scanning (atstart or never)? [atstart] atstart To considerably speed up the initial CPAN shell startup, it is possible to use Storable to create a cache of metadata. If Storable is not available, the normal index mechanism will be used. Note: this mechanism is not used when use_sqlite is on and SQLLite is running. <cache_metadata> Cache metadata (yes/no)? [yes] yes The CPAN module can detect when a module which you are trying to build depends on prerequisites. If this happens, it can build the prerequisites for you automatically ('follow'), ask you for confirmation ('ask'), or just ignore them ('ignore'). Please set your policy to one of the three values. <prerequisites_policy> Policy on building prerequisites (follow, ask or ignore)? [ask] ask Every Makefile.PL is run by perl in a separate process. Likewise we run 'make' and 'make install' in separate processes. If you have any parameters (e.g. PREFIX, UNINST or the like) you want to pass to the calls, please specify them here. If you don't understand this question, just press ENTER. Typical frequently used settings: PREFIX=~/perl # non-root users (please see manual for more hint +s) <makepl_arg> Parameters for the 'perl Makefile.PL' command? [] Parameters for the 'make' command? Typical frequently used setting: -j3 # dual processor system (on GNU make) <make_arg> Your choice: [] Parameters for the 'make install' command? Typical frequently used setting: UNINST=1 # to always uninstall potentially conflicting fil +es <make_install_arg> Your choice: [] A Build.PL is run by perl in a separate process. Likewise we run './Build' and './Build install' in separate processes. If you have any parameters you want to pass to the calls, please specify them here. Typical frequently used settings: --install_base /home/xxx # different installation dire +ctory <mbuildpl_arg> Parameters for the 'perl Build.PL' command? [] Parameters for the './Build' command? Setting might be: --extra_linker_flags -L/usr/foo/lib # non-standard library locati +on <mbuild_arg> Your choice: [] Do you want to use a different command for './Build install'? Sudo users will probably prefer: su root -c ./Build or sudo ./Build or /path1/to/sudo -u admin_account ./Build <mbuild_install_build_command> or some such. Your choice: [./Build] Parameters for the './Build install' command? Typical frequently used setting: --uninst 1 # uninstall conflicting files <mbuild_install_arg> Your choice: [] If you're accessing the net via proxies, you can specify them in the CPAN configuration or via environment variables. The variable in the $CPAN::Config takes precedence. <ftp_proxy> Your ftp_proxy? [] <http_proxy> Your http_proxy? [] <no_proxy> Your no_proxy? [] CPAN needs access to at least one CPAN mirror. As you did not allow me to connect to the internet you need to supply a valid CPAN URL now. Please enter the URL of your CPAN mirror Configuration does not allow connecting to the internet. Current set of CPAN URLs: Enter another URL or RETURN to quit: [] New urllist Please remember to call 'o conf commit' to make the config permanent! CPAN: Storable loaded ok (v2.15) Going to read '/home/scmadmin/.cpan/Metadata' Database was generated on Mon, 15 Sep 2014 09:53:18 GMT Running install for module 'Mail::Sendmail' CPAN: YAML loaded ok (v0.72) Running make for M/MI/MIVKOVIC/Mail-Sendmail-0.79.tar.gz CPAN: Digest::SHA loaded ok (v5.48) CPAN: Compress::Zlib loaded ok (v2.03) Checksum for /home/scmadmin/.cpan/sources/authors/id/M/MI/MIVKOVIC/Mai +l-Sendmail-0.79.tar.gz ok CPAN: Archive::Tar loaded ok (v1.68) Mail-Sendmail-0.79/ Mail-Sendmail-0.79/Todo Mail-Sendmail-0.79/README Mail-Sendmail-0.79/MANIFEST Mail-Sendmail-0.79/ Mail-Sendmail-0.79/ Mail-Sendmail-0.79/Changes Mail-Sendmail-0.79/Makefile.PL Mail-Sendmail-0.79/Sendmail.html CPAN: File::Temp loaded ok (v0.22) Going to build M/MI/MIVKOVIC/Mail-Sendmail-0.79.tar.gz Checking if your kit is complete... Looks good Writing Makefile for Mail::Sendmail ********************************************************************** NOTE ---- The test ( is a real test for this module: it sends an e-mail. You may want to edit before running it (directly or through make test), so the mail is sent to you. If you also leave my address, the mail goes into a database from which I can get statistics. Read the docs, and have fun... ********************************************************************** cp blib/lib/Mail/ Manifying blib/man3/Mail::Sendmail.3pm MIVKOVIC/Mail-Sendmail-0.79.tar.gz make -- OK Running make test PERL_DL_NONLAZY=1 /usr/bin/perl "-Iblib/lib" "-Iblib/arch" 1..2 Test Mail::Sendmail 0.79 Trying to send a message to the author (and/or whoever if you edited t (The test is designed so it can be run by Test::Harness from Edit it to send the mail to yourself for more concrete feedback. If yo +u do this, you also need to specify a different mail server, and possibl +y a different From: address.) Current recipient(s): 'Sendmail Test <>' ok 1 Server set to: Sending... content of $Mail::Sendmail::log: Mail::Sendmail v. 0.79 - Mon Sep 15 16:16:27 2014 Date: Mon, 15 Sep 2014 16:16:27 -0400 Server: localhost Port: 25 From: Subject: Mail::Sendmail version 0.79 test To: Result: 250 2.0.0 s8FKMkj7000302 Message accepted for delivery content of $Mail::Sendmail::error: connect to failed (Connection timed out) connect to failed ok 2 MIVKOVIC/Mail-Sendmail-0.79.tar.gz make test -- OK Running make install Prepending /home/scmadmin/.cpan/build/Mail-Sendmail-0.79-MNOVHo/blib/a +rch /home/scmadmin/.cpan/build/Mail-Sendmail-0.79-MNOVHo/blib/lib to +PERL5LIB for 'install' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +!! ERROR: Can't create '/usr/lib/perl5/site_perl/5.8.8/Mail' mkdir /usr/lib/perl5/site_perl/5.8.8/Mail: Permission denied at /usr/l +ib/perl5/5.8.8/ExtUtils/ line 457 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +!! at -e line 1 make: *** [pure_site_install] Error 13 MIVKOVIC/Mail-Sendmail-0.79.tar.gz make install -- NOT OK
Problem running cluster-based commands from perl script
1 direct reply — Read more / Contribute
by eMBR_chi
on Sep 15, 2014 at 15:29

    Hello wise Monks,

    My current project involves some parallel computing and I am working on a cluster. I use 'system' within my Perl script to pass instructions to the command line, which works okay. My problem however is that for commands that require a distribution of the job to other nodes on the cluster the processing returns to my script before such jobs have been completed. I need to be able to make the execution wait till completion before returning and proceeding with the rest of the script. For now I think it waits just for as long as it takes the head node to distribute the job and then returns to the script while the jobs are still being processed on the other nodes.

    My script is something like this

    #usr/bin/perl -w use strict; use warnings; . . . print "File split: STARTED\n"; system "/share/apps/RAMMCAP-2009-1106/qsub-scripts/ + -i $file_in -o read-split_$file_in -n 46"; print "File split: COMPLETED\n"; my $readsplitf = "read-split_$file_in/output.1"; print "rRNA blast: STARTED\n"; system "/share/apps/RAMMCAP-2009-1106/qsub-scripts/qsub_rRNA_hmm_run.p +l -i $readsplitf -o rRNA_hmm_$file_in"; print "rRNA blast: FINISHED\n"; . . .

    Subsequent steps following the initial RAMMCAP command do not get executed appropriately as results do not get available before subsequent steps run. I know there are ways to implement some kind of 'wait' on the cluster, but I do not know how. Any advice would be much appreciated.

    Thank you very much.

How to filter few key value pairs from hash reference
4 direct replies — Read more / Contribute
by jaypal
on Sep 15, 2014 at 12:55

    Hello Perl Monks,

    I need some suggestions in filtering out few key value pairs from a hash reference. Here is a quick example that will probably demonstrate what I am trying to do (you will notice that in my final data structure the array of hashes has one key value pair less as it has been moved outside of array):

    Lets assume I have a following data structure:

    @data = ( { id => '1', name => 'Tom', sex => 'Male', }, { id => '2', name => 'Harry', sex => 'Male', }, { id => '3', name => 'Pam', sex => 'Female', }, { id => '4', name => 'Dick', sex => 'Male', } );

    From this array of hashes, I am interested in creating a hash of array of hashes which will have inner key of sex whose value will be array of hashes.

    My attempt to do this was as follows:

    use strict; use warnings; use Storable qw / dclone /; #my @data = ( ... ) As shown above; my $data2; for my $href ( @data ) { my $copy_ref = dclone($href); my $sex = $copy_ref->{sex}; delete $copy_ref->{sex}; push @{ $data2->{$sex} }, $copy_ref; } use Data::Dumper; print Dumper $data2;

    This was the output:

    $VAR1 = { 'Female' => [ { 'name' => 'Pam', 'id' => '3' } ], 'Male' => [ { 'name' => 'Tom', 'id' => '1' }, { 'name' => 'Harry', 'id' => '2' }, { 'name' => 'Dick', 'id' => '4' } ] };

    I am creating a copy of the hash reference and then manipulating the copy reference and then adding it to new data structure to protect my original data structure. Is there a better way to filter certain key value pairs from a hash reference without having to create a copy reference but protecting the original data structure.

    Since this is only for learning purposes, I am interested in looking at other options even if they come at the cost of readability.

    Looking forward to your wisdom.


issue with DBI and MS SQL SERVER
3 direct replies — Read more / Contribute
by DanBev
on Sep 15, 2014 at 10:04

    Hi monks, I've an issue connecting to MS SQL Server with DBI. I've read all tutorials and configurations of TDS but my code still not works...

    Simply I try my $dbh = DBI->connect(DBI:Sybase:server=xx.xx.xx.xx, "USERNAME", "PASSWORD") ;
    and the error is

    DBI connect('server=xx.xx.xx.xx','USERNAME',...) failed: OpenClient me +ssage: LAYER = (0) ORIGIN = (0) SEVERITY = (78) NUMBER = (41) Server xx.xx.xx.xx, database Message String: Unable to connect: Adaptive Server is unavailable or d +oes not exist at line 19 Cant' connect to database: OpenClient message: LAYER = (0) ORIGIN = (0 +) SEVERITY = (78) NUMBER = (41) Server xx.xx.xx.xx, database Message String: Unable to connect: Adaptive Server is unavailable or d +oes not exist

    My TDS configuration is

    [xx.xx.xx.xx] host = xx.xx.xx.xx port = 1433 tds versione = 7.0

    Instead, trying in shell it works

    root@___# /usr/local/freetds/bin/tsql -S -U USER Password: locale is "it_IT.UTF-8" locale charset is "UTF-8" using default charset "UTF-8" 1>

    Please help me to understand... is missing
1 direct reply — Read more / Contribute
by timpoiko
on Sep 15, 2014 at 09:06
    Hello all Monks. I wanted to take GD:SVG in use, so I wrote man GD::SVG and read it. I found a piece of code, so I took it and tried to run.
    #!/usr/bin/perl use strict; use GD::Simple; GD::Simple->class('GD::SVG'); my $img = GD::Simple->new(500,500); $img->bgcolor('white'); $img->fgcolor('blue'); my $g1 = $img->newGroup('circle in square'); $g1->rectangle(100,100,400,400); $g1->moveTo(250,250); my $g2 = $g1->newGroup('circle and boundary'); $g2->fgcolor('black'); $g2->bgcolor('red'); $g2->ellipse(200,200); print $img->svg;
    However, only outcome from this is:
    $ perl Can't locate auto/GD/ in @INC (@INC contains: /etc/perl /us +r/local/lib/x86_64-linux-gnu/perl/5.20.0 /usr/local/share/perl/5.20.0 + /usr/lib/x86_64-linux-gnu/perl5/5.20 /usr/share/perl5 /usr/lib/x86_6 +4-linux-gnu/perl/5.20 /usr/share/perl/5.20 /usr/local/lib/site_perl . +) at /usr/lib/x86_64-linux-gnu/perl5/5.20/GD/ line 1007.
    Googling does not help at all and I can't found this "" from the CPAN nor Debian's package repositories. So what I can to do to get this example code working?
Need regex to filter out unwanted rows
2 direct replies — Read more / Contribute
by ps2931
on Sep 15, 2014 at 07:29
    Hello monks!

    I'm trying to filter out rows from a large text files based on the criteria -

    The allowed charcters are: aA-zA, underscore(_), colon(:), dot(.), forward slash(/), comma(,), hyphen(-) numbers(0-9) and double quotes (" ").

    Any character other than the characters listed above is invalid. I want to print line which failed the criteria. The sample line from test file is something like:

    2749 "CQWERC20F+XZIAQAAAQjLiDI9sNc=", "1","ds_uid","CWER1Y1mHZIAQAA8di +wRHfuwrM=","2012-10-14 18:41:44.429","2012-10-14 18:41:44.572","1975- +10-10 00:00:00.000","7307 mg rd","","naasik","NK","44026","IN","44063 +59999","","","","DEFAULT","","","AABBCCXX","","Qqwwee<feff>","","qqww","0","YOPANEL","","false","en","","","","","","","",""," +","","","","","","","",""

    The above line is invalid since it has '<' symbol. Can anyone help me?

Multiline Regex replacement in Multiline file
2 direct replies — Read more / Contribute
by akamboj84
on Sep 15, 2014 at 02:28

    Hello Experts, I am kind of stuck in code. I am trying to do multi regex replacement in multiline file. However my scripts is unable to do replacement, I am not able to figure out why? Can someone help me please

    ->>Script Code

    my $def="hashpatterns.txt"; my %dic=(); open(D, $def) || die "can't open definition file:$def\n"; while (<D>) { my ($oldp, $newp) = split /#/; $dic{$oldp}=$newp; } close(D); my $file="input.txt"; open(F, $file) || die "can't open definition file:$file\n"; open(W, '>out.txt') or die "can't write to file:$!\n"; my $line=join "", <F>; my $matchkey=join "|", keys %dic; $matchkey=qr /$matchkey/; $line =~ s%$matchkey%$dic{$matchkey}%g; print W $line; close(F);

    --> hashpatterns.txt

    \s+user\s"[^"]+"\s+password\s"[^"]+"\s+hash2\s+access(\s+console){2}(\ +s+new-password-at-login)?(\s+member\s"(default|engineer|networktest)" +){2}(\s+exit){0,2}#REPLACE1

    \s+user\s[^"]+"\s+password\s[^"]+"\s+hash2\s+access(\s+console){2}(\s+ +new-password-at-login)?(\s+member\s"(default|READ-ONLY)"){2}(\s+exit) +{0,2}#REPLACE2

    \s+user\s"[^"]+"\s+password\s"[^"]+"\s+hash2\s+access(\s+(console|snmp +|li)){3}\s+console(\s+new-password-at-login)?(\s+member\s"(default|LI +|li-prof1)"){2}(\s+exit){0,2}#REPLACE3


    user "testuser1" password "08Cl3V.leJKU/GskqArA0Yp4MFo" hash2 access console console new-password-at-login member "default" member "engineer"

    user "v-test" password "VCp0GjSBK/KiWW.PgkQp7swXVMZ" hash2 access console console new-password-at-login member "default" member "READ-ONLY"

ModPerl2 Mod_rewrite
2 direct replies — Read more / Contribute
by Abhijit10089
on Sep 15, 2014 at 02:06
    Hi Monks, I am working on Unix to Linux Migration for an application and I am no +t able to make the PerlTranshandler work. The application is designed to change the URL inputted by the user i.e +. Html* and redirect it to new one i.e. g.asp?arguments Please find below the code snippet: if ( $r->uri =~ /^\/html\/(.*)$/i ) { $ruri = '/g.asp'; $r->uri($ruri); $r->args("f=$1"); } But the code flow is never moving to g.asp which has 777 properties an +d saved in the DocumentRoot. And when I am printing the r->uri and r->args: URI /g.asp URI /error/ Here i am not able to understand that the code is not going into g.asp + (because i have a print command in the first line itself and that ne +ver gets executed) secondly where does this error message gets popped + up? I have the following configuration on my system: 1. ModPerl2 installed and working fine. 2. Server version: Apache/2.2.12 (Linux/SUSE) Server built: Nov 30 2011 17:54:13 3. Added RewriteEngine On & Allowoverride All in the httpd.conf file. Best Regards, Abhijit Singh Dahiya
How to campare two variable consist line and print difference
3 direct replies — Read more / Contribute
by Mjpaddy
on Sep 15, 2014 at 02:00
    Hi monks

    How to compare two variables which has values a line and print the specific unmatched word by its position and line number

    for example

    $str1 = "But I think the device doesn't allow writes or something and +that's causing this issue."; $str2 = "But I think the device allow writes or something and that's c +ausing this issue.";
    open (FHH, ">error.txt")|| die $!; $str1 = "But I think the device doesn't allow writes or something and +that's causing this issue."; $str2 = "But I think the device allow writes or something and that's c +ausing this issue."; my $line = 0; my $pat = qr/$str2/; while($str1){ $line++; if($str1 =~m/($pat)/igs){ my $pre = $`; } else{ print FHH "Line $line:$pre \'$pat\' is missing from $str2." } }

    In the $str2 dont have "doesn't" as compare to $str1

    So the output will be in other file called error.txt in this format: Line 1:cols 24 'doesn't' is missing from $str2

    I have tried some modules in perl like: File::Compare, Text::Compare, List::Compare but they are not giving the proper answer.

    Which strategy should I choose to get multiple lines store in two variables like a two whole file is store in two different variable and compare 2nd file on 1st and print position of mismatch in other file.

    Please Help!

    Thanks in advance
solving multistep equations
4 direct replies — Read more / Contribute
by Anonymous Monk
on Sep 14, 2014 at 16:16

    i just started learning perl a little while ago, and i have been spending ages trying to write this script to do my algebra homework for me since i am too lazy to do it myself lol. it has to solve multistep equations such as this: 197 = 5(4a + 10) + 4a. and i totally cannot get it to work and it is driving me mad!! i came up with a basic algorithm for how it should work, it takes one argument, the equation, formatted so that it contains no spaces and the variable is called x, so that 197 = 5(4a + 10) + 4a would be 197=5(4x+10)+4x. then it splits the equation into two halves around the equals sign so that it becomes my $left = 197 and my $right = 5(4x+10)+4x. and that is where i get stuck, i want it to substring 5(4x+10) out of $right and then distribute it, but i cant figure out how to get it to do that. does anyone have any idea how i would write this script?? thanks in advance

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
  • 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?

    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 having an uproarious good time at the Monastery: (6)
    As of 2014-09-17 01:54 GMT
    Find Nodes?
      Voting Booth?

      How do you remember the number of days in each month?

      Results (56 votes), past polls