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
<deleted user>

ashgromnies posted:

What the hell is a closure? I read your example and didn't understand WTF was going on and I'm most of my way through a CS degree.

A reference to a subroutine that carries scoped data.

code:
package main;
use strict;
my $a = sub { my $thing = 'blue'; };
sub test { print $_[0]() };
&test($a);
This will print 'blue', because $thing persists in the scope of the closure.

Very powerful and useful concept. :)

Also, regarding the "closure as object" concept, don't forget that you can bless function references.

Adbot
ADBOT LOVES YOU

<deleted user>

Sartak posted:

It's even more than that. To have a closure, the variable has to be defined outside of the function. In CSey words, it needs to be a "free variable" in the scope of the function.

Interesting, I'd not realized a closure differed from an anonymous sub based on it encapsulating data from a higher scope.

quote:

code:
use autobox::Closure::Attributes;

print $a->thing; # "blue"
$a->thing("green");
print $a->thing; # "green"

test($a); # I am colored green

I'm curious why you chose to incur the overhead of AUTOLOAD and bring in PadWalker to accomplish this. Why not use a hash reference? If you really want accessors, you could define a simple object, or even bless the hash into Class::Accessor::Fast and attach some.

<deleted user>

Puck42 posted:

code:
UPDATE users SET fullname = ? WHERE ( index = ? )

The word 'index' is a MySQL reserved word, so:

code:
UPDATE users SET fullname = ? WHERE ( [b]`index`[/b] = ? );
-- or --
UPDATE users SET fullname = ? WHERE ( [b]users.index[/b] = ? );
Better yet, don't use reserved words as column names in your schema.

<deleted user>

ashgromnies posted:

I need some help with Catalyst exception handling.
It looks like it is calling the JSON view but the client's browser receives back an HTML error page from the AJAX request.


Ideas?

Catalyst's error handling is horrible. Remember that if you are in a controller dispatch, exceptions get wrapped to Catalyst errors (which I absolutely hate).

By default Catalyst will check $c->error at the end of a request and show its goofy error page. To override the default error handler, you need to check $c->error yourself (usually in MyApp::end()).

code:
sub end {
   my ($self, $c) = @_;
   ...
   if(($c->req->header('X-Requested-With') || '') eq 'XMLHttpRequest') {
      # select a json view
   }
   
   if( scalar(@{$c->error}) ) {
      ...
      # handle the error however you'd like
      MyApp::View->set_error(...);
      ...
      # then, clear the errors so Catalyst won't handle them
      $c->{error} = [];
   }
}

<deleted user>
What are you accomplishing by using threads? It seems like a job queue might be a better fit for what you are trying to do. Have the worker update the job status as it processes it, then just let your Catalyst action query the job status. This has the benefit of being asynchronous.

Also, in this statement:

code:
if (eval { $somemodel->perform_action($params, $callback) } ) {}
If you are going to catch exceptions, you may as well treat them as such:

code:
eval {
  $somemodel->perform_action($params, $callback)
  $c->stash->{'json_status'} = SUCCESS;
}

if($@) {
   $c->log->debug("error occured during perform_action: $@");
   $c->stash->{'json_status'} = ERROR;  # better than using a string
   $c->stash->{'error_msg'} = $@;
}

<deleted user>

ashgromnies posted:

So no one knows how to get around the fact that Catalyst writes out session variables at the end of the request? I tried using a Cache, that didn't work either :(

Sessions are implemented as plugins (last I had seen anyway), so you are bound by the plugin implementation and the various Catalyst phases.

The intent of a "session" is to preserve state between HTTP messages, not within one request, so it should not be an issue when the session state is committed. I suspect you may be using the wrong tool for the job, but to be honest I'm not sure I'm correctly discerning exactly what you are wanting to do (maybe that's why others are not responding).

Adding threading to an application greatly affects how MVC should be applied.

<deleted user>
How about this?

code:
find . -regex '.*\.yml$' -type f -print0 |xargs -0 perl -mYAML -e 'eval { YAML::LoadFile($_); 1; } or warn "fail: $_\n\t$@" for(@ARGV);'

<deleted user>

yaoi prophet posted:

It's only run-time checking, of course, but it's an interesting (ab)use of subroutine attributes.

Isn't it sad that there are so many modules like this on CPAN? There are tons and tons of attempts to add things to the language to make it seem modern and sane. I think it was a big mistake to set perl adrift to work on parrot and perl6.

There's some cool stuff out there, like Moose, but the fact that it needs to exist is a red flag. And the problem with all of this run-time compensation is that it really does take its toll on performance with heavy use (and I'm of the opinion that you either use it everywhere or not at all). Plus it adds to module dependency hell. For example, to install Catalyst you basically become a CPAN mirror... it's horrible.

<deleted user>

poofactory posted:

x_amount => "12.23",
x_amount => "Amount",
x_amount => "$Amount",

The "12.23" is a string literal. So is "Amount". Your $Amount is a scalar variable, which is a thing that holds whatever value is assigned to it. Whenver you say $Amount, perl treats it as "the value stored in $Amount". When a scalar variable appears in a double-quoted string (like the last line above) it will be evaluated, meaning its value is replaced into the string at the position it occurs. You don't need the quotes in your above code ("$Amount" and $Amount are the same here).

Also, I'm not sure what's going on in the rest of your script, but something has to assign a value to $Amount before it will be useful to you. In your case, you want to assign it the data from the HTTP request form input. How you do that depends on what perl modules you are using to work with request data.

<deleted user>

Oh god. Every time I see a module like this I laugh until I realize there are probably people using it in production.

<deleted user>
Catalyst started off great, but it has become a gigantic bloated pig. The whole project went into the woods after a while.

I've looked at all the perl frameworks at length, and I can't say I like any of them anymore. What I'd really like to see is something that very efficiently processes the HTTP request of an environment (CGI, mod_perl, etc) into some object, and a fast url->action mapper. That's the only parts of a framework I find I actually want. The rest is all crap from CPAN (and I do mean crap).

quote:

Continuity...

Continuity is a neat idea, but it's not extremely useful since its going to have much steeper memory requirements than the traditional approach. That's a bad thing in a perl environment.

<deleted user>

Triple Tech posted:

I tried jamming the substr's into a list (@array = substr, substr, substr), that made it slow. The col_width technique that heeen posted wouldn't work because each column is different. And I'm pretty sure using a regexp would be universally slower than unpack or substr.

I'm not following your description of the data. I think you are saying: 1.3 million records, each record has 160 fields, and you know the width of each field? You should be able to smoke through that. Are the fields all text or are some binary?

Is the data in a flat file? How are you reading from that (seek/read I hope)?

quote:

Edit: I also tried pushing them onto a stack instead of having Perl make that temp list, but it was still slow. I can't believe scalars are so much faster than arrays...

You're probably doing this already, but make sure you are using array references and not pouring data through list context needlessly. Here's why:

code:
------------
#!/usr/bin/perl -w

package main;

use strict;
use Benchmark;

my @foo = qw/one two three four five/;
my (@bar, @bam, @baz);

Benchmark::cmpthese(-5, {
    bylist  => sub { push(@bar, [@foo]) },
    byref   => sub { push(@bam, \@foo) },
    byref2  => sub { my $aref = \@foo; push(@baz, $aref) },
});
code:
[genadmin@mancub sand]$ perl b.arrays.pl
            Rate bylist byref2  byref
bylist  333040/s     --   -75%   -81%
byref2 1341143/s   303%     --   -23%
byref  1738044/s   422%    30%     --
I'm sure you'd get more insight if some code were posted, but I highly doubt your co-workers approach is optimal.

<deleted user>

TiMBuS posted:

I went hunting today, and I concur. All I really want are sessions handled for me, a pluggable, easy to manage template system and automatic dispatching. Oh and the ability to specify different methods of dispatch handling.

And sessions and templating are (rightly) not even provided by the framework. I think people go in this progression of "acknowledge anarchy -> adopt framework -> learn patterns -> rewrite large chunks of framework -> wonder why they need a framework".

And don't even get me started on template systems. :jihad:

<deleted user>

Khorne posted:

How do I convert an a4 value in perl (DWORD) to a string, or at least a decimal/hexadecimal number that I can pad some how?

I tried sprintf("%08x", $var); and it just returns 8 0s. I tried bit shifts with & and other far more work than it should be solutions. I'm willing to admit that I am overlooking something stupidly simple.

Check out pack() and unpack().

code:
my $b = pack('l', 0xffffffff);

<deleted user>

Zombywuf posted:

I find your requirements strange and disturbing.

But hey, whatever floats your boat.

URL->action mapping is not a strange thing to want at all. It is much more inline with HTTP to make application requests by path than by query string, and having a good URL structure is very important for things like Google indexing.

TiMBuS posted:

Template Toolkit and Mason forgot what the hell a template was a long time ago.

Yeah, both of these could be put out to pasture. They are mostly relics of the big transition to embedded scripting many years ago, back when ASP and PHP first really caught on. Now the fad is frameworks. I wonder what's next after that fizzles. It's amazing that Perl has managed not to change at all during any of this. :frog:

<deleted user>

permanoob posted:

It isn't dying and open RETURNS no. I just tried printing and dieing the $dest_file value. Print outputs the correct file name it should be writing to. Die is killing the script with

It sounds like the open() at line 100 is simply not being reached. Is the print statement on line 102 ever outputting anything? Try putting a print statement after line 56 to see if you ever make it that far.

The script looks pretty error-prone, so you may just want to break out the debugger.

<deleted user>

SpeedFrog posted:

Don't really like constant.pm... the lack of (easy) interpolation and all the edge-cases requiring &s or ()s make the cure worse than the disease.

Instead, why not:

Because that Readonly module is going to be really slow. The whole point of constant.pm is that the compiler will inline the data (because it is a sub) and creates no symbol table entry. It's more like a pre-processor constant the way C constants are. The syntax is retarded, I agree, but it's a very important module.

<deleted user>

ashgromnies posted:

code:
SOAP::Data->name("pointHistory" => @credit_arr)

I'm in a rush and not familiar with SOAP or this module, so sorry for posting, but two things to check....

1) $elem may not be getting what you think (list of individual nodes), and you're dumping that here via list context. How about using the SOAP::Data->name('thing')->value(); form instead?

2) I think you are wanting to supply SOAP::Data->name("history") with a list of "pointHistory" nodes, but it looks like you are supplying just one with @credit_arr as its value/children.

Either way, bust out Data::Dumper, check out what is going into $elem & @credit_arr and have a look at the SOAP::Lite source and make sure those methods expect/return what you expect. I only glanced, but the docs seem to suck.

<deleted user>

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.

Are you talking about a module to interface POP3 or IMAP protocol to access mail attachments? Sending mail is SMTP, storage and access of mail are other animals in a different part of the zoo.

<deleted user>
I had no idea Vim had built-in perl support (and ruby/python). I have absolutely no idea how I've not heard of it before.

Run a perl regex on each of lines 10-20. Since $_ is set to value of the line being examined, both lines below are the same:

code:
:10,20perldo $_ =~ s/foo/bar/gi
:10,20perldo s/foo/bar/gi
I'd much rather write perl regex for line manipulation than use Vim's regex.

Vim exposes some of its innards to the perl interpreter. This will print the perl version as a message on the Vim console:

code:
:perl VIM::Msg(sprintf("perl version is %vd", $^V));
Delete a random line from the current buffer:

code:
:perl $curbuf->Delete(int(rand($curbuf->Count()))+1);
I might write a View::Vim component for querying our app framework from inside Vim. That would be nice for development.

<deleted user>

MrHyde posted:

What's the difference between the all-caps filehandles and just using a scalar variable?

Perl uses something called a "typeglob" (a "GV" internally) to keep track of things defined in a package for a given name. The "things" can be scalars, arrays, hashes, filehandles, formats, subs, etc. Think of a typeglob as "all things in this package with this name". The typeglob is what allows you to have both $foo and @foo and %foo and a filehandle named foo.

Perl uses context to determine what type of thing you refer to.

code:
package main;
use strict;
use warnings;

# perl expects that STDOUT is a filehandle because that is
# what print expects (print provides the filehandle context).
print STDOUT "hello\n";

# in this context, perl thinks the same bareword is a sub!
{
   no strict 'subs';
   no warnings;
   STDOUT;  # same as &STDOUT()
}
STDOUT could be any name. The point is, perl associates the name to a thing of some type. STDOUT is only a filehandle for print because that is what it expects.

Now let's get really weird. In the same script, add:

code:
my $STDOUT = "WTF";
my @STDOUT = qw/1 2 3/;
# prints "WTF3" to stdout
print STDOUT $STDOUT . @STDOUT . "\n";
Again, print wants a filehandle first, so that is how it treats the bareword STDOUT. After that, the scalar $STDOUT is seen, and then @STDOUT (in scalar context). All of these exist at the same time because they are different types of things referred to by the same name in the symbol table.

Want more?

You can use * to refer directly to a typeglob.

code:
# we'll use this soon...
sub foo { print "foo as sub got ${_[0]}" }

# scalar reference to typeglob
my $foo = \*STDOUT;

# print scalar value of $foo to STDOUT
print $foo "$foo\n";

# now see if you can figure these out...
*main::fuz = *main::foo;
fuz($foo);
fuz(*foo);

This is actually a useful idiom (especially when you bring local into the mix), but I'll stop there. Also be aware there is a *FOO{TYPE} syntax that allows you to refer to a thing of a specific type.

Anyway, the reason a bareword filehandle is frowned upon is that you are creating a thing of a certain type by implied context in package scope. Using a scalar reference is more explicit and has lexical scope.

<deleted user>
open implies the '<' mode if none is specified. It won't ever clobber existing files unless specifically instructed to do so.

<deleted user>

Triple Tech posted:

Goodness, I didn't know this was crit Perl time... Anyway, scalars are far, far superior to filehandles (symbol thingies?) because they will automatically close when the scalar goes out of scope. And, filehandles are global in scope (generally bad) while scalars, defined with my(), are the most limited in scope.

Note that open assigns a filehandle reference to a scalar if passed. The scalar is not a filehandle.

quote:

Small/limited scope is good.

code:
{
  open my($fh), 'foo.txt' or die;
  process($fh);
}

# $fh is already closed and dead! :)

That's a bit useless because $fh only exists within the open() call and would go out of scope by the time process() was called.

(edit: totally wrong, ignore me)

<deleted user>

Sartak posted:

That's not true.

Sorry about that... I never use my that way so it just looked wrong.

code:
*genericadmin::post = sub { think() && logout() };

<deleted user>

Sartak posted:

I've got one more for you!

Right, I thought someone above was saying that using the two-arg form with no mode was bad because it could clobber an existing file.

Also... does anyone in this thread do much XS?

<deleted user>

quote:

I've done some XS

I hooked $SIG{__WARN__} to call pstack and was able to answer my own question. I wasn't handling an undef SV* properly.

<deleted user>
Is anyone familiar with how fields.pm performs since the 5.009 changes? I don't have a perl > 5.8.8 anywhere to test.

These are benchmark results for 5.8.5 for creating vanilla objects and accessing an instance attribute.

no_fields: simple bless({}, $class) constructor
restricted hash: same, but uses Hash::Util::lock_keys() after bless
fields_untyped: constructor has "my $self = shift"
fields_typed: constructor has "my MyClass $self = shift"

code:
bench object creation... ($sv = $foo->{new})
                     Rate fields_untyped fields_typed restricted_hash  no_fields
restricted_hash 14165/s              --           -84%         -85%         -86%
fields_untyped  90334/s            538%             --          -7%          -8%
fields_typed    96863/s            584%             7%           --          -2%
no_fields       98646/s            596%             9%           2%           --
Looks like locking hash keys is expensive... wonder if its better >= 5.009 ..?

code:
bench attribute access ($a = $foo->{bar})...

                     Rate fields_typed fields_untyped restricted_hash  no_fields
fields_typed    1268666/s           --            -6%            -27%       -35%
fields_untyped  1349928/s           6%             --            -23%       -30%
restricted_hash 1743672/s          37%            29%              --       -10%
no_fields       1938101/s          53%            44%             11%         --
Kind of funny that untyped lexicals are faster than typed, because I thought there was opcode-level optimizations when fields is used with a typed lexical. It's repeatably slower by 5-10% though. At least lookups on restricted hv keys is not much slower than a blessed hashref.

<deleted user>

checkeredshawn posted:

I appreciate the advice, Erasmus Darwin, but to clarify, I'm trying to shorten the script. I figured I could find some module that would help me better compare the data, but I've failed to find any such module through cpan.

Shorter? This is only three lines and creates a structure with the comparisons you want...

code:
&nbsp;
    my %compare = %servervalues;
    map { 
        my $ips = $_;
        $_ = {
            'result' => $ips,
            'same' => [ grep { $servervalues{$_} eq $ips } keys(%servervalues) ],
            'unsame' => [ grep { $servervalues{$_} ne $ips } keys(%servervalues) ],
        };
    } @compare{keys(%compare)};
    printf(
        "%s\n -> differs from '%s'\n -> same as '%s'\n", $_,
        join(', ', @{$compare{$_}->{unsame}}),
        join(', ', @{$compare{$_}->{same}})
    ) foreach(keys(%compare));
:buddy:

Adbot
ADBOT LOVES YOU

<deleted user>

TiMBuS posted:

wat
isn't that the same as values(%compare)?

It is. I had @compare{keys(%different_hash)} there before and changed it. But values would be better because it is faster than a slice.

  • Locked thread