Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
The stupid question is the question not asked

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
sorting an associated array
2 direct replies — Read more / Contribute
by torres09
on Apr 23, 2014 at 15:22

    hey monks

    I have an associative array . So how can i Sort an associative array contents based on its key value...and print the sorted array

    thanks in advance

use of diamond operator for multiple files
3 direct replies — Read more / Contribute
by aeqr
on Apr 23, 2014 at 13:42
    Hello, I would like to know if there is a shortest way to do this:
    my @seq1; my @seq2; load_seqs(\@seq1,\@seq2); sub load_seqs{ open(my $fh, "<", "q1.fa") or die "cannot open sequence file: $!"; while(my $line=<$fh>){ if($line =~ m/^(?!\>).*\n$/){push (@{$_[0]},$line)}; } open(my $fh, "<", "q2.fa") or die "cannot open sequence file: $!"; while(my $line=<$fh>){ if($line =~ m/^(?!\>).*\n$/){push (@{$_[1]},$line)}; } }
    I have tried the diamond operator but I am not sure on how to use it in that case. Thanks!
Results depending on evaluation order
2 direct replies — Read more / Contribute
by fishy
on Apr 23, 2014 at 12:30
    Hi Monks,

    I was trying the code from Okay! What!?!?!?
    my $value1 = 14.4; print "value1 = $value1\n"; my $value2 = 10 + 14.4 - 10; print "value2 = $value2\n"; if ( $value1 == $value2 ) { print "value 1 equals value 2\n"; } else { print "value 1 does not equal value2\n"; }
    value1 = 14.4 value2 = 14.4 value 1 does not equal value2

    and from My floating point comparison does not work. Why ?
    $number = 1.80; $premium = $number * ( 1 + 10/100 ); $expected = 1.98; print "Number 1 : $premium \n"; print "Number 2 : $expected \n"; print "Not" if $expected != $premium; print "Equal !! ";
    Number 1 : 1.98 Number 2 : 1.98 NotEqual !!

    when I came up with the following behavior (tested on Perl 5.10.1 and on 5.18.2):

    Swapping operands on the first sample code:
    my $value2 = 10 + 14.4 - 10; # wrong evaluated
    my $value2 = 14.4 + 10 - 10; # wrong evaluated
    my $value2 = 10 - 10 + 14.4; # right evaluated

    Using eval:
    my $value2 = eval (10 + 14.4 - 10); # right evaluated

    Using eval on the second sample code:
    $number = 1.80; $premium = eval ($number * ( 1 + 10/100 )); $expected = 1.98; print "Number 1 : $premium \n"; print "Number 2 : $expected \n"; print "Not" if $expected != $premium; print "Equal !! ";
    Number 1 : 1.98 Number 2 : 1.98 Equal !!

    So, I got odd results depending on the order of the operands and right results on evaluating.

    How can that be?
Package declaration in code block
2 direct replies — Read more / Contribute
by McA
on Apr 23, 2014 at 11:46

    Hi all,

    more and more I see code examples of the following form:

    { package Modul; use This; use That; ... }

    What is the difference to the form of

    package Modul1; use This; use That;

    without being wrapped into a block?

    Can someone enlight me, please?

    Best regards

Dynamically adding methods to Moo class
1 direct reply — Read more / Contribute
by McA
on Apr 23, 2014 at 11:24

    Hi all,

    I'm writing a Moo based class with several methods. I'm pretty new to Moo. While writing the methods I saw that the bodies of the methods are almost the same. Is there a recommended way to "generate" these methods with Moo? Is there a kind of method generation facility? Or do I have to tweak with the symbol table?

    Thank you in advance.

    Best regards

Replacing DBI->do()
5 direct replies — Read more / Contribute
by jms53
on Apr 23, 2014 at 09:44
    I have a small DB module and I'm not sure wether or not I'm using DBI->do() correctly.

    This is the code I have:

    my ($first, $last) = split (' ', $user); my $id = $dbh->do(q{ INSERT INTO person SET id=?, first_name=?, las +t_name=?; }, values {id=>'NULL', first_name=> $first, last_ +name=> $last}); $dbh->disconnect;

    When I test this, perl complains that I am not using a hash for the values. Should I be using a different DBI function? I have tried many combinations, and the documentation doesn't show an example.

    Thank you!
    J -
Passing a File Descriptor to a New Process on Windows
1 direct reply — Read more / Contribute
by hardburn
on Apr 23, 2014 at 09:28

    I have a script that opens a filehandle and then calls fork() and exec() to run a separate program for doing the reading. The trouble is that by default, Perl sets the close-on-exec flags on new filehandles. This can be changed with Fcntl:

    use Fcntl; my $flags = fcntl $in, F_GETFD, 0 or die "fcntl F_GETFD: $!"; fcntl $in, F_SETFD, $flags & ~FD_CLOEXEC or die "fcntl F_SETFD: $!";

    This doesn't work on Strawberry Perl on Windows, though, due to the F_GETFD macro being missing.

    At Sebastian Riedel's suggestion, I tried local $^F = 10; instead. This makes Perl consider up to 10 open filehandles to be "system filehandles" which do not get the close-on-exec flag set (when using this, remember that you'll already have three filehandles open: STDIN, STDOUT, and STDERR).

    This worked on Linux and Cygwin, but Strawberry Perl still failed to reopen the filedescriptor with "Bad file descriptor". This is the error you usually get when the close-on-exec flag is still set.

    Strawberry Perl is built with threads:

    This is perl 5, version 18, subversion 1 (v5.18.1) built for MSWin32-x +64-multi-thread

    Since fork() would be emulated with threads in this case, perhaps that accounts for the difference with Cygwin?

    Full example code below. The first one ( is passed a file to open and then forks off to exec the second one (

    #!/usr/bin/perl use v5.14; use warnings; #use Fcntl; my $FILE = shift or die "Need file to read\n"; # Don't set close-on-exec flag when we open a file handle local $^F = 10; say "Opening file"; open( my $in, '<', $FILE ) or die "Can't open '$FILE': $!\n"; # Clear the close-on-exec flag #my $flags = fcntl $in, F_GETFD, 0 or die "fcntl F_GETFD: $!"; #fcntl $in, F_SETFD, $flags & ~FD_CLOEXEC or die "fcntl F_SETFD: $!"; $SIG{CHLD} = 'IGNORE'; my $child_pid = fork(); if( $child_pid ) { # Parent while(1) { sleep 10 } } else { # Child say "Forking child process"; my $fd = fileno( $in ); exec( 'perl', './', $fd ) or die "Could not exec: $!\n"; }

    #!/usr/bin/perl use v5.14; use warnings; my $FD = shift or die "Need file descriptor\n"; open( my $in, '<&', $FD ) or die "Could not open file descriptor '$FD' +: $!\n"; while(<$in>) { chomp; say "Got in child: $_"; } close $in;

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Find files recursively and get their attributes
1 direct reply — Read more / Contribute
on Apr 23, 2014 at 08:15
    Hi Monks,

    I have a requirement to find zip,tar,jar,tar.gz etc.. files recursively in a directory and its sub directories and extract them.After extracting, i need to find a specific file and report its name and size into an xml file.

    i am able to write the code to dump the file's size and name to an xml file but i am not able to extract the files recursively and get specified files i need to report in the xml file.

    If the file found is a .swtag file,I am storing its size and its contents in a hashreference for later use.

    Here is my code using File::Find module.

    my $min_depth = 0; find( { wanted => \&wanted, }, @dirs); sub wanted { my $depth = $File::Find::dir =~ tr[/][]; return if $depth < $min_depth; if ( $File::Find::name =~ m/.zip\z$|file.*?\z$|.bak\z$|.jar\z$|.wa +r\z$/g ) { print "I am extracting zip file : $File::Find::name\n"; system("unzip -o $File::Find::name"); } if ( $File::Find::name =~ m/.tar.gz\z$/ ) { &execute_command("tar -xzvf $File::Find::name"); } if ( $File::Find::name =~ m/.iso\z$/ ) { system("mkdir /tmp/mnt"); system("mount -o loop $File::Find::name /tmp/mnt"); my @dirs = ( '/tmp/mnt' ); find ( { wanted => \&wanted, },@dirs ); system("unmount /tmp/mnt"); system("rm -rf /tmp/mnt"); } if ( $File::Find::name =~ m/.SYS$|.sys$|.sys2$|.SYS2$|.cmptag$|.sw +tag$|.swidtag$|.tag$|.fxtag$/ ) { print "$File::Find::name\n"; my $fsize = stat($File::Find::name); $fileparamhash->{$File::Find::name}->{Size} = $fsize->size; if ( $File::Find::name =~ m/.swtag$/ ) { my $parser = XML::LibXML->new; my $doc = $parser->parse_file("$File::Find::name"); my $FileDescription; my $versionname; my @filedesc = $doc->getElementsByTagName("ProductName"); foreach ( @filedesc ) { $FileDescription = $_->textContent; } my @versions = $doc->getElementsByTagName("ProductVersion"); foreach ( @versions) { $versionname = $_->textContent; } $fileparamhash->{$File::Find::name}->{FileDescription} = $File +Description; $fileparamhash->{$File::Find::name}->{FileVersion} = $versionn +ame; } } } }

    can anyone look into my buggy code and help is urgent.

    Many Thanks, Sriram

array range positions
2 direct replies — Read more / Contribute
by juanito23
on Apr 23, 2014 at 07:06

    Hi team, I have a array with some fields like this

    @csv_out = "m2000_id","2014-04-23 11:48:30","Ne is Disconnected",14742 +,,"NULL",6007,"N/A","FailedtosetSSLconnectionmode","Media +tionService0206",,405171,"874986",0,1,11,1,1,1,0,1,-",13890 +20188,"1389020188",1415599200,0,0,0,0,1,301,0,1389020188,0,0,0

    So now I need to put double quotes around the fields that have characters (already done), and as well in the first 10 fileds. I tried something like

    elsif ($_ =~ m/[a-zA-Z]/ || $csv_out[0 .. 10]){ print "$_\n"; $_=~ s/,/ /g; print WRITEFILE '"'."$_".'"' . "," ;

    as you can see the first clause in the elsif is ok, but the second one cannot make it work. Any simple way to specify that I need double quotes on all caracther fields and the first 10 fields? Since the first 10 contain fields with numerics as well.

traverse through passwd file, and split fields to extract UID/GID
3 direct replies — Read more / Contribute
by intoperl
on Apr 23, 2014 at 06:59
    <> Hello, i have a code below which intends to populate an array using File Handle, and File handle contains the linux passwd file info separated by ":". I am able to succeed till this point. Now, i further need to split each passwd line and extract UID and GID for each user. i am able to achieve this only for the first line of passwd file, and not for rest others. Below is the code and the passwd file i am using, please guide, where am i wrong.

    $HAN = "handle"; $FILE = "passwd.txt"; open("$HAN", "$FILE")||die "Error opening file: $!\n"; while(<$HAN>) { @arr = <$HAN>; } foreach(@arr) { @IDINFO = split(/:/,"$arr[$_]"); print "UID is: @IDINFO[2] \n"; print "GID is: @IDINFO[3] \n"; }
    Passwd File, i am using: xerox:x:10532:151:simmy xrsa:x:222:507:SystemExpert hintest:x:32381:1873:xThin xtactid:x:65446:266:eVenues ricpay:x:30197:1168:Emeral way:x:10010:111:Expressway t061gs:x:80061:3415:Gurav e456988:x:38975:3477:center zrmda01:x:137001:137:Domino lmda02:x:137002:137:Domino TYMFTP:x:51331:2130:Customer ddda03:x:137003:137:Domino ddda04:x:137004:137:Domino dda05:x:137005:137:Domino dda06:x:137006:137:Domino ddm01:x:88085:137:Notes
    output: (its only taking first line and printing multiple times) UID is: 222 GID is: 507 UID is: 222 GID is: 507 UID is: 222 GID is: 507 UID is: 222 GID is: 507 UID is: 222 GID is: 507 UID is: 222 GID is: 507 UID is: 222 GID is: 507 UID is: 222 GID is: 507 UID is: 222 GID is: 507 UID is: 222 GID is: 507 UID is: 222 GID is: 507 UID is: 222 GID is: 507 UID is: 222 GID is: 507 UID is: 222 GID is: 507 UID is: 222 GID is: 507

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 exploiting the Monastery: (12)
    As of 2014-04-23 20:07 GMT
    Find Nodes?
      Voting Booth?

      April first is:

      Results (554 votes), past polls