Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
jeeves
May 27, 2001

Deranged Psychopathic
Butler Extraordinaire
Yeah, I was going about the whole thing wrong, trying to break up the line into words or such.

I just needed to do a match on <em> or </em>, and if it matches, first substitute either of those tags for a blank space, and then do a substitution of every word to make it word+dashes around it.

code:
s{ ([a-z]+) } {-\1-}gix; 
That makes the above examples of mine output correctly (not counting number as words), and also doesn't count punctuation as words.

And yes, I know I am using {} instead of // for RE in this, and also tons of useless whitespace (via the x at the end).

Edit - Ugh, now that I think of it, this solution breaks on this line though: "<em> blah </em> blah" by making both blahs have dashes BUT OH WELL. I'd never use RE for actual html conversion anyhow.

jeeves fucked around with this message at 22:06 on Jul 16, 2012

Adbot
ADBOT LOVES YOU

Nevergirls
Jul 4, 2004

It's not right living this way, not letting others know what's true and what's false.

Sizzler Manager posted:

As I understand it, if you use .. inside an if like this, it starts returning true as soon as the left hand side matches, and it stays true over multiple lines until the right side matches.

That's roughly correct; it's a language feature borrowed from sed and awk. Its behavior is determined by list/scalar context. And it gets a little crazy. http://perldoc.perl.org/perlop.html#Range-Operators

slightlyslow
Aug 19, 2002
Cheer up, emo kid.
Hey, so I'm forcing myself to learn Perl so I can use this (of course) Perl-specific bioinformatics API. Is there any reason a declared variable would reset after a foreach loop? Here's the specific part of my code that is going awry.

code:
    my $num_genes=0;
    my @gene_names;
    foreach my $homology (@{$homologies}) {
      if ($homology->description eq "ortholog_one2many"){
	print $homology->description," ", $homology->subtype,"\n";
	my $members = $homology->get_all_Members();
	foreach my $this_member (@$members) {
	  print $this_member->source_name, " ",
	  $this_member->stable_id, " (",
	  $this_member->genome_db->name, ")\n";
	  $num_genes++;
	  say $num_genes;
	  if ($this_member->genome_db->name eq "homo_sapiens"){
	    push @gene_names, $this_member->stable_id;
	    push @gene_names, find_uniprot($this_member->stable_id);
	  }
	}
      }
    }
    say $num_genes;
    say @gene_names;
Now, my assumption was that the $num_genes and @gene_names variables would keep their values that were built up in the foreach loops, but they for some reason reset to 0 and empty, respectively. I'm coming from python, so this behavior is baffling. Or have I missed something obviously wrong?

het
Nov 14, 2002

A dark black past
is my most valued
possession

slightlyslow posted:

Hey, so I'm forcing myself to learn Perl so I can use this (of course) Perl-specific bioinformatics API. Is there any reason a declared variable would reset after a foreach loop? Here's the specific part of my code that is going awry.

Now, my assumption was that the $num_genes and @gene_names variables would keep their values that were built up in the foreach loops, but they for some reason reset to 0 and empty, respectively. I'm coming from python, so this behavior is baffling. Or have I missed something obviously wrong?
None of the obvious things stand out here, my first question would be does the "say $num_genes" in the nested loop actually print what you expect? Also, do you use strict/use warnings at the top?

slightlyslow
Aug 19, 2002
Cheer up, emo kid.
Yeah, I'm using strict/warnings and am not getting anything. The $num_genes variable is incrementing correctly inside the foreach loop.

Hm, looking more closely that bit of code is part of an eval block and seems to be exiting out early. I think I need to do some more debugging before asking for more help. Thanks though!

OK, so I neglected to actually check which errors were being thrown. One of the function returns was different from what I expected and caused the screwup. Sorry about that.

slightlyslow fucked around with this message at 20:28 on Aug 2, 2012

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
What is state of developers were you work? (assuming you do Perl full time) We lost three people recently and it's been really hard to find developers of that caliber to replace them. How are you guys coping with the current state of the industry?

Ninja Rope
Oct 22, 2005

Wee.
Lots of resumes, all terrible.

het
Nov 14, 2002

A dark black past
is my most valued
possession

Triple Tech posted:

What is state of developers were you work? (assuming you do Perl full time) We lost three people recently and it's been really hard to find developers of that caliber to replace them. How are you guys coping with the current state of the industry?
It's been difficult, nobody's really hit our sweet spot for Unix knowledge, programming acumen, and knowledge of stuff like Perl or ruby. So far our best bets (granted, this is in a Unix systems programming context) have been transitioning Ops people (sysadmins) who show some promise with scripting languages. Other than that it's been a mix of newbies who can't code their way out of a paper bag and older C coders (half of whom still can't code their way out of a paper bag). We had one kid we really wanted but either he got cold feet about moving, our HR dept/boss hosed up giving him a good offer, or we hosed up the interview (probably one of the last two).

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
We deal with the shortage by churning through as many voice and face-to-face interviews as possible. Even then we can't hire quickly enough, it's hard to find good Perl dudes :(

BTW we're literally always hiring, so if you're a good Perl dude and are willing to relocate, send me a PM.

prefect
Sep 11, 2001

No one, Woodhouse.
No one.




Dead Man’s Band

Mario Incandenza posted:

We deal with the shortage by churning through as many voice and face-to-face interviews as possible. Even then we can't hire quickly enough, it's hard to find good Perl dudes :(

BTW we're literally always hiring, so if you're a good Perl dude and are willing to relocate, send me a PM.

Relocate to where?

Ninja Rope
Oct 22, 2005

Wee.
I've started asking for code examples. Everyone who makes it to round 2 gets an example spec and is expected to write a unix daemon that implements that spec. Doesn't matter what language, I figure a good programmer can learn Perl faster than we can turn a bad programmer into a good one.

Similarly, if anyone wants a Perl job in the Bay Area. I think we re-locate, but I'm not sure.

Jonny 290
May 5, 2005



[ASK] me about OS/2 Warp

het posted:

It's been difficult, nobody's really hit our sweet spot for Unix knowledge, programming acumen, and knowledge of stuff like Perl or ruby. So far our best bets (granted, this is in a Unix systems programming context) have been transitioning Ops people (sysadmins) who show some promise with scripting languages. Other than that it's been a mix of newbies who can't code their way out of a paper bag and older C coders (half of whom still can't code their way out of a paper bag). We had one kid we really wanted but either he got cold feet about moving, our HR dept/boss hosed up giving him a good offer, or we hosed up the interview (probably one of the last two).

This gives me hope. (NOT SOLICITING) I've got a skillset that pretty much exactly shows what you're looking for, and am moving to Denver or the PNW in the next 12-18 months and have been polishing up the resume and learning more about Perl and languages in general. Sorry for the jobchat derail, it's interesting to hear what the market's like here and there.

Polygynous
Dec 13, 2006
welp
Yeah, this recent discussion has me wanting to get of my rear end and finish school, or at the least do more with perl than my typical hacky ten line scripts. For that side of things I think part of my problem is the lack of a good reference, is something like the camel book good for that? (hey, 4th ed is out and the ebook is half off this week...)

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?

prefect posted:

Relocate to where?
Amsterdam.

prefect
Sep 11, 2001

No one, Woodhouse.
No one.




Dead Man’s Band

If I spoke Hollandaise, I'd be on the next plane. :smith:

Rohaq
Aug 11, 2006
I really want to go to Amsterdam :(

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

prefect posted:

If I spoke Hollandaise, I'd be on the next plane. :smith:

According to Curtis Poe, it's really not bad for an American Perl programmer to move to Amsterdam and pick up the language while he's there

Clanpot Shake
Aug 10, 2006
shake shake!

I'm trying to look at an FTP directory and find files with a modification date of today (the current day). I found this code that opens a directory and reads its date, but it doesn't always work.

When I run this in /home/username it correctly prints all of the files and their dates. When I run this elsewhere, like /lib or the FTP directory, it always prints "31121969" (December 31, 1969). Why is this? Is it a permission thing? Does this date have any significance?

code:
#! /usr/bin/perl

use POSIX;

opendir (D, "/lib") || die "$!\n";
while (readdir(D))
{
	my $date = POSIX::strftime( 
				 "%d%m%Y", 
				 localtime( 
					 ( stat $_ )[9]
					 )
				 );
	print "$_ :: $date\n";
}
__END__

MrKatharsis
Nov 29, 2003

feel the bern
A lot of computer systems count the date as time forward since Dec 31st, 1969. I would assume that the value it's returning is "0" or it's substituting "0" as a return due to some permissions snafu.

Polygynous
Dec 13, 2006
welp
$_ was coming up empty; changing "while (readdir(D))" to "while ($_ = readdir(D))" fixed that.

edit: don't know why it would work sometimes and not others, perlfunc has this though:

quote:

If you're planning to filetest the return values out of a "readdir", you'd better prepend
the directory in question. Otherwise, because we didn't "chdir" there, it would have been
testing the wrong file.

Clanpot Shake
Aug 10, 2006
shake shake!

spoon0042 posted:

$_ was coming up empty; changing "while (readdir(D))" to "while ($_ = readdir(D))" fixed that.

edit: don't know why it would work sometimes and not others, perlfunc has this though:
Before making this change it always printed the filename, so it wasn't empty. I made this change and the output is the same. I then prepended the file path to the filename and it works now. Weird.

code:
#! /usr/bin/perl

use POSIX;
use File::Spec::Functions;

my $dir = "/lib";
opendir (D, $dir) || die "$!\n";
print "$dir\n";
while ($_ = readdir(D))
{
	$file = File::Spec->catfile($dir,$_);
	my $date = POSIX::strftime( 
				 "%d%m%Y", 
				 localtime( 
					 ( stat $file )[9]
					 )
				 );
	print "$_ :: $date\n";
}
__END__
e; now I have to get this to work with an FTP directory listing which is apparently a whole different beast.

e2; apparently there's a mdtm function that gives the file's modification date in Net::FTP. sigh.

Clanpot Shake fucked around with this message at 15:54 on Aug 10, 2012

EVGA Longoria
Dec 25, 2005

Let's go exploring!

Clanpot Shake posted:

Before making this change it always printed the filename, so it wasn't empty. I made this change and the output is the same. I then prepended the file path to the filename and it works now. Weird.

code:
#! /usr/bin/perl

use POSIX;
use File::Spec::Functions;

my $dir = "/lib";
opendir (D, $dir) || die "$!\n";
print "$dir\n";
while ($_ = readdir(D))
{
	$file = File::Spec->catfile($dir,$_);
	my $date = POSIX::strftime( 
				 "%d%m%Y", 
				 localtime( 
					 ( stat $file )[9]
					 )
				 );
	print "$_ :: $date\n";
}
__END__
e; now I have to get this to work with an FTP directory listing which is apparently a whole different beast.

e2; apparently there's a mdtm function that gives the file's modification date in Net::FTP. sigh.

I'm doing the FTP thing in an old script of mine already, I'll pull it up tomorrow if you want to take a look.

het
Nov 14, 2002

A dark black past
is my most valued
possession

Clanpot Shake posted:

e; now I have to get this to work with an FTP directory listing which is apparently a whole different beast.

e2; apparently there's a mdtm function that gives the file's modification date in Net::FTP. sigh.
I don't know if this is an option for you, but I would absolutely avoid doing the dirty work of this in Perl and just wrap it (or a shell script) around lftp. The lftp man page seems to indicate that the mirror command takes a flag that only selects files newer than a certain date, which is what you're looking for, right?

GTGastby
Dec 28, 2006
I'm trying to kill a particular process (foo) from within a perl script (bar).

I'm trying the command:

code:
system("kill -9 `ps -ef | grep foo | awk '{ print $2 }'`");
The unix bit seems to run fine when I run it in the shell, but when I put it in my perl script, the script shuts down / kills itself when it hits that line. Although it does kill foo in the process - I'd like the perl script to keep running.

Any idea what is going wrong?

code:
ps -ef | grep 
The above bit is not picking up my perl script, for those wondering.

het
Nov 14, 2002

A dark black past
is my most valued
possession
This doesn't answer your question, but generally stuff like that is sort of a red flag to me; it looks like writing a shell script in perl, which generally ends up being ugly.

That said, I can't see any reason it would kill the perl script unless "foo" were in the command line for the perl script, which I'm assuming you ruled out already (I also notice you don't have grep -v grep in your pipeline but that probably won't hurt anything in this context)

edit:

oh yeah, good point
vvvvvvvvvvvv

het fucked around with this message at 04:17 on Aug 15, 2012

Ninja Rope
Oct 22, 2005

Wee.

GTGastby posted:

I'm trying to kill a particular process (foo) from within a perl script (bar).

I'm trying the command:

code:
system("kill -9 `ps -ef | grep foo | awk '{ print $2 }'`");
The unix bit seems to run fine when I run it in the shell, but when I put it in my perl script, the script shuts down / kills itself when it hits that line. Although it does kill foo in the process - I'd like the perl script to keep running.

Any idea what is going wrong?

code:
ps -ef | grep 
The above bit is not picking up my perl script, for those wondering.

It could be the $2 being interpolated due to the double quotes, but you should probably re-write it to use something like Proc::ProcessTable and kill().

GTGastby
Dec 28, 2006

Ninja Rope posted:

It could be the $2 being interpolated due to the double quotes, but you should probably re-write it to use something like Proc::ProcessTable and kill().

Thanks guys. Duly noted about the shell script - I went with this approach because it's seemingly very difficult to compare time in a shell script - ie "if it's past 6pm, then kill this job". This is undoubtedly a horrible hack job any way you look at it.

I'll look into ProcessTable and kill(). Was hoping to avoid those because it will involve looping and arrays and I'm bad at perl. :(

het
Nov 14, 2002

A dark black past
is my most valued
possession

GTGastby posted:

Thanks guys. Duly noted about the shell script - I went with this approach because it's seemingly very difficult to compare time in a shell script - ie "if it's past 6pm, then kill this job". This is undoubtedly a horrible hack job any way you look at it.

I'll look into ProcessTable and kill(). Was hoping to avoid those because it will involve looping and arrays and I'm bad at perl. :(
If installing modules from CPAN is a problem (like because of firewalls) then that's one thing, but looping and data structures are pretty easy to help with.
Perl code:
my $t = new Proc::ProcessTable;
foreach my $p (@{$t->table}) {
	kill(9, $p->{pid}) if ($p->cmdline =~ /foo/);
}
I should warn that I would be extremely explicit with that /foo/ regexp, you really do not want some process killing stuff because some substring occurs in the cmdline. If you know it's /usr/bin/foo or whatever, then you should be making the regexp match that as specifically as possible.

uG
Apr 23, 2003

by Ralp
I'm making some sort of tiny stupid mistake, and i've spent way too much time trying to figure it out. It has to do with the chomp at line 27 and the assignment of $_ to $away on line 28. For some reason, it deletes the first character of $away if I print it, or if I do print "$away | $away" it might print " | NEVV" (instead of "NEV | NEV").

http://pastebin.com/saiTHRQN


EDIT: Problem was extra formatting at the end of each line in the source file, naturally :)

uG fucked around with this message at 19:06 on Aug 15, 2012

Nevergirls
Jul 4, 2004

It's not right living this way, not letting others know what's true and what's false.

uG posted:

if I do print "$away | $away" it might print " | NEVV" (instead of "NEV | NEV").
yeah, every time I've seen that it's extraneous carriage returns. CR without LF will put the cursor at the beginning of the line and overwrite output.

which can be useful:

perl -CS -Mutf8 -e'while(++$|){for(qw(✭ ✮ ★ ✯ ✫ ☆ ✩)){print"\r$_ throbbing, please wait… $_ ";select$u,$u,$u,0.1}}'

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
So, who's going to be at YAPC::EU next week?

uG
Apr 23, 2003

by Ralp
I like the idea of DBIx::Class::Validation for my Catalyst app's API, but I don't like that I seemingly have to do the validation on my forms separately. I've used HTML::FormHandler before but didn't think it was very elegant when it came to validating API input. I could use FormValidator::Simple with DBIx::Class::Validation and as Catalyst::Plugin::FormValidator::Simple, but it seems i'd still be left with declaring the same constraints twice.

What do you guys do for input validation when you have forms and an API?

EVGA Longoria
Dec 25, 2005

Let's go exploring!

Anyone have any tricks for designing complex data structures? I've been doing it in my head but it ends up taking longer than it should to wrap my head around it. I'd like an easy way to (visually, preferably) layout a hash of arrays of hashes and the like to make sure I'm getting all the key pieces.

syphon
Jan 1, 2001
code:
use Data::Dumper;
print Dumper($data);

prefect
Sep 11, 2001

No one, Woodhouse.
No one.




Dead Man’s Band

syphon posted:

code:
use Data::Dumper;
print Dumper($data);

One of these days, I'm going to write a Data::Dumper::Graphviz module and then pass out from pure happiness. (Graphviz is awesome.)

Edit: Oh, poo poo, there's already GraphViz2::Data::Dumper!

(I made a questionable career choice a few years ago, and have been stuck in VBScript-land since.)

prefect fucked around with this message at 23:18 on Aug 20, 2012

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?

EVGA Longoria posted:

Anyone have any tricks for designing complex data structures? I've been doing it in my head but it ends up taking longer than it should to wrap my head around it. I'd like an easy way to (visually, preferably) layout a hash of arrays of hashes and the like to make sure I'm getting all the key pieces.

Maybe you just need to mitigate its complexity? What are you designing?

Arrays for collections, hashes for objects. Dunzo.

EVGA Longoria
Dec 25, 2005

Let's go exploring!

Triple Tech posted:

Maybe you just need to mitigate its complexity? What are you designing?

Arrays for collections, hashes for objects. Dunzo.

It was slightly more complex with that, but I ended up getting it done.

In a vaguely related sense of things, does anyone have good sources for DBIx::Class? Specifically, an introduction? I'm familiar with ORMs to some extent, but this would be my first time really using one in more than the most basic way, and I'd like to do it right.

Neither Modern Perl nor Higher Order Perl go into this that I can find.

uG
Apr 23, 2003

by Ralp
The docs for DBIx::Class are pretty thorough and are probably your best bet.

Clanpot Shake
Aug 10, 2006
shake shake!

I've got a problem that requires some deeper knowledge of of Unix file permissions than I have. I have a script that moves files from one directory to another (really a map of several dozen source->dest directories but that isn't important).

One of the destination directories (call it Inbound) is watched by another process I have no control over. What it does is look for the files that I'm moving, and when it sees them moves them to another folder for processing. The trouble is, it doesn't appear to wait for the file to be completely written - it will just grab whatever portion of the file is there when it checks. This is causing some errors.

I've been using the CPAN File module to move files from A to B, but that isn't going to work for this. I need to move the files to this particular directory with no read permissions (000, ideally), then chmod them to another value (I've been given this value).

How can I move files with no read permissions?

Adbot
ADBOT LOVES YOU

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde
You don't have to copy a file's contents to move it. You can create a hard link at the new location and unlink the old location.

  • Locked thread