Donations gladly accepted
If you're new here please read PerlMonks FAQ and Create a new user.
Want Mega XP? Prepare to have your hopes dashed, join in on the: poll ideas quest 2012 (Don't worry; you've got plenty of time.)
|
New Questions
|
Getting values from server
3 direct replies — Read more / Contribute
|
by DaveMonk
on May 21, 2012 at 12:33
|
|
|
Hi monks,
I have two simple scripts, a TCP server and a TCP client. My server sends the client a random word (e.g Hello) upon EVERY CONNECTION...
So my client has a for loop that connects to the server 10 times and requests a new word. After the seventh connection, everything stops working, and I get an error:
"Use of uninitialized value $word in concatenation (.)"
Here is my code:
[...]
for (my $i = 0; $i < 10; $i++) {
$socket->recv(my $word, 1024);
print "Your new word is: $word\n";
}
[...]
I am using warnings and strict in my code.
Do I need the IO::Socket::INET in the for loop, or, whats happening? :(
NOTE: $socket is also declared in the first scope of code
|
Bid data but need fast response time
3 direct replies — Read more / Contribute
|
by Anonymous Monk
on May 21, 2012 at 09:36
|
|
|
Dear Perl Monks,
I have a script which is fairly simple, but requires a huge amount of data from disk. I want to be able to run it from a web script, but the data load time is enormous (~30 seconds).
I see two solutions:
1. Use something like mmap to persist the data between calls to perl. (I am not sure, but I think this may happen automatically due to use of page cache. I am running Linux, btw). I thought I *might* need a super-simple holder process that holds the data in memory (And does nothing more.)
2. Use a client-server scheme. I like this less because of possible issues like memory leaks. Ideally, it would be set up so that the "user" enters a line via telnet, and the client reads back a one-line response. (Yes, I'll firewall the ports for safety and validate inputs.) I saw Net::Server and Daemon modules on CPAN. Is either preferred?
Ideally, I would like to be able to run the process on each request (less likely to have memory leaks, etc).
Any wisdom on which way to go would be appreciated,
Padewan
|
Finding specific alphanumeric IDs from the string
1 direct reply — Read more / Contribute
|
by Anonymous Monk
on May 21, 2012 at 02:59
|
|
|
Hello everyone,
I am writing a regression tool and require to match error in written ids within the strings. Following instances would make it clearer:
Required syntax is comma separated ids.
Ids should be expected in any form like this:
123
123456,34567889
12345,CWG123456,1234
(i.e. a numeric string with more than 3 digits or alphanumeric string starting with CWG only)
Classes of Errors observed by me in the string for pattern matching can be of following scenarios:
1) "Blah Blah 10-20 m can be taken into consideration 1234556"
2) 123,30,40
3) http://www.takeithere.com/123456789/fig1,987643,34467889
According to my pattern it removes all the metacharacters found in the coming string and joins 10-20 into 1020 which is more than 3 digits and considers this to be an id. Can you please provide me with an idea to screen Ids in required format only otherwise display error specifically for the ones which do not have proper syntax.
|
spreadsheet module
1 direct reply — Read more / Contribute
|
by trickyq
on May 21, 2012 at 00:02
|
|
|
I am an old perl user. Have been out of the loop for ten years or so. SO I apologize in advance.
I am trying to use the spreadsheet xlsx module to extract two full columns of data from a spreadsheet for line by lime comparison. I can write the code for the actual comparison easily. I have no idea how to use the module to get the columns from the spreadsheet. can anyone point me to some good docs for this? or give me some quick pointers on use of the module.
thanks
|
syntax error near unexpected token `)'
3 direct replies — Read more / Contribute
|
by vit
on May 20, 2012 at 12:55
|
|
|
Dear Monks,
I want to match with optional "'" and optional subsequent word
cat file.txt | perl -ne '{chomp; ($a, $b) = split/\s+/; $s = "$a $b";
+ next if($s !~ /^\w+\'?\w+?\s+\w+\'?\w+?$/); print "$_\n"}' | wc -l
I am getting
bash: syntax error near unexpected token `)'
Is it something related to sygwin or activesatate Perl I use? Or I am missing something?
|
[Resolved]Email::Mime 1 email + multiple attachments
1 direct reply — Read more / Contribute
|
by kazak
on May 20, 2012 at 12:24
|
|
|
Hi, to all.
I'm trying to add multiple attachments with Email::Mime to 1 email. Script executed by schedule and checks MySQL DB in order to get recipients list, attachment list, etc. I managed to implement my idea but not like I wanted. (For each recipient script compiles his own e-mail and attaches 1 file, in result 1 recipient will not receive 1 e-mail with 3 attachments, but 3 e-mails with 1 attachment in each e-mail.) The main problem for me is: If I'm trying to access "@parts" outside of "foreach loop", @parts are empty, so I definitely doing something wrong, but I can't find where, thanks in advance.
Here is the piece of code and attachment list format:
Attachment list may look like this: summary.csv,weekly.csv,john.csv
my @files = split /,/,$Attachment;
foreach (@files) {
chomp;
my $f_name = $_;
$f_name =~ s/\/root\/Helpers/reports\///;
print "FILENAME:$f_name\n";
my @parts = (
Email::MIME->create(
attributes => {
filename => "$f_name",
content_type => "application/x-7z-compressed",
disposition => "attachment",
encoding => "base64",
name => "$f_name",
},
body => io( $_ )->all,
),
);
$email = Email::MIME->create(
header_str => [ From => 'reporter@example.com' ],
parts => [ @parts ],
header_str => [ To => $To ],
header_str => [ Subject => $Report_name ],
);
$email->filname_set($f_name);
$email->name_set($f_name);
my $eml = $email->as_string;
}
|
Database abstraction including data types
2 direct replies — Read more / Contribute
|
by mbethke
on May 20, 2012 at 12:22
|
|
|
There's heaps of database abstraction modules in Perl but it seems that while abstraction always means "You don't have to deal with messy C interfaces", and sometimes "You can write SQL in Perl", it hardly ever means "we'll take care of data type idiosyncrasies for you". I need something like that.
My problem (briefly mentioned in my announcement of Ashafix) is this: I've used a couple of DBMS layers but all programs have been tied to one particular DBMS. Now I want to write one that runs unchanged on at least PostgreSQL and MySQL and my first problem is the handling of booleans. Pg uses an own type that returns 't' or 'f' while MySQL by convention uses TINYINT that is set to zero or non-zero. Currently I'm using DBIx::Simple and I'd prefer to keep it fairly slim. My ideas so far:
- Define an ENUM in MySQL to emulate the Pg behavior. Doesn't work because other software is using the database too and would get very confused.
- Use DBIx::Class, write my own InflateColumn::Bool and a primitive Bool class that uses a tied hash or something to give the usual zero-or-nonzero-scalar behavior when used in boolean context.
Disadvantage: that's about as fat as it gets.
- Subclass DBIx::Simple, feed it a simple column=>type mapping for each table I'll use and make all retrieval methods use a hash()/hashes() method internally so I can do the mapping myself.
Disadvantage: smells inefficient (although it's probably still much faster than DBIx::Class), may be reinventing the wheel.
Have I by any chance overlooked something on CPAN that would let me do this in a simpler way?
|
Creating new database handles with mod_perl
2 direct replies — Read more / Contribute
|
by lschult2
on May 19, 2012 at 20:01
|
|
|
Hello,
I've run into an issue when stress testing mod_perl that the database connections are going away. I suspect that processes are sharing database connections, causing the issue.
But I've followed all instructions for Apache::DBI, and can't figure this out.
I'm making the connections in the child process and not in startup.pl. But when I examine the $dbh returned by each child from the DBI->connnect, the address is the same for every httpd process.
Firstly, if this is working properly and reconnecting for each process, should the address returned by DBI->connect be different for each child process? I've assumed so, but as far as I can tell the core C code in DBI (dbih_setup_handle) is managing this and is returning the same address. So maybe I'm not understanding what it means to reconnect in the child.
Am I reconnecting properly if the $dbh handles are the same?
|
ithreads or fork() what you recommend?
4 direct replies — Read more / Contribute
|
by pawan68923
on May 18, 2012 at 15:52
|
|
|
Hi,
I have to modify and use parallel processing for a large and complex system which is implemented in Perl. Each child process or threads are supposed to use memory around 1 GB and will run for around 1-3 hours on Solaris Server. Total parent's execution time would be around 12-14 hours.
My question is - what is your recommendation - use of ithreads or multi processing using fork(). 100s of processes or threads need to be created with a concurrency limit of around 15 at a time to loop over list of 130-150 jobs. Considering amount of memory it is going to use and execution duration, which approach you would suggest? Relibility of the overall processing is important since it will run in production environment.
Thanks!
Best regards,
Pawan
|
initialize all values in a hash
4 direct replies — Read more / Contribute
|
by AWallBuilder
on May 18, 2012 at 06:11
|
|
|
I want to perform a seemingly simple task, but am confused. I would like to initialize all values in my hash to equal 60.
my $maxBits=60; # hash reference - gave error message below
my %maxBits=60; # wrong
%maxBits=map {$_=>60} ; #?
Can't use string ("60") as a HASH ref while "strict refs" in use at fl
+agginscript_Flag1.pl line 58, <IN> line 1.
here is a larger chunk of the code so you can see the context
############### read in blast table and parse #######
my $in_blast_tab=$ARGV[0];
open(IN,$in_blast_tab) or die "cannot open $in_blast_tab\n";
my $HoFlg1={}; my $HoFlg2={};
my $maxBits=60;
#%maxBits=map {$_=>60} ; # or intialize all values to be 60 using map
print "maxBits after intialization\n";
print Dumper($maxBits);
while (my $line=<IN>) {
next if ($line =~ /^#/);
next unless ($line =~ /\S/);
chomp $line;
my ($Query_id,$strand,$Subj_id,$Perc_iden,$align_len,$num_mm,$
+gap,$q_start,$q_end,$s_start,$s_end,$e_value,$bit_score)=split("\t",$
+line); # extra field of strand
next if ($bit_score <60);
if ($bit_score > $maxBits->{$Query_id}){
$maxBits->{$Query_id}=$bit_score;
}
my ($Flag1, $Flag2 ) = &Flag( $Subj_id, \%proph_prots, \%euk_p
+rots, \%vir_prots );
$HoFlg1->{$Query_id}->{$Flag1}->{$bit_score}++;
$HoFlg2->{$Query_id}->{$Flag2}->{$bit_score}++;
print join("\t",$Query_id,$Subj_id,"bit",$bit_score,"F1",$Flag
+1,"F2",$Flag2,"maxBits{Query}",$maxBits->{$Query_id})."\n";
}
#print "Hash HoFlg1\n";
#print Dumper($HoFlg1)."\n";
#print "Hash maxBits\n";
#print Dumper(%maxBits)."\n";
sub Flag {
my ( $Subj_id, $proph_prots, $euk_prots, $vir_prots ) = @_;
return "Proph", "Phage" if exists $$proph_prots{$Subj_id};
return "Euk", "Euk" if exists $$euk_prots{$Subj_id};
return "Vir", "Phage" if exists $$vir_prots{$Subj_id};
return "Bact", "Bact";
} ## end sub Flag
my $bits_perc=$ARGV[1]; # consider hits within this percent of top hit
my $outfile="$in_blast_tab.$bits_perc.FlagTab";
open(OUT,">",$outfile);
print OUT "# this file was generate by $0 on".localtime(time)."\n";
print OUT "# these are the counts of hits for each Flag that are withi
+n $bits_perc of the top bit_score\n";
print OUT "#".join("\t",qw(Query count_Proph count_Euk count_Vir count
+_Bact))."\n";
my @flag_list2=("Phage","Euk","Bact");
my @flag_list1=("Proph","Euk","Vir","Bact");
foreach my $q (keys %{$HoFlg1}){
print OUT "$q\t";
for my $flag (@flag_list1){
my $count={};
if (! exists $HoFlg1->{$q}->{$flag}){
$count->{$flag}=0;
}
else {
for my $b (keys %{$HoFlg1->{$q}->{$flag}}){
if ($b > $bits_perc*$maxBits->{$q}){
$count->{$flag} += $HoFlg1->{$q}->{$flag}->{$b
+};
}
}
}
if (! defined $count->{$flag}){
$count->{$flag}=0;
}
print OUT "$count->{$flag}\t";
}
print OUT "\n";
}
|
Read data from csv file to a data structure format
2 direct replies — Read more / Contribute
|
by Anonymous Monk
on May 18, 2012 at 02:59
|
|
|
Product Name,Product Code,Company1,Company2,Company3
Test One,C001,Y,N,N
Test Two,C002,N,N,Y
Test Three,C003,Y,Y,N
and I am looking for an output in the below format
{
Company1 => {C001 => Y, C002 => N, C003 => Y},
Company2 => {C001 => N, C002 => N, C003 => Y},
Company3 => {C001 => Y, C002 => Y, C003 => N}
}
I tried using Text::CSV_XS module, but not able form a structure as mentioned above
use Text::CSV_XS;
my $csv = Text::CSV_XS->new();
open my $FH, "<", "test.csv";
while (<$FH>) {
$csv->parse($_);
my @fields = $csv->fields;
Is there any CPAN module available to get this structure
Can you please help on this.
Thanks
|
Links between Mason components?
2 direct replies — Read more / Contribute
|
by tye
on May 17, 2012 at 13:15
|
|
|
Have I just not found it? It seems a pretty basic, fundamental, and
important feature. And I have not yet found any built-in support in
HTML::Mason for constructing a link (as in <a href=...)
from one Mason component to another where that link includes arguments.
That is, other than a quite awkward construction like:
<a href="/path/to/component?who=<% $who |u %>;why=<% $why |u %>">
Title goes here</a>
Having to remember to include "|u" for each parameter makes that nearly
unacceptable in my book (after seeing way too many bugs from lack of URL
escaping that pass unnoticed for a long time and then turn into a crisis,
even a security problem). It also gets quite tedious (and error-prone
and hard to read) when producing a table full of similar links.
But to see how awkward that really can be, imagine what I find to be a common
case: Having a hash of arguments that you want to include in the link. Am I
really supposed to roll my own URL constructor for such an obvious case?
<& .link, 'Edit Settings', '/widget/settings', %Context &>
...
<%def .link>
% my( $title, $page, %args ) = @_;
<a href="<% $page %>?
% for my $key ( sort keys %args ) {
<% $key |u %>=<% $args{$key} |u %>;
% }
"><% $title |h %></a>
</%def>
Although that first line is a reasonable interface, the implementation, of
course, doesn't actually work, producing:
<a href="/widget/settings?
acct=some_acct%23id;
widget=widget%2Bid;
">Edit Settings</a>
Is there a better (and actually correct) way to write such in Mason?
Too bad defining a "removing newlines and adjacent whitespace" Mason filter
(call it "|w") doesn't allow me to address this problem as simply as:
<& .link, 'Edit Settings', '/widget/settings', %Context |w &>
(You can use "|w" inside of <% ... %> but not inside of
<& ... &>.)
So, (at least for now) I resign myself to looking outside of Mason for a
solution.
So, I roll these pieces together and get:
And try to use that in my Mason:
Note the gyrations to prevent .link from including newlines.
Okay, that is quite a bit uglier than I had hoped for. But it actually works.
But it quickly demonstrated how it wasn't very flexible when I tried to use
it in a page that uses JavaScript to generate a list of links client-side
(also changing it to not take a hash of parameters but instead just a
comma-separated list of key names used to look up the parameter names and
values that are ever used from this page):
<& .link, "Edit $widget_name Settings", 'settings', 'acct,widg' &>
...
<script type="text/javascript">
...
+ '<& .link, "Edit {{feature_name}}", 'edit', 'acct,widg,feat'
+ &>'
Where the new .link replaces the ',feat' with
feature_id => '{{feature_id}}' and the JavaScript replaces
'{{feature_name}}' and '{{feature_id}}' with values
that vary between rows (one row generated per feature).
There is a risk that the second call to .link above would include something
(a ', a \, or a newline) that wouldn't be legal inside of a JavaScript string.
That sounds like a job for a Mason filter. I could define "|l" to strip
newlines (a common desire when using Mason, it seems) and "|sq" to escape
those problem characters.
Oh, except, as we already mentioned, you can't use something like "|sq"
with <& ... &>.
I could define .sq that escapes the string passed to it. Ooh, I just found
this syntax:
+ '<&| .sq &><& .link, ... &></&>'
That actually addresses (if in a manner still uglier than I had hoped)
some of the questions I had when I started writing this.
What other features am I missing? How can I do this better?
|
|
|
New Meditations
|
Five Years at the Monastery
4 direct replies — Read more / Contribute
|
by moritz
on May 21, 2012 at 04:05
|
|
|
Five years ago I joined perlmonks. Before we discussed a strange regex
feature on the German Perl IRC channel, and it spilled over to
perlmonks. My first post came out anonymously, but I believe it
is the only node I ever wrote anonymously.
Obviously I enjoy my time here, and I want to reflect on some positive and
negative aspects of perlmonks. Positive things first:
- Great community; most questions get answered quite well and fast
- Friendly, helpful and witty community
- The CB catches most off-topic discussions
- Very nice link syntax ([mod://CGI], [doc://$@] etc)
- Motivating and IMHO fair XP system
Most of my critisms relates to the technical aspects of perlmonks, which
shows its age:
- Poor Unicode support (try to use non-Latin-1 characters in <code>
blocks)
- Poor hackability: it's closed source, and even if you get access to the
source, it's hard to set up a test system, and to get started
hacking
- Unnecessary high barrier to entrance. Why does one have to use
<p> or <br> tags to get a somewhat readable node?
- I feel that somtimes trolls get too much attention. I don't have a
good solution for that, but sometimes I feel there must be more that
could be done. (Maybe grey out nodes with reputation <= -5, hide them
and their subtrees by default, and abolish Worst Nodes?)
I firmly believe that these problems are solvable, though I don't see
myself in the position to solve them.
Most importantly I want to thank all the monks for teaching me incredible
amounts about Perl and about Programming, and for providing me with
interesting problems to think about.
|
|
|
New Cool Uses for Perl
|
Ashafix: Postfixadmin port in Perl/Mojolicious
No replies — Read more | Post response
|
by mbethke
on May 18, 2012 at 11:55
|
|
|
I started a new project a while ago and recently it got its own site, time to let them Monks know about it and get it properly bashed:
Ashafix—a Postfixadmin port using Mojolicious
I was tired of trying to hack Postfixadmin to implement stuff like per-user transports and ending up wanting to gouge my eyes out at the look of the PHP, so I decided to take one last look and convert it to something more digestible/extensible, adding a bit of security like the CSRF protection Postfixadmin never got due to its nonexistent (in release versions that is, the alpha is a bit further) templating system and some protection against SQL injection through consistent use of prepared statements.
The result is still very alpha but has basic things working. As I suck at HTML design, it looks pretty much exactly like Postfixadmin but uses the Template Toolkit under the hood. It supports only MySQL so far as sprinkling the business logic with database case distinctions as in PFA was something I didn't even want to start; if someone has a good idea on how to abstract away datatype differences such as Postgres' boolean that can be 't' or 'f' vs. MySQL's TINYINT with 0 or !0 short of using DBIx::Class with column inflators, go ahead!
Any other ideas, criticism and patches (code is on GitHub) are welcome too of course!
|
|
|
New Obfuscated Code
|
Pixel art
1 direct reply — Read more / Contribute
|
by Grimy
on May 18, 2012 at 09:21
|
|
|
eval'op
e PnY+0;opPen
*XP PYH,">$0.bmp"
;XprPiYnt(KHY+pack
"XsxPY8Kl2s4a*(YH24
)X*PXPYL",19K778,7Y4,
1X2,PXPL24,32,K1,4,uYn
pXaPXcPLk("u",uKc"p_/
sX[PX^-Pj=L0d7l3K+i?,v
_Xrko3[aruPe=LanzgK%7=u
)X<=y;/RXR RL_"),sPort
{X1}grepRN{ RLs/P.\n
/X\t/;Rs N RP/\
HX/\t/R; N RX0u
ntXil!sR/(N RX.)
[JPX!-I RZO X-~]
/JP$X1$R1QO X/}<
0J>PX)&&NQ XqqP:
|J&&PX::; X'=~s/
|[JfhPX\uj-\uyPX:|
qw`&JAPX]//gr#xP/
#MP[\J]PEFXGHIP/
#MP{}JP P__/
#MZPAC|
#MPDB/
#MZP|
#MP|
#Mw|
#Mw|
#M/
#/
#
Generates this pixel art of Rainbow Dash (it will be named "$0.bmp"). Requires Perl v5.14 (see below for a more compatible version).
Can't be run from the command line, must be stored in a file (without trailing newlines).
Strict and warning compliant.
Only tested under Strawberry Perl.
|
|
|
New Monk Discussion
|
PerlMonks site design
10 direct replies — Read more / Contribute
|
by kimmel
on May 16, 2012 at 16:07
|
|
|
So I made a comment on reddit the other day about the PerlMonks site design: http://www.reddit.com/r/perl/comments/tlied/becoming_a_friar_on_perlmonks/
TL;DR: No one's done the work on a new PerlMonks site design.
Challenge issued, challenge accepted :) I downloaded a copy of the front page and got to work. I have 3 primary goals with this redesign attempt.
1. Tableless layout
2. Responsive on cellphones and other small screen devices.
3. Less visual clutter
I am not changing the color scheme or the layout so it looks the same as it does now.
Can someone post the templates that generate the pages and sections so I can verify that my changes will fit?
I am waiting on the rest of my browser shots of the site and then I will make another post with a link to a github account containing the new design as well as screenshots of the current site versus the new one on desktop browsers and on cellphones.
Are there any 'gotchas' I should know about for working on this project?
Bring on the commentary!
|
|
|
|