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
heeen
May 14, 2005

CAT NEVER STOPS
^^ I think you don't even need do, wouldn't just curly brackets suffice?


a little gem I recently stumbled over:
How to sort a subset by a given ordered superset:
code:
#/usr/bin/perl -w
my @subset=('z','v','a','t');
my @ordered_total=(a..z);
print "$_," for (@subset);
print "\n";
print "$_," for (@ordered_total);
print "\n";


my %h; 
$h{ $_} = 1 for @subset;
my @ordered_sub = grep $h{ $_}, @ordered_total;


print "$_," for (@ordered_sub);
print "\n";
code:
z,v,a,t,
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,
a,t,v,z,

Adbot
ADBOT LOVES YOU

heeen
May 14, 2005

CAT NEVER STOPS
by the way, this also works:
code:
perl -e 'print "$_\n" foreach(<*.*> );'

heeen
May 14, 2005

CAT NEVER STOPS
Someone posted a module that, when ncluded, would change all string literals to pirate speak or something like that, does someone remember the link?

heeen
May 14, 2005

CAT NEVER STOPS
how about
code:
$data[$i] = substr $input, $i*$col_width, ($i+1)*$col_width for my $i (1..160)
#or
@data = $input=~/(.{$col_width}){160}/ #or a precompiled regexp

heeen
May 14, 2005

CAT NEVER STOPS
^^^ might look a bit hackish, but maybe ($col1, $col2, ..., $col160) performs faster?

I would really like to see a simple benchmark of the various methods posted so far, including the regexp. I think the regexp wouldn't perform too badly, since it gets compiled once at compile time and should be reasonably fast after that. If you build it with fixed length matches of any character it won't have backtracking or complicated matching cases.

heeen
May 14, 2005

CAT NEVER STOPS
code:
SOAP::Data->name("pointHistory" => @credit_arr)
Even without knowing the package, this looks highly suspicious. sub parameters get coerced into flat arrays, which would lead to the following parameters passed to the sub:
code:
@_=("pointHistory", $credit_arr[0],  $credit_arr[1],  $credit_arr[2], ...)
which I'm quite certain the sub can't handle correctly. How does the sub know how many parameter elements belong to pointHistory and where the next hash key begins?
What the sub probably expects is:
code:
SOAP::Data->name("pointHistory" => \@credit_arr)
which it then can turn into a hash again.

heeen
May 14, 2005

CAT NEVER STOPS

LightI3ulb posted:

Does anyone know of any modules that would allow me to receive mail and work with the attachments? The ones I've found on google are typically just for sending.

How do you define "receiving"? opening a port and listening to it? You're better off with letting a proper mail demon handling the receiving and a perl module reading a mbox file or Maildir frequently.

heeen
May 14, 2005

CAT NEVER STOPS

Triple Tech posted:

So I want to call method from a super class that uses data defined in each sub class. Sort like...

code:
# object method

sub parse {
  my $self = shift;

  _munge($self::parameter);
}
Is this the way to go? Is my point clear enough? I don't even know if that example works of if it's even best practice. Obviously, the other way would be to just define shallow methods...

code:
sub parse {
  my $self = shift;

  _munge($self->getParameter);
}

What type is your object? blessed hashref? inside-out object?

heeen
May 14, 2005

CAT NEVER STOPS

Triple Tech posted:

What's a compelling reason to use an INIT block versions just some code in main:: when making a module? My coworker says it's easier to tell how code in a module breaks if it's inside an INIT, versus main where it just says module use failed. I told him, you could see the error if you just ran the module as a script. Thoughts?

You put initializaton and unloading code into INIT, BEGIN and END blocks when you're writing scripts that get compiled once and then used precompiled, like FastCGI and PersistentPerl

edit: Irssi scripts, too.
edit: not necessarily irssi scripts, but I think I had a case once when I used it.

heeen fucked around with this message at 19:55 on Aug 14, 2008

heeen
May 14, 2005

CAT NEVER STOPS

SubG posted:

What's the syntax for POSTing a multiple SELECT input via LWP::UserAgent?

If I remember correctly you have to submit foozle=>foo and foozle=>bar together.
wait, that doesn't work here...
try using an array instead of a hash:
$ua->post('http://some_url',[foozle=>'foo', foozle=>'bar']);

heeen fucked around with this message at 23:51 on Aug 22, 2008

heeen
May 14, 2005

CAT NEVER STOPS

SpeedFrog posted:

To be honest I can't really see any reasonable use for wantarray().
I've seen this:
code:
sub getStuff()
{
my @foo;
.
.
.
return wantarray? @foo : \@foo;
}
how about :

return generateHugeListOf10_000_000elements() if wantarray;
return $numberof10_000_000elements;

heeen fucked around with this message at 18:57 on Aug 27, 2008

heeen
May 14, 2005

CAT NEVER STOPS
Behold my palindrome test:
code:
use strict;
use warnings;
chomp (my $input = <> );
while($input=~/(.)(.*)\1/)
{
print "palindrome!\n" if length($2) <= 1;
$input=$2;
}

heeen
May 14, 2005

CAT NEVER STOPS
whoops forgot to add start and end markers to the regexp.
code:
use strict;
use warnings;
chomp (my $input = <> );
while((undef,$input) = $input=~/^(.)(.*)\1$/)
{
print "palindrome!\n" if length($input) <= 1;
}

heeen
May 14, 2005

CAT NEVER STOPS
code:
my %foo; #hash
$foo{name}="the dude";
my $ref=\%foo; #get a reference ("pointer") to %foo
print $ref->{name}; #the dude

my %bar;
$bar{name}="the girl";
$foo{girlfriend}=\%bar;

print $ref->{girlfriend}->{name}; #the girl
print $ref->{girlfriend}{name}; #only first arrow required

heeen
May 14, 2005

CAT NEVER STOPS
Riddle me this: why does php outperform perl in this nsieve test twelve-fold:
http://shootout.alioth.debian.org/gp4/benchmark.php?test=nsieve&lang=perl&id=2
http://shootout.alioth.debian.org/gp4/benchmark.php?test=nsieve&lang=php&box=1
I converted the perl version from lists to strings like the php version, and it made it two seconds slower, so that can't be it.

Adbot
ADBOT LOVES YOU

heeen
May 14, 2005

CAT NEVER STOPS

Triple Tech posted:

That implementation is gay. Looking at the C implementation, I'm going to assume these aren't out to prove how a certain data structure performs (array access), but to just solve the problem. You'd use a bit vector with vec(). (See Computing primes)

Edit: Ugh, who wrote that Perl submission. It's so different from the PHP one.

There's a seperate version with bitsets for each language.

  • Locked thread