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
Subotai
Jan 24, 2004

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

Adbot
ADBOT LOVES YOU

Subotai
Jan 24, 2004

Triple Tech posted:

Two things. One, it sounds like what you're asking for is to output to a file as soon as possible. That is achieved by the following:

code:
$|++;
I'm fully aware that it looks crazy, but that's the way most Perl programmers write it. It sets the output autoflush variable to true. That means every time something gets sent to a filehandle, it will definitely show up. I don't think a handle needs to be closed to be written to this way, that's what I'm banking on.


Why would you ever want to increment this instead of setting it to 1? Unless you are sharing it across threads or something and it is being used in the same manner a semaphore or something weird like that, you should just set it to 1.

Subotai
Jan 24, 2004

I have read a ton of Perl books and gone through lots of Perl code and have NEVER seen anyone increment it. I think it is bad form and confusing. :colbert:

Subotai
Jan 24, 2004

scrabbleship posted:

I am writing a quick perl script to find all instances of "size" in a file and then delete that line. The problem is my regular expression also catches other lines where it has "size" but inside another word. For example, I want it to get rid of the line "Size: 4234" but not "Install-Size: 34234".
Here is the code I have been working on:
code:
open(infile,"generic");
open(outfile,"<genericNew");
while(<infile>)
{
	if($_ !~ /(size)/i)
	{
		print outfile "$_";
	}
	
}
close(infile);
close(outfile);
Is there anyway I can catch one with a regular expression and not the other?


Well this will help a little:
if(/(^|\s)size/i)

Just see if there is a space in front of size or if it is at the beginning of the line.

Subotai
Jan 24, 2004

Cheesus posted:

What's the best way to interface with a C++ library from Perl?

And once I'm there, what's the best way to return a C++ map as a hash?

I've been reading up on swig and while it seems pretty easy to call functions, I'm having a difficult time understanding how to return complex, dynamic structures.

The most efficient way would be to use XS. It not not that easy to use though. A lot of Perl modules use it to interface with C++.

Subotai
Jan 24, 2004

genericadmin posted:

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

I've done some in XS. It was like a year and a half ago though.

Subotai
Jan 24, 2004

Triple Tech posted:

Using parenthesis for built-ins is not good practice. In fact, the PBP is to not use parenthesis for built-ins.

Where are you getting this??

Subotai
Jan 24, 2004

Yeah I have never heard of anyone saying you should never use them. You should listen to Randal Schwartz over Damian Conway any day. *shakes fist at Damian Conway*

Subotai
Jan 24, 2004

Triple Tech posted:

Well it's getting rid of unneeded parenthesis. Of course you need them for some things. I just don't like reading this, ever:

code:
open(my($fh), shift(@ARGV)) or die("oh noes, gonna die");


You should use them where it makes sense to. To say "never use them" is dumb.

I personally would write that as:
open(FILE, shift) or die "blah";

But to each his own.

Subotai
Jan 24, 2004

checkeredshawn posted:

It's a really long flat text file containing 26 fields of information for each object.

Why dont you open the file and read the contents instead of accepting them on the command line?

Subotai
Jan 24, 2004

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?

INIT is the first thing to get called after compilation (except BEGIN blocks or 'use' statements, etc). The main reason to use it is to make sure you have certain code executed before anything else. You can think of it as a constructor I guess. I have never heard of it being used to find errors. Maybe he is thinking of eval?

*edit* typos

Subotai fucked around with this message at 17:34 on Aug 14, 2008

Subotai
Jan 24, 2004

Triple Tech posted:

I've never heard of it being used for anything ever. What's a really good, typical example of someone using an INIT that can't just be in main before the subroutines?


It could be considered cleaner I suppose. I dunno, ask your co-worker to explain himself.

Subotai
Jan 24, 2004

more falafel please posted:

I've never been much of a Perl expert, but what's the philosophical reason behind Perl flattening lists?

I think it was just a mistake. I am sure you can find Larry Wall talking about it somewhere on the net.

Subotai
Jan 24, 2004

Triple Tech posted:

Could someone please explain what a lexical $_ is used for and how to use it?

perldoc

It is used the same way you would use the global version. It just means that you can now change $_ in a certain scope and not affect the global version.

Subotai
Jan 24, 2004

Triple Tech posted:

Let's say you make a really simple class (it never stays that way does it :)). Do you bother implementing getters for private use or do you just access the object's innards directly? That is, should you be allowed to know how you work? Or should the same amount of care and abstraction be taken as if it was publicly consumed?

There is no need to try and abstract the internal data members of a class from itself.

Subotai
Jan 24, 2004

Sartak posted:

Your class will get more complicated. Having layers of abstraction will ease that evolution.


I for one find it much more of a pain in the rear end to have to write a function to access a data member in a class that I already have access to. Unless you really think you might have to go back and redo how or the information in the class is stored or handled, and it is too much work to actually change the hash in the places you use it, then go ahead and write an accessor function. I believe it mainly will be a waste of time, involve extra code that doesn't need to be there, and make the code harder to debug.

He is talking about a simple class in Perl. It shouldn't be something you turn into a monolithic piece of code. He probably shouldn't have any use for accssesor functions internally.

Adbot
ADBOT LOVES YOU

Subotai
Jan 24, 2004

Mithaldu posted:

Little performance question:

This runs in 5 seconds:
code:
use constant ONE => 1;
use constant ZERO => 0;
use constant TWO => 2;

my $a;

my @test = (4,2,3);
 
for (0..10000000) {
    $test[ONE]++;
    $test[ZERO]++;
    $test[TWO]++;
    $test[ONE]--;
    $test[ZERO]--;
    $test[TWO]--;
    $a = $test[ONE];
    $a = $test[ZERO];
    $a = $test[TWO];
}

This runs in 14 seconds:
code:
my $a;
  
my %test = (
            ONE =>4,
            ZERO => 2,
            TWO => 3
           );
 
for (0..10000000) {
    $test{ONE}++;
    $test{ZERO}++;
    $test{TWO}++;
    $test{ONE}--;
    $test{ZERO}--;
    $test{TWO}--;
    $a = $test{ONE};
    $a = $test{ZERO};
    $a = $test{TWO};
}
Is that normal or am i overlooking something?


Both are O(1) but the hash will have more overhead since it has to do some extra math to find the hash bucket.

  • Locked thread