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
Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Copying Sartak's #cobol contribution for posterity...

code:
my $tryForBetter = 1;
my $theSite;
my $theCalculation = mark(sub {
    my $site = shift;
    $theSite = $site;
    my $initialCalculation = 3;
    $site->return($initialCalculation); # line A
});

print "The calculation is $theCalculation\n";

if ($tryforBetter) {
    $tryForBetter = 0;
    # We discover a better value for the calculation
    my $betterValue = 3.1415;
    $theSite->return($bettervalue); # Line B
}
http://idea-log.blogspot.com/2005/10/why-are-continuations-so-confusing-and.html

Adbot
ADBOT LOVES YOU

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Okay, now my real question. We've covered what. Now why. Why is this superior than traditional return's? I've not yet seen anything you couldn't do with just return. It's even worse in the above example. If there's no subroutine flow to complicate things, how is it any different then assigning one value, then assigning another right after. How does rewinding help the workflow of two assignments?

PS, thanks to Sartak for being so patient. You trooper.

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!

biznatchio posted:

What's it do that this doesn't do?
code:
...
die $fake_continuation;
...

Continuation::Escape doesn't trigger an exception to jump back up the stack. This is useful because if your code inside the call_cc catches exceptions then it has to know to rethrow the escape-continuation exception. If you get a stack trace (immeasurably useful for debugging) then it'll look like the exception occurred at the rethrow statement instead of the original expression, which really sucks. $SIG{__DIE__} adds more fun to the mix.

In short, I don't want to reuse exceptions as a flow control mechanism.

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Any suggestions on an ORM package? Any favorites?

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
DBIx::Class seems to be the latest and greatest. It's the most actively developed and maintained.

Dave Rolsky (author of HTML::Mason, DateTime, Params::Vaidate, Log::Dispatch..) has been writing Fey for a while but I haven't looked at it. Might be worth playing with if this is for a hobby project.

wolffenstein
Aug 2, 2002
 
Pork Pro
http://pastie.textmate.org/private/0gmfj9j1aafa0royekgiyg

It's output:
php:
<?
$VAR1 = {
          'server' => [
                      {
                        'awstats' => '/var/lib/awstats',
                        'name' => 'Buttjumper',
                        'address' => 'buttjumper.yourmom.com',
                        'logs' => '/var/log/awstats',
                        'username' => 'asdfasdf',
                        'os' => 'Linux'
                      },
                      {
                        'awstats' => '/var/lib/awstats',
                        'name' => 'LWBP01',
                        'address' => 'LWBP01.yourmom.com',
                        'logs' => '/var/log/awstats',
                        'username' => 'rfreiburger',
                        'os' => 'Linux'
                      }
                    ]
        };
Buttjumper
Reference found where even-sized list expected at xmlTest.pl line 12.
Use of uninitialized value $d{"HASH(0x876b1a4)"} in concatenation (.) or string at xmlTest.pl line 14.?>
The error message is where the values of the first server's hash should print. What simple mistake am I making?

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Did you dereference the hash before consuming it? Each server has multiple hash-objects, but then each of those objects need to be referred to as real hashes and not just elements of the collection.

fansipans
Nov 20, 2005

Internets. Serious Business.

Sartak posted:

DBIx::Class seems to be the latest and greatest. It's the most actively developed and maintained.

Seconded. It's got very comprehensive documentation too.

I've also heard good things about Rose::DB

Mithaldu
Sep 25, 2007

Let's cuddle. :3:

Triple Tech posted:

Okay, now my real question. We've covered what. Now why. Why is this superior than traditional return's? I've not yet seen anything you couldn't do with just return.
Not an expert on this and have only read part of the article you linked (which is pretty darn neat), however my take on a useful example is this:

Tree-search. If you have to search through some tree recursively and are only looking for one single result, i'd gather you can do something like this:
1. Mark the point where you start searching.
2. Dive into tree.
3. Once you've got the result, jump back to the marked point with the result.

Compared with bubbling the result back up with returns, this could probably be faster. More importantly though it should be easier to implement with less code, since you don't need to return ANYTHING at all, and can thus also completely skip any sorts of checks on returns.

Mithaldu fucked around with this message at 07:27 on Jan 15, 2009

Sock on a Fish
Jul 17, 2004

What if that thing I said?
I'm very new to perl and I've got like the simplest problem in the world that I can't seem to fix it. I've got an if statement that by all signs should be evaluating to true and executing its code block some of the time, but instead is never evaluating to true. The statement is so friggin simple:
code:
if ($field != 'type') {
   do whatever;
}
I print the value of $field right before the if statement and it is indeed not always equal to 'type'. I print the value if ($field != 'type') right before the statement and it is indeed sometimes 1. Yet, the code in the block never gets executed.

Mithaldu
Sep 25, 2007

Let's cuddle. :3:
String comparison is done with eq or ne. Stuff like != only works on numbers. :)

http://perldoc.perl.org/perlop.html

Sock on a Fish
Jul 17, 2004

What if that thing I said?

Mithaldu posted:

String comparison is done with eq or ne. Stuff like != only works on numbers. :)

http://perldoc.perl.org/perlop.html

Thanks!

So far, coming from Python, my impression of Perl is that it cares way too goddamn much about data types.

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Wow, I've never heard anyone say anything like that considering Perl is so lazy and does everything so automagically...

What do the more experienced Perl guys have to say about this, from a philosophical perspective?

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Triple Tech posted:

What do the more experienced Perl guys have to say about this, from a philosophical perspective?

Perl has significantly weaker typing than Python.

Because its typing is so weak, a running Perl program can't infer if you intended, for example, a string comparison or a numeric comparison. It requires the programmer to explicitly annotate the code with which comparison is the correct one.

This comes out of an older programming mentality - the programmer presumably knows what he wants, so it's "not any harder" to make it explicit. And given that the programmer can make it explicit, there's no reason to have the programming language "second guess" the programmer and force strong typing on them - computers should just do as they're told.

Fortunately, this attitude has been going away for a long time. Almost all the newer generation of languages are at least strongly typed enough to differentiate between numbers and strings, and most of the dynamic languages have very strong type systems.

mister_gosh
May 24, 2002

I am retrieving a path from an external source and now have a string:

C:\Program Files\Foo Bar Baz\bin

I need to shorten it like so:

C:\Progra~1\Foo Ba~1\bin

Any idea how I can do this with the least amount of packages?

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
GetShortPathName from Win32

mister_gosh
May 24, 2002

Triple Tech posted:

GetShortPathName from Win32

Works great, thanks!

Fenderbender
Oct 10, 2003

You have the right to remain silent.
s/\\(.{8}).+(\\)/\\\1~1\2/g would work too I would think. Untested and off the top of my head.

Erasmus Darwin
Mar 6, 2001

Fenderbender posted:

s/\\(.{8}).+(\\)/\\\1~1\2/g would work too I would think. Untested and off the top of my head.

I think your regexp is still broken. Regardless, you need to check the file system to reliably determine a short name due to the possibility of naming collisions. If I make a root-level folder on C: called "Program Stuff", it'll wind up being Progra~2 due to Progra~1 being taken by "Program Files".

Fenderbender
Oct 10, 2003

You have the right to remain silent.

Erasmus Darwin posted:

I think your regexp is still broken. Regardless, you need to check the file system to reliably determine a short name due to the possibility of naming collisions. If I make a root-level folder on C: called "Program Stuff", it'll wind up being Progra~2 due to Progra~1 being taken by "Program Files".

Ah, yes, completely forgot about that.

s139252
Jan 1, 1970
test

ShoulderDaemon posted:

Perl has significantly weaker typing than Python.

Because its typing is so weak, a running Perl program can't infer if you intended, for example, a string comparison or a numeric comparison. It requires the programmer to explicitly annotate the code with which comparison is the correct one.

Perl's typing isn't that weak. Perl has a hierarchy of several types of scalars (integer, double, string, reference, etc) and can freely change a scalar from one type to another as needed.

code:
# here $x is an integer (an IV scalar)
$x = 9;

# here $x is a string (a PV scalar)
$x = "foo";

# here $x is a PV scalar, but the addition operator
# downgrades it to an IV scalar with value 5 before
# performing the addition;  $y becomes an IV scalar
# with value 10...
$x = "   5  ";
$y = $x + 5;
The == and eq are two different operators because they do different things, not because Perl is too stupid to discern type.

The == operator tests literal C equality of its operands as integers. The scalar operands are downgraded to IV type scalars if they are a more complex type, which is why " 43 " == 43 works without warning. This operator is faster than eq.

The eq operator does string comparison via an API function called sv_eq. This op is utf8-aware, and converts its operands to "PV scalars" before evaluation. It is more expensive than using the == operator, however, Perl has absolutely no problems comparing two integer scalars via eq.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

satest4 posted:

Perl's typing isn't that weak. Perl[...] can freely change a scalar from one type to another as needed.

That is the definition of weak typing. If there was only one scalar type, like Tcl, it would be an untyped language.

satest4 posted:

The == and eq are two different operators because they do different things, not because Perl is too stupid to discern type.

Well, programmers normally elect to encode semantic differences like which equality matters in the type of the values involved, but because Perl allows essentially any type conversion this is very hard to do, so instead the programmer manually selects which comparator to call.

I think it's all just an evolution in mindset. Many of the older-generation programming languages have weak typing: Perl, C, C++ (to a lesser extent), BASIC, PASCAL. Programmers were more willing to annotate individual statements and less willing to trust compilers to infer information from types. Programming with weak types is much more similar to how the program will actually be executed, and the early high-level languages strongly reflect this.

Nowadays most programmers don't care about the compile-time or runtime overhead of strong typing systems, and are willing to admit more and more clever compilers. In C++ we got signature-based dispatch, stronger restrictions on enums and pointers and such, and the class system. Then, in Java, we lost a wide variety of automatic conversions and got even stronger restrictions on array types and primitives. Newer dynamic languages like Ruby and Python make conversions explicit and use a significant amount of type information at runtime.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
You are throwing around a lot of different ideas in your rush to declare progress. There is no accepted meaning for "weak typing", and everyone who says it means a different thing; in particular, I have no idea what you mean it to be. There is an accepted notion of "untyped" as meaning "having no static type system," which is a somewhat vague notion but certainly includes Ruby and Python. And even C "infers information from types" quite a lot.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Quote is not edit.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

rjmccall posted:

You are throwing around a lot of different ideas in your rush to declare progress. There is no accepted meaning for "weak typing", and everyone who says it means a different thing; in particular, I have no idea what you mean it to be. There is an accepted notion of "untyped" as meaning "having no static type system," which is a somewhat vague notion but certainly includes Ruby and Python. And even C "infers information from types" quite a lot.

That's a fair complaint. I was using what wikipedia indicates as the most common usage; that the language performs many implicit type conversions and consequently cannot make many promises about type safety.

I've never heard untyped specifically exclude dynamic typing. Certainly Ruby's typechecking is capable of providing more type safety than Perl or C; it just happens at runtime and is not subject to static analysis. Tcl, on the other hand, genuinely makes no distinctions at all between different kinds of scalar values.

And yes, C does have enough type information that the compiler can make some use if it, but much of the work is left to the programmer. For example, if we have a function which we would like to vary the implementation of depending on if its parameter is an int or a long, we would have to give the implementations different names and explicitly call one or the other at appropriate times. In C++, we've allowed the language some additional room to infer which implementation of the function is the most appropriate to call, and we can assign them both the same name.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

ShoulderDaemon posted:

That's a fair complaint. I was using what wikipedia indicates as the most common usage; that the language performs many implicit type conversions and consequently cannot make many promises about type safety.

This is all very difficult to interpret. We usually use "implicit type conversions" to mean things that the compiler/runtime intentionally does; it's not obvious how that affects "type safety". Clearly it doesn't change the basic soundness of the language — I can only think of one (common) language that makes any implicit unsound conversions at all (C's treatment of void*). If you mean "type safety" as in "the system forbids meaningless operations", well, I don't see how the implicit conversions in most statically-typed languages affect that — typically they're just casts to supertypes, and any "meaningless" operation defined for a supertype is necessarily defined for the subtype. So I think you're arguing that dynamically-typed languages should fail when they encounter unexpected types of values instead of trying to infer or convert to something sensible — and I agree that that's a good design principle for dynamically-typed languages.

That said, I'm not sure how compatible this is with your praise for overloading. I mean, the multiplication operator in Ruby is overloaded to work on some curious combinations of types — surely that impacts type safety just as much as implicit type conversions do.

ShoulderDaemon posted:

I've never heard untyped specifically exclude dynamic typing. Certainly Ruby's typechecking is capable of providing more type safety than Perl or C; it just happens at runtime and is not subject to static analysis. Tcl, on the other hand, genuinely makes no distinctions at all between different kinds of scalar values.

So, first off, that's not really true of Tcl; the value of an expression is always a string, yes, but those strings can be the names of variables that hold real objects that behave exactly like you would expect an object to behave. And, of course, if you're programming for efficiency you have to bear in mind that the major Tcl implementations basically store values using a sum-type just like every other dynamically-typed language. But anyway, we could substitute sh and get back to the major point.

The use of "untyped" to mean "the syntax doesn't carry type information" dates back to Church and the lambda-calculus; whatever. PL researchers usually wouldn't call dynamic typing a type system at all: of course the language has different kinds of values, that goes without saying. Even our idealized Tcl has multiple kinds of values; it just chooses to represent them all using strings. If you wanted to, you could build a real static type system on top of Tcl, tracking booleans and numbers and variable references and true-strings, and you'd gain some safety and lose a great deal of expressiveness.

Sock on a Fish
Jul 17, 2004

What if that thing I said?
I've got a step in a script at which I need to see if two times are equal to one another. They come in as a UTC string, and I'm not sure which is quicker -- convert the string to an integer with str2time and compare the integers, or just use a string comparison on the twenty-odd character string. I can't just use time to test the two methods, since my data gets pulled from an external source that varies wildly in the amount of time it takes to return a query result.

Anyone know which is quicker?

Fenderbender
Oct 10, 2003

You have the right to remain silent.

Sock on a Fish posted:

I've got a step in a script at which I need to see if two times are equal to one another. They come in as a UTC string, and I'm not sure which is quicker -- convert the string to an integer with str2time and compare the integers, or just use a string comparison on the twenty-odd character string. I can't just use time to test the two methods, since my data gets pulled from an external source that varies wildly in the amount of time it takes to return a query result.

Anyone know which is quicker?

If you're not worried about compile-time overhead, Date::Manip is good for such a thing.

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Why care about speed at all? Just use string equality.

Given no context, I would assume this is just premature optimization.

Fenderbender
Oct 10, 2003

You have the right to remain silent.
Oh, I completely missed that it's coming in UTC format. I need to readed better.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Sock on a Fish posted:

I've got a step in a script at which I need to see if two times are equal to one another. They come in as a UTC string, and I'm not sure which is quicker -- convert the string to an integer with str2time and compare the integers, or just use a string comparison on the twenty-odd character string. I can't just use time to test the two methods, since my data gets pulled from an external source that varies wildly in the amount of time it takes to return a query result.

Anyone know which is quicker?

Let me give you a hint: str2time does not magically turn the string into an integer without looking at every character in the string.

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Sure, I guess that should have been fairly obvious. Maybe, just maybe numeric comparisons are faster than string comparisons. But you're also spending cycles on conversion, which sort of defeats your quest for speed.

Again, why not go for the one with the least inertia. Less logic, less dependencies.

Ninja Rope
Oct 22, 2005

Wee.
I'm not sure if you're basing your question off of what satest4 said, but 'eq' is not so slow as to make it unwise to use. Use eq and == where appropriate, ie, eq for strings and == for integers, and don't worry about the performance implications of either.

love truncheon
Feb 1, 2006
toot toot!
Here's a few (hopefully) quick question. For a bit of background, I've got a pretty large app that does a lot of background processing, with a bit of user interaction (status messages, question & answer bits etc). The cli "interface" is done via Term::UI

The first problem i've got is that some new (external to my package) routines that need to run take quite a long time to processes (like a few minutes), so i'd like to essentially background them and keep the main body running, until i need the results... From experimenting (use Thread qw(async)) it seems the external modules are not very thread safe.
I know i can fork() and leave the child running until it finishes or waitpid() it out, if there anyway to return the modified variables from the parent back to main?

Second one up, it'd benefit quite a bit if the interface could have a status bar (similar to screen's hardstatus). Does anyone know of a nice way of doing this? It wouldn't be the end of the world if the Term::UI stuff had to go

Mithaldu
Sep 25, 2007

Let's cuddle. :3:
If you want concurrency without using actual threads, you could try Coro.

Mario Incandenza
Aug 24, 2000

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

bbatter posted:

Second one up, it'd benefit quite a bit if the interface could have a status bar (similar to screen's hardstatus). Does anyone know of a nice way of doing this? It wouldn't be the end of the world if the Term::UI stuff had to go
Curses::UI? Its label subclass does what you need:
code:
my $cui = Curses::UI->new(-color_support => 1);

my $win = $cui->add('window_id', 'Window');

my $label = $win->add(
   mylabel       => 'Label',
  -text          => 'dongs',
  -width         => -1,
  -textalignment => 'left',
  -height        => 1,
  -y             => -1,
);
$label->draw;

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Have you guys looked at Fey? This Dave Rolsky character seems pretty involved. And, it seems to address what I think I want, an object-oriented way of modeling SQL.

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
Dave Rolsky writes practically bug-free code, so I'm sure Fey is very well done. It's also built on top of Moose.

I haven't looked at it yet though. I find myself generally moving away from relational databases and am likely to try KiokuDB (an object database) for my next project that would've required an ORM.

ashgromnies
Jun 19, 2004
One of my coworkers wrote something like this:

code:
#!/usr/bin/env perl

use strict;
use Data::Dumper;

my @arr = ("one", "two");
my $hash_ref = {numbers => {}};

@{$hash_ref->{numbers}}{@arr} = ("uno", "dos");

print Dumper $hash_ref;
in an app and it's pretty cool but I've never seen that notation before.

Specifically, this: @{$hash_ref}{"donkey"} = "rear end";

What the hell is going on there? It will set the "donkey" key in $hash_ref to be "rear end" but... how? I didn't know you could use the {} notation on an array.

Adbot
ADBOT LOVES YOU

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
It's called a hash slice and is occasionally quite useful. It's clearer if you have a plain hash and not a complicated expression that evaluates to a hashref, like your coworker's code.

code:
my %color_of;
@color_of{"player 1", "player 2"} = ("black", "white");
You're not using {} on an array, it's more that you're treating a list of hash "slots" as an array.

  • Locked thread