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
Nevergirls
Jul 4, 2004

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

Twlight posted:

This is what I have so far, it cut down a lot of the txt that I didn't need. now each line has this format
code:
ip adress<tab>ws-username(this is the info i want)<tab>unused info

here is my code

open(FILE,"pso.txt");
@arr = <FILE>;

open(NEWFILE,">newfile.txt");
foreach $line(@arr)
{
	if($line =~ m/.ws\-/i)
		{
			print NEWFILE $line;
    }
You're halfway there.
code:
perl -wne "print if /.ws\-/i" <pso.txt >newfile.txt
Learning Perl's defaults and command line options is important because it saves work.

Nevergirls fucked around with this message at 09:35 on Oct 31, 2007

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.

Triple Tech posted:

A better, "more right", more complicated way would be to have a script interpret and parse the entire conf file, modify the Perl object that represents it, and then serialize it again... That would be the most flawless way.

The Perl way is to have someone else do the heavy lifting for you.

Nevergirls
Jul 4, 2004

It's not right living this way, not letting others know what's true and what's false.
I have to recommend IPC::System::Simple's systemx and capturex, because they do everything I can't be bothered to think about.

Nevergirls
Jul 4, 2004

It's not right living this way, not letting others know what's true and what's false.
Tatsuhiko Miyagawa is the best thing to happen to Perl in a decade. First, he wrote Plack. Then he found that CPAN.pm was swapping on his VPS, so he wrote cpanminus, which is tiny, does the right things with no configuration, and covers enough to almost entirely replace CPAN(PLUS).

Nevergirls
Jul 4, 2004

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

JawnV6 posted:

The pattern I always use is this:
code:
if( $string =~ /regexp/i ) {
  $name = $1;
  $plate = $2;
}
You're not checking if the regular expression matched, you're just indenting after the =~ with no handling if it doesnt match :psyduck:. So when you would see $1 and $2 defined and not $3, it means the regular expression failed and $1/$2/$3 are the results from one of the regexes further down in the program.

I prefer abusing list context:
code:
my ($name, $plate) = ($string =~ /$regexp/i);
Since the variables are lexical you don't have to worry about pollution from other matches.

Nevergirls
Jul 4, 2004

It's not right living this way, not letting others know what's true and what's false.
Your two-column table looks suspiciously like a hash:
code:
my %data = @data;

print '<table width="250" border="2">';
for my $k (sort keys %data) {
  print "<tr><td>$k</td><td>$data{$k}</td></tr>\n";
}
print '</table>';

Nevergirls
Jul 4, 2004

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

Clanpot Shake posted:

I'd like to write a perl script that calls several executable jar files in sequence and appends all of their output (standard and err) to a log file.

I've googled and tried several variations, all similar to this:
pre:
open ($log, '>>', "Publish.log") or die "could not open file: $!#";

@cmd = `java -jar thingy.jar 2>&1`;

foreach (@cmd)
{print $log $_;}
but I'm not capturing all of the program's output. Can someone show me an example of how to do this? I couldn't quite wrap my head around open3.

Try IPC::Run, specifically something like

code:
use IPC::Run qw(run);

my @jars = qw(thingy.jar thingy2.jar onemanone.jar);

for my $jar (@jars) {
  run ['java', '-jar', $jar], '<', \undef, '>>', "out.txt", '2>>', "err.txt";
}
ought to work.

Backticks are of the Devil. Avoid them at every opportunity.

Nevergirls
Jul 4, 2004

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

syphon posted:

Why? I use them extensively to call external programs in Win32 platforms (where it's much more useful to parse the output than it is to catch the return code). I've never really run into a problem with them... but that certainly doesn't mean that one doesn't exist!

As soon as you do it, you've stopped writing perl and started writing shell. (Admittedly, this may not be as much of a concern on Windows.) On top of being inflexible, it introduces things you shouldn't have to think about. E.g., to capture stderr, you have to do something like
code:
my @output = `$cmd @args >/dev/null 2>&1`;
You have to do shell things like quote metacharacters in @args (or $cmd), especially if they're user-supplied, plus you have to get the redirection syntax right (the above isn't). And you can't get separate STDOUT and STDERR, you either have to pick one or the other or intermingle them or send them to files and read them back in. That's just gross.

Modules like IPC::Run or IPC::System::Simple are easier to use and more secure than deferring to the shell. I mean, there's a reason why you're doing this in perl in the first place, right?

Nevergirls
Jul 4, 2004

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

Rohaq posted:

Quick question, I'm using while(<>) to iterate through a file line by line at the moment, is there a quick way to remove the line being processed from the file at the end of the loop, without messing up the while loop? I'd like to process a line, then remove it from the file after it's done.

You can turn on in-place edit mode in the file, just like using -i on the command line:

code:
BEGIN { $^I = ".bak" }

while (<>) {
  print if /dick/;
}
Only the lines you tell perl to print will end up in the file.

Nevergirls
Jul 4, 2004

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

syphon posted:

I keep trying to get into Catalyst or Plack

You say this like Catalyst and Plack are conceptually close. They are not.

Plack is a web server interface. You have a web application that talks Plack, and it can run on any server that understands Plack. It makes deploying web applications dead easy. This is similar to (and named after) Rack in the ruby world.

Catalyst is a web framework. It gives you tools to write web applications. (So does CGI.pm, albeit tools that were crummy by even 1998 standards.) Catalyst talks Plack, so you can deploy it on any server that understands it.

Stuck in a CGI-only environment? Plack::Handler::CGI to the rescue. Yes, even you can run complex web applications with the breathtaking speed afforded by CGI.

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

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}}'

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.

Rohaq posted:

I should have probably said; my regex contained no quote marks, I just put them in quote marks in my post.

But to my other question: Anybody got any favourite regex testers in here?

Regexp::Debugger, like any good DCONWAY module, is insane. It's brand new and I've only used it a couple times when my lovely one-liner isn't working (via rxrx). It allows you to step through what the engine is doing, not just get a match/non-match.

Here's a screencast from YAPC::NA:
https://www.youtube.com/watch?v=zcSFIUiMgAs

  • Locked thread