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
Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Hughmoris posted:

After trying to understand referencing/dereferencing a little more (I still don't really get it)
All a reference is is a pointer to a value. To access or manipulate that value and its members, you have to dereference, using the infix operator (->) or the appropriate sigil.

Hughmoris posted:

I'm using Strawberry Perl, v5.20.1
Is Perl on Windows any less miserable than I remember?

Adbot
ADBOT LOVES YOU

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Ephphatha posted:

Is there a "say" equivalent that prints to STDERR? I've got a few scripts that I print warnings out to highlight bad data (so there's no need for line numbers) and remembering to put \n on the end of each string is a drag.
say STDERR "bloobledebloop";

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

This should definitely go in the Coding Horrors thread.

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Salt Fish posted:

Im bad but you could wget or curl and pipe the output into your script as standard in via a cron.
That's the easy part.

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Hughmoris posted:

Thanks. I don't know if I'll even get that far as I can't successfully install Dancer2. On ubuntu, using the command "CPAN install dancer2" will do some processes and eventually display "Running Build install
make test had returned bad status, won't install without force" before stopping the install.

I then tried "cpan force install dancer2" and "cpan fforce install dancer2" and they both ended with the same result and same error message. Not sure where to go from here.
Which test failed?

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Hughmoris posted:

Lots and lots.
This is never the right answer.

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Hughmoris posted:

I'm pretty unfamiliar with how modules are installed but it seemed like everything failed. Unfortunately I didn't save a copy of the log before I fixed the problem so I can only give vague answers
That's just some general advice. When you have the actual error messages on hand, you get answers faster.

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Mithaldu posted:

Just in case Blotto's post didn't make it more clear (though i'm unclear how saying "it's an alias" is not clear enough): The advantage to for is four letters less to type AND less to read for developers who come after you, since aside from the letters "each", for and foreach are literally the same thing. They're different names that result in identical code.
Which doesn't really strike me as being a very meaningful advantage.

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Hughmoris posted:

Well, I may have spoke too soon. I think I'm getting hung up on dereferencing. I have a block of code that looks something like this:
Perl code:
my $doc = $view->GetFirstDocument;
$doc->{Scope}->[0];
$doc->{Delegate}->[0];
$doc->{LastAct}->[0];
and it is outputting this:
code:
Small
Sarah
Win32::OLE::Variant=SCALAR(0x3d5ef80)
I'm not positive how to derefence $doc->{LastAct}->[0] further and get to the value. The first two items are strings, and the problem item is a date/time stamp if that makes any difference. Any ideas?
code:
$doc->{LastAct}->[0]->Value()
should do it.

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Mithaldu posted:

You mean:
code:
$doc->{LastAct}[0]->Value
Seriously, at least try and give newbies examples in idiomatic Perl. :(
Ok.
code:
 *{ref($doc->{LastAct}[0])."::".(grep {/value/i} keys %{ref($doc->{LastAct}[0])."::"})[0]}->();

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Mithaldu posted:

A true perler would've known to use a comma there, i'm going to have to confiscate your card.
code:
use MIME::Base64;
print join(" ",(grep {!/[0-9]/} map {decode_base64($_)} qw(dXI= Y29tbWFz MTIzNGR3ZQ== cDAwcA== Y2Fu MHhmZjk5YWEzMzQ0 NjY2 c3Vjaw== NTQ1NjNoZ3JyZjMy aXQ=)),"\n");

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Mithaldu posted:

A true perler also has a sense of humor.
You kind of have to, if being a True Perler is important to you.

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

welcome to hell posted:

Even among the IRC crowd you aren't going to see a lot of consistency on style preferences for minor things like this.
When it comes to people who hang out on IRC talking about their programming language of choice, minor is sort of relative.

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Hughmoris posted:

Speaking of barcodes, does anyone have experience using Perl to generate barcodes? I'm tackling a project at work where the goal is to take a text file that contains a Name and Account Number, and to generate a tiff image that contains the Name, the Account Number, and a barcode (generated from the Account Number).

I'm 99% sure the barcode standard for this is Code39.
Something like this should do the trick:
code:
use Image::Magick;
use Data::Dumper;
use strict;
my $image = Image::Magick->new();
$image->ReadImage("canvas:white");

my %barcode = (
	font=>"free3of9.ttf",
	pointsize=>64,
	text=>"5563844",
	fill=>"black",
);
my %label = (
	pointsize=>12,
	text=>"Fred MacMurray - 5563844",
	fill=>"xc:#999999",
);

my $barcodeMetrics = getMetrics($image,%barcode);
my $labelMetrics = getMetrics($image,%label);
my ($imageWidth,$imageHeight) = ($barcodeMetrics->{width}+10,$barcodeMetrics->{height}+$labelMetrics->{height}+10);
$image->Resize(geometry=>"${imageWidth}x${imageHeight}!");
$image->Annotate(%barcode,x=>5,y=>5+$barcodeMetrics->{yOffset});
$image->Annotate(%label,x=>($imageWidth/2)-($labelMetrics->{width}/2),y=>$barcodeMetrics->{height}+10+$labelMetrics->{yOffset});
$image->Write("test.tiff");

sub getMetrics {
	my ($image,%options) = @_;

	my @metrics = $image->QueryFontMetrics(%options);
	return {
		width=>$metrics[4],
		height=>$metrics[5],
		yOffset=>$metrics[2],
	};
}
You can download the code39 font a few places.

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Hughmoris posted:

Thanks, the PDF::ReportWriter looks promising.


Wow, thank you for taking the time to write that up. I'm going to give it a shot and I'll let you know how it turns out.
No problem. I used to do quite a bit of work with ImageMagick, so it wasn't much of a stretch.

If ReportWriter doesn't quite fit your needs, it's PDF::API2 does all the heavy lifting and allows for more granularity. Keep in mind, though, that in PDFs, 0,0 is the bottom left. Also, you'll probably have to factor in the ascender/descender of the font. PDF can be a pain in the rear end.

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Hughmoris posted:

I'm on the tail end of a work project, and am a bit stumped on what should be the easiest part.

At the end of my program, I end up with a .tif file. I want to transfer that .tif file to a \\server\filepath. What is a secure way to provide my script a \\server\filepath in addition to the username and password for the server, then securely transfer the .tif file?

I'm learning Perl as I go, so I may be missing something simple here.

*Perhaps something like Net::SCP::Expect?
Is SSH running on the target machine?

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Hughmoris posted:

I don't have control of the target machine but I'm being told no, it doesn't have SSH. I want to say it runs some version of Windows Server, and the Perl script will be running on a Windows 7 computer running Strawberry Perl.
Not really a windows guy, but try this: http://migo.sixbit.org/software/smb-perl/

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Mithaldu posted:

(in particular Ruby's OO is extremely barebones compared to Perl's)
I have a few issues with ruby, but its OO being barebones isn't one of them.
code:
puts "lol".class.superclass.new.methods.inspect
puts "lol".class.superclass.methods.inspect
Now if that's possible in core perl, I'd like to know how.

Extortionist posted:

Perl's flexibility is a huge handicap against learning (yes, I am arguing that the issue here is something inherent to perl). I don't think it has much to do with the laziness you imply, so much as it has to do with the difficulty of learning various concepts and perl's willingness to let you put together something that entirely contradicts best practices but also solves the problem you're trying to solve (this is probably also one of perl's biggest strengths--just not so much when it comes to learning). I deal with the consequences of this too often in my daily work to assume that someone trying to learn something blindly in such a permissive language will magically fall into best practices. Surely someone can do it if they're smart and diligent enough--but probably they'd also have an easier time of it if the language didn't fool them about what they were doing.

I will stand by my assertion that perl's OO support is hacked. The core language plainly isn't designed to properly support OO, and you can easily blow apart any OO code in perl intentionally or accidentally.
Exactly. "There's more than one way to do it," in practice, often translates to "Here, have some more rope."

While I've coded in perl for about 17 years, it's not a good starter language.

[edit] Perl 6 :laffo:
[edit2] Mithaldu, are you that "true perler" guy from a few months back?

Ellie Crabcakes fucked around with this message at 08:17 on Feb 23, 2016

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Mithaldu posted:

Particularly attribute and constructor declaration in Ruby are barely more detailed than those in core Perl.

Can you do this in Ruby at all?
code:
package RoleX {
    use Moo::Role;
    use Types::Standard "Int";
     
    has x => ( is => "rw", isa => Int, required => 0, default => sub { 5 } );
}

package Point {
    use Moo;
    with "RoleX";
    has "+x" => ( required => 1 );
}

my $o = Point->new( x => 4 );
And yeah, i don't care if you have to call in libraries or whatever, i just want to know if, in Ruby, you can have the syntax to declare attributes in a parametrized and compartmentalized manner, meaning:

- the constructor expects named arguments for the attributes
- setters/getters are created automatically, with the "is" arg ( ro/rw/rwp/lazy ) determining the exact set of them created
- type checks of some sort can be declared quickly and are baked into the constructor
- "required" status of attributes can be declared and are baked into the constructor
- "default" values can be provided in the attribute declaration and baked into the constructor
- the whole thing can be bundled up and re-used as one whole thing in many other classes
- the exact specification of an attribute pulled in from somewhere else can be modified in the target class

How many of these points can Ruby provide?
This needs clarification. What exactly is "that"?
a) The syntax? No, you need to implement at least some of that. (Although you could possibly hack that together by attaching things to UNIVERSAL.)
b) The functionality those methods do? Absolutely, i could hack you a quick example if you like.
c) The functionality applied on a string? No, strings are not objects in Perl (by default, but then we go back to b).
John, are you the "reacts to a joke with direct insults" guy from a few months back?
Perhaps I should have been more specific and said "without having to import a single class or module"

Because perl just can't do it off of a base install. If it could, Moo and Moose and Class::C3 and all of the other bolt-ons would be totally unnecessary.

Now if you're not actually a ridiculous perl bigot, I apologize, but you kinda come off as one. Especially when you say a from-the-ground-up OO language is barebones compared to perl.

Do you have any experience with any actual OO language?

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Seems like a good learning project. I'd recommend AnyEvent for listening to the stream.

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

octobernight posted:

I'm working with large 200-500 Gb text files, and I want to find lines that match a pre-defined list line ids and return the next line following the match. Here's my starting code:

code:
my %ids = ("foo",undef, "bar",undef, "baz",undef );
open(INPUT, "big.txt");
while (my $line = <INPUT>) {
	my $next = <INPUT>;
	if (defined $ids{$line}) {
		$ids{$line} = $next;
	}
}
When I run this code, according to iostat, my bottleneck is CPU (roughly 80-90% cpu is user, only 0.10% is iowait). This makes sense because the system's bandwidth is 200-300 Gb/s so I/O shouldn't be an issue. My machine has 16 cpus, so I suspect that if I run 16 threads, each reading at a different chunk of data, it would greatly speed up this code. My idea would be to have each thread seek into 16 positions into the file and start processing from there. I haven't worked out the details (need to figure out what happens when thread doesn't start in the right location as the positions are just estimates), but I wanted to know what other people thought, and whether they had any alternative suggestions?
This code won''t work.

1) It won't match the ids unless you chomp the line first.
2)defined $ids{foo} is always going to return false because the value of $id{foo} is set to undef. What you want is exists $id{foo}

Unless the id line and the content line have fixed widths--and I imagine they do not--then threads aren't going to help. First off, you won't know how many threads to spawn because you have to read through the entire file to get a line count. Then, each thread is going to have to read lines and throw them away until they get to their starting point.

Adbot
ADBOT LOVES YOU

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

octobernight posted:

Thanks! I meant to use exists, not defined, and I left out the code that trims the lines so that all whitespace is removed from the ends. I wasn't planning on start exactly at a particular line, rather I was going to jump to XXXX byte location for each thread. I would then throw away whatever line it read it and find the first complete line that has the line start identifier (in my file, the id format is $id). It was something off the top of my head, though.
With a bit of massaging, that could be a workable approach.

Instead of having the threads jump in at a random location and discard lines, stat the fil and, divide the size by the number of threads. Seek to $chunksize*$i, read until $ preceded by a newline. Feed those offsets into the worker threads and you won't have to discard any lines and the almost-guaranteed mangled data resulting therefrom.

That said, this whole setup seems a lot less than ideal. What sort of data is in these files and how are they generated? What happens to the data pulled out?

  • Locked thread