Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
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
Dots appearing when opening a directory
4 direct replies — Read more / Contribute
by The_Last_Monk
on Mar 06, 2015 at 11:49
    Fellow Shaolin monks, So i am trying to print the names of a file in a directory, here is my code:
    $dir = path to the directory opendir (DIR,$dir) or die "dead"; while(my $text = readdir DIR) { print "$file\n" }
    The directory has a text file in it, but once it opens the directory, the output is this:
    . .. File.txt
    What would be a solution to get rid of those dots? Is there a regex that should be used here so that it only prints the files starting with a word or a number? If so, what would be the regex?
alarm in windows, alternative?
1 direct reply — Read more / Contribute
by sniperd
on Mar 06, 2015 at 10:46
    Hello Monks! I need to make a system call in Windows and sometimes it times out and I'm trying to use alarm, however it looks like that doesn't work in windows (Activestate). Here is my code:
    use warnings; use strict; main (); sub main { eval { local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required alarm 2; my $data = `ping 127.0.0.1 -t`; alarm 0; }; if ($@) { die unless $@ eq "alarm\n"; # propagate unexpected errors } else { print "got here\n"; } }
    To keep it simple I just chucked in there ping -t for testing. The real code is calling phantomjs to hit a website and the .exe just blows up sometimes, basically getting stuck and I the script will just sit there. Is there some simple command I can use to call command line on windows and have it just die (or continue) if a certain number of seconds have passed? Thank you in advance.
[perl6] Complex Attribute Validation and/or Triggers
No replies — Read more | Post response
by duelafn
on Mar 06, 2015 at 10:03

    Are there ways in perl6 to write more complex attribute validators and/or before/after/around modifiers on attribute access? I can't seem to find anything that would let me, say, implement an interval class with a lower and upper bound, or call some code every time an attribute is changed. Did I miss it somewhere?

    class Interval { has Real $.lb is rw = die 'Lower bound is required'; has Real $.ub is rw = die 'Upper bound is required'; } ### Example which does not work: # class Interval { # has Real $.lb where * <= $.ub is rw = die 'Lower bound is requir +ed'; # has Real $.ub where * >= $.lb is rw = die 'Upper bound is requir +ed'; # after lb { say "Lower bound was changed!" if @_ } # } my $i = Interval.new( lb => 1, ub => 3); say $i.perl; $i.lb = 0; # should print message say $i.perl; $i.lb = 6; # should blow up! say $i.perl;

    Thanks,
        Dean

extract pattern from large blob of text
2 direct replies — Read more / Contribute
by littleperl
on Mar 06, 2015 at 09:34

    I would like to extract the value for 'var nonce' from the following text blob (string)

    "... var realm = "heaven"; var nonce = "186248:541534:a67930c5cffeb25a5485e6b4f32570e2"; var qop = "auth"; var uri = "/earth.lp"; ...."

    Currently my code looks a like:

    my $req = HTTP::Request->new(GET => $server_endpoint); my $resp = $ua->request($req); my $message = $resp->decoded_content; $message =~ m/nonce/i; print " starts: @- \n ends: @+ \n";

    To get the begin and end of the nonce keyword (and next I will need to extract the value (186248:541534:a67930c5cffeb25a5485e6b4f32570e2) ).

    Problem is I do not get the start or end position back on this string? what am I missing here?

numeric hash keys
3 direct replies — Read more / Contribute
by AWallBuilder
on Mar 06, 2015 at 08:38
    Dear all,

    I've done some reading and it sounds like it is possible to have numeric hash keys and perl is supposed to figure out when numbers should act as numbers and when they should act as strings. However, I am getting an error message that indicates to me that it thinks my numbers are strings. Any advice appreciated. Thanks

    use strict; use warnings; use Data::Dumper; ############################ my $inGNPS="MsRtLib_all.tab"; open(IN,$inGNPS); my %HoPMAnnot; my %HoPMrtAnnot; while(my $line=<IN>){ chomp $line; my ($ParMass,$RT,$annot)=split(/\t/,$line); $HoPMAnnot{$ParMass}=$annot; $HoPMrtAnnot{$ParMass}{$RT}=$annot; } close(IN); print Dumper (%HoPMAnnot); ###### my $inATdat="consensus_features_extracted.csv"; open(IN,$inATdat); while(my $line=<IN>){ chomp $line; next if($line =~/^,/); # header starts with comma my ($feat_numb,$mz_min,$mz_max,$rt_min,$rt_max,$quality,$inten +sity)=split(/,/,$line); foreach my $PM (keys %HoPMAnnot){ if ($PM <=$mz_max && $PM >=$mz_min){ print join("\t",$line,$HoPMAnnot{$PM})."\n"; } } } close(IN);

    data dumper output

    $VAR1 = '272.043'; $VAR2 = 'N/A'; $VAR3 = '272.188'; $VAR4 = 'N/A'; $VAR5 = '838.844'; $VAR6 = 'Sodium_Formate'; $VAR7 = '601.393'; $VAR8 = 'N/A';

    error

    Argument "parent mass" isn't numeric in int at checkTheoFeat4GNPSannot.pl line 25, <IN> line 1034.

Export global variables not working correctly
4 direct replies — Read more / Contribute
by Anonymous Monk
on Mar 06, 2015 at 00:45

    I'm not good at English, but I believe that when you look at codes you will understand what I meant

    package AAA; use 5.20.1; use lib ('.'); use BBB; use parent('Exporter'); our @EXPORT=('$b','$msg'); our $bbb=BBB->new(); return 1; package BBB; use 5.20.1; use lib '.'; use AAA; sub new{ my $class=shift; my $self=bless {},$class; return $self; } sub hello{ my $self=shift; say 'hello'; } sub hello2{ my $self=shift; $b->hello(); } return 1; package main; use 5.20.1; use lib ('.'); use AAA; $bbb->hello2();

    $ perl test.pl Global symbol "$bbb" requires explicit package name at test.pl line 5. Execution of test.pl aborted due to compilation errors.

unique words
4 direct replies — Read more / Contribute
by rasy
on Mar 05, 2015 at 23:26
    how to read text from the input (one word per line) and display the unique words in the input
    #!/usr/bin/perl use strict; use warnings; my $cnt=0; my $input=<>; my $unique = ''; if ($unique == $input){ $cnt++; } else if($cnt<1){ print "$unique\n"; }
Perl-Tk MouseWheel support for Scale
1 direct reply — Read more / Contribute
by perltux
on Mar 05, 2015 at 19:10
    Hi, I'm trying to get MouseWheel support for the TK::Scale widget in Windows working but I don't really understand how. The few examples I've found on the Net refer to other widgets and don't seem to work for me when trying to apply them to a Scale widget.

    Here is my test code (mouse wheel is not working with this code on Windows):

    #!/usr/bin/perl use strict; use warnings; use Tk; my $var=0; my $incr=1; my $mw=MainWindow->new(); my $scale=$mw->Scale( -variable => \$var, -to => -63, -from => 63, -resolution => $incr, -tickinterval => 0, -label => '', -width => 10, -length => 150, -sliderlength => 20, -relief => 'flat', -borderwidth => 0, -highlightthickness => 0, -showvalue => 0, -orient => 'vertical' )->pack(-padx=>50); $scale->bind('<Button-4>' => sub{$scale->set($var+$incr)}); $scale->bind('<Button-5>' => sub{$scale->set($var-$incr)}); $scale->bind('<MouseWheel>' => [ sub { $_[0]->set($var+$incr*-($_[1] / + 120))}, Ev('D') ] ); MainLoop;

    How do I get this working on Windows? (on Linux it works fine, the binding is sooo much easier)
Advice on best practices
9 direct replies — Read more / Contribute
by goldcougar
on Mar 05, 2015 at 11:39

    Hello. I've coded in perl in the past, but its been a bit since I've coded regularly in perl. I've recently started working with a consultant that decide to use perl for a project. I took a look at his code, and I was immediately surprised by what I found. Given that I am not a professional perl programmer, I wanted to check with a group of professionals to confirm my suspicions, and see if this is being done the wrong way.

    The overall task of the code is to take files submitted by a customer which is in various formats, and convert them into a format to be imported into a database (PostgreSQL to be specific). The code is executed via Windows command line. I can't post entire code, since its the customers product, but I will post some snippets here to protect the innocent.

    Reusing code- The code consists of 5 different perl files all in the same system directory. For sub functions, he uses code like below. However, there is no reuse of the subs across the files. So, for example sub doXYZ exists in all 5 of the perl files, duplicated exactly. Shouldn't he be creating a module for the shared sub functions and including it, so the code for these is shared?

    sub doXYZ{ ... }

    File IO- The code involves some moving, renaming, copying, deleting etc of files and folders. Perl has great built-in File IO libraries from my experience. However, he builds a string of the windows command he wants to run, and runs it via System. I have never seen anyone do this before. Am I crazy, or is this the wrong way?

    #Delete Example $theCmd="del "."\"".$SourcePath."\" >> ".$LOG_DIR."\\".$F_DUMMY." 2> +&1"; $returnCode=system $theCmd; #Delete Example $theCmd="copy "."\"".$SourcePath."\\".$SourcePathFileName."\""." "."\" +".$DestPath."\\".$DestPahtFileName."\""." >> ".$LOG_DIR."\\".$F_DUMMY +." 2>&1"; $returnCode=system $theCmd;

    Monitoring for new files- The code needs to check when a new file is added, and then start processing it. The code uses an infinite loop to monitor for files. Shouldn't he be using one of the Scheduling CRON modules in Perl, or scheduling it via the windows system task scheduler? I pointed this one out already, and the developer said this was just the easy way to do it and he wasn't sure how the customer wanted to schedule it. However, I can't see why anyone would ever do it this way, and not just do it the right way the first time?

    while(1==1){ #code here for processing files sleep(60); }

    Connecting to SQL Database- The code needs to have the output file it builds to be imported into the PostgreSQL database. It does this by writing to a hard coded file with a list of file names, return delimited, that is has finished. Then there is a PostgreSQL scheduled job looking for that file, importing it into a table. It then has the filenames, and then goes out again and issues the PostgreSQL COPY command to copy that file into the appropriate final table. My experience here is that passing parameters to another system (from Perl to PostgreSQL) should never be done by writing to a shared file that the other program reads. This always causes problems with concurrency, and it just bad practice. In addition, Perl has many connectors to databases available, like to PostgreSQL, and it can make its own connection to PostgreSQL and issue the COPY command directly. I've used DBI in the past to connect to other SQL databases and it was simple and easy.

    #FilesToPostgres.txt looks like this: processedFileName1.csv processedFielName1.csv #PostgreSQL then has a scheduler running which reads that file into a +table, and then loops its rows and runs subsequent commands like COPY tableName FROM /path/to/processedFileName1.csv

    So, I was hoping to get some experts to let me know if what this programmer is doing is acceptable, or if I'm correct that we have some issues here that we need to fix, and this programmer doesn't seem to understand the basics of perl (which by the way was his language of choice, as he could have used whatever he wanted). I hate seeing new projects start with bad code, so I want to get it right from the start, but this guy considers himself a senior developer and I don't want to offend him, unless I know for sure there are some issues

    Thanks in advance!

Math::Int128 issue on OS/X
1 direct reply — Read more / Contribute
by salva
on Mar 05, 2015 at 11:37
    I have this bug report for Math::Int128.

    The reporter is trying to use Math::Int128 C API but his module dies because of some unresolved symbol. It seems to me that the issue is related to the compilation/linking of the client module.

    I don't have a Mac and my knowledge of OS/X is quite limited, so I would really appreciate if somebody else could take a look at that.


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 cooling their heels in the Monastery: (11)
    As of 2015-03-06 17:13 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

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









      Results (171 votes), past polls