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
floWenoL
Oct 23, 2002

Ninjew posted:

Perl supports closures, in that, if that variable is bound outside of that block of code, it is considered as a constant, not a variable, in that code. For example:

I'm not sure what exactly you mean by "constant", but if you mean the value of the variable is fixed to whatever the value is at the time of the creation of the closure, it's not true:

code:
use strict;
my $asdf = 'asdf';
my $ref = sub { my $foo = shift; print "$foo$asdf\n"; };
$ref->('teh');
$asdf = 'wut';
$ref->('teh');
prints "tehasdf" and "tehwut".

floWenoL fucked around with this message at 11:44 on Oct 25, 2007

Adbot
ADBOT LOVES YOU

floWenoL
Oct 23, 2002

Ninjew posted:

God, I love perl. No other language gives you the same kind of freedom to cheat and create very clever solutions to problems. Have a problem that would be best solved using a functional language like LISP? Just set no strict refs, and off you go! It is very refreshing to be able to actually program in a way that is not limited or constrained by the language. Don't listen to anyone who says that this flexibility is a negative aspect of perl, rather than a positive one. They are very wrong.

You are equally wrong in painting Perl's flexibility (to say it in the best possible way) in a rose-colored light (mixing metaphors here). It's a double-edged sword, and frankly, the negative edge is bigger. I'm also not sure why you associate the behavior of "no strict refs" with functional languages, given that that is a feature shared by most, if not all, dynamic languages, functional or not.

Edit:
I like Perl, but let's not turn this thread into a wankfest.

floWenoL
Oct 23, 2002

Triple Tech posted:

1) How do you feel about Perl6? I saw a Google Video of Audrey's talk to a Japanese audience and it was pretty amazing. It was a really good presentation and I'm absolutely sold on Perl6. I'm a believer! I think if I knew more about what was required to contribute, I would. But I won't speak Haskellese or whatever.

I saw Audrey's presentation live when she went to Amazon. There's some nifty stuff in Perl 6, but it's going to suffer from the same problem as Perl 5, but to a larger extent: to do any maintainable work you need to adopt a set of coding conventions (e.g., "Perl Best Practices") and resolve to only use a subset of the language. And this quickly gets ugly when you have to work with code from a different base (and different conventions and subsets of the language).

floWenoL
Oct 23, 2002

Triple Tech posted:

I have this new coding style I've adopted, and personally, I like it. But I'm pretty sure no one else is keen on it. What do you guys think?

I don't know if this illustrates it enough, but it's tucking a whole set of temporary variables inside the scope of a do-block, where the last line/return value of the do block is what we actually want.

That's actually pretty cool. I'd rather have a 'return' explicitly in the last line of the block, though.

Edit:
Oops, do {} doesn't support return. :|

floWenoL fucked around with this message at 22:30 on Oct 30, 2007

floWenoL
Oct 23, 2002

heeen posted:

^^ 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:

Yeah, that works, but you can do it a bit neater:

code:
#!/usr/bin/perl

use strict;
use warnings;

my @subset = qw(z v a t);
my @ordered_total = ('a'..'z');

print join(',', @subset) . "\n";
print join(',', @ordered_total) . "\n";

my %h = map { $_ => 1 } @subset;
my @ordered_sub = grep { $h{$_} } @ordered_total;

print join(',', @ordered_sub) . "\n";
And if you have many subsets to sort, it might be better overall to have an element => position hash and sort each subset according to that.

floWenoL
Oct 23, 2002

tef posted:

Here is a simple metric to evaluate coding styles:

Does it make the code simpler?
Does it make the code more general?
Does it make the code clearer?

Yours doesn't make the code simpler, or clearer.

I disagree. His way reduces the time in which the variable is in an "invalid state", which arguably makes it clearer.

floWenoL
Oct 23, 2002

ryan__ posted:

It does appear to be a valid technique. I've seen several examples of it in perldoc and perl design patterns, but that doesn't leave me any less disgusted with it. He could(and should) accomplish the same task by a) writing it all on one line without the temp variables, b) returning an anonymous sub, c) branching into a normal sub.

I'm assuming this is a trick for one-time insertion into someone else's code. I'd love to see a full page of do blocks in a new script.

I don't understand the vehement reaction to this. Writing it all on one line is infeasible for more complex examples, the anonymous sub does the same thing but with more syntactic noise, and branching it off into a normal sub should only be done if the same code is used in multiple places or it does a distinct conceptualizable task. I can certainly see a whole page of do blocks if you're doing a bunch of distinct steps, each of which isn't worth decomping into a separate function.

Adbot
ADBOT LOVES YOU

floWenoL
Oct 23, 2002

Subotai posted:

How do I get info about filesystems in Perl? There is a statfs call but no statvfs call, etc.

Call df and parse its output? v:)v I'm sure there's a module like that in CPAN, too.

  • Locked thread