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?

Kakesu posted:

:psyduck: directory mania!

For starters, don't make shell calls if there are CPAN modules available to do the same thing. There should definitely be an ActiveState-approved module that creates file paths...

Two, the system function accepts arrays! The first argument to the function is the command you want to run and subsequent arguments are arguments to the command, properly and automatically escaped by Perl.

So, you should try to write out your system call (which you shouldn't be using) like this:
code:
system('mkdir', $dirname);
And you can test if you have the correct number of slashes by just printing $dirname to STDOUT. That or just use forward slashes (no escaping necessary).

Adbot
ADBOT LOVES YOU

Kakesu
Nov 4, 2005

ETHICAL.

Triple Tech posted:

For starters, don't make shell calls if there are CPAN modules available to do the same thing. There should definitely be an ActiveState-approved module that creates file paths...

Two, the system function accepts arrays! The first argument to the function is the command you want to run and subsequent arguments are arguments to the command, properly and automatically escaped by Perl.

So, you should try to write out your system call (which you shouldn't be using) like this:
code:
system('mkdir', $dirname);
And you can test if you have the correct number of slashes by just printing $dirname to STDOUT. That or just use forward slashes (no escaping necessary).

Thanks. Right after I posted that, we found another Perl user that explained that we could just use mkdir on its own, without a shell call, and that seems to be working.

Still no idea why it stopped working, though. The thing I forgot to mention in my original post is that this script has been running continuously for the past 3 months, but this is the first time it's failed like this. Very strange.

ashgromnies
Jun 19, 2004
Anyone know how to use glob?

Here's some code. I have a list of glob-type filenames in @attachments(the test I am running with only contains one, "./smoke_test_*" but it could contain any number of them)

Here's the code:

code:
    foreach my $glob(@attachments){
        warn "gonna glob $glob\n";
        my @files = glob($glob);
        # do stuff on @files, doesn't matter right here
    }
Here's the output:
code:
gonna glob ./smoke_test_*
glob failed (child exited with status -1, core dumped) at <scriptname> line 227, <_GEN_0> chunk 1.
Line 227 of the script is the
code:
my @files = glob($glob);
line so it fails on that for some reason and I can't tell why. $glob obviously = "./smoke_test_*" which should be a valid glob expandable filename, right?

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
You should try a one-liner first, like
code:
my @a = glob('*.pl'); print "@a";
If that doesn't work, well, then you're boned.

ashgromnies
Jun 19, 2004

Triple Tech posted:

You should try a one-liner first, like
code:
my @a = glob('*.pl'); print "@a";
If that doesn't work, well, then you're boned.

Nope, I get
code:
glob failed (child exited with status -1, core dumped)
for the code you posted. WTF does that mean

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
I'm trying to look up more about it, but off the top of my head, I can't help but think that there's something wrong with your perl. What version are you using? And what OS are you running?

You could reimplement the glob concept by just using readdir.

Edit: I think older versions of Perl outsource their globbing to the shell. So if there's something wacky about the environment you're running, that might be it.

ashgromnies
Jun 19, 2004

Triple Tech posted:

I'm trying to look up more about it, but off the top of my head, I can't help but think that there's something wrong with your perl. What version are you using? And what OS are you running?

You could reimplement the glob concept by just using readdir.

Edit: I think older versions of Perl outsource their globbing to the shell. So if there's something wacky about the environment you're running, that might be it.

I am using perl, v5.6.2 built for i686-linux.


EDIT: Removing "use File::Glob;" from my script solved the issue with the glob('*.pl') not working, but I still can't glob a string and it doesn't spew any errors.

code:
    foreach my $glob(@attachments){
        chomp $glob;
        warn "gonna glob \"$glob\"\n";
        my @files = glob("$glob"); print "@files";
    }
results
code:
gonna glob "./smoke_test_*"
EDIT:
removing the quotes in glob("$glob"); fixed it

loving perl

ashgromnies fucked around with this message at 21:35 on Dec 17, 2007

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Are you sure you're in the right directory? Is the one-liner still giving you problems?

heeen
May 14, 2005

CAT NEVER STOPS
by the way, this also works:
code:
perl -e 'print "$_\n" foreach(<*.*> );'

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!

heeen posted:

by the way, this also works:
code:
perl -e 'print "$_\n" foreach(<*.*> );'

Of course, a lot of that is redundant!

code:
perl -le 'print for <*.*>'

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
When you golf like that, it gives non-Perlers a negative impression of the language... :( Golf away!!! :patriot:

Edit: Everyone rejoice! Perl 5.10 is out! Here is the delta. Sounds pretty neat to me.

Triple Tech fucked around with this message at 15:43 on Dec 19, 2007

hk0
Sep 24, 2005

HO HO HO!!!

Triple Tech posted:

Edit: Everyone rejoice! Perl 5.10 is out! Here is the delta. Sounds pretty neat to me.

All right, a switch statement! Perl's a REAL BOY NOW.

No but seriously I'm :derp: and :filez: and compiling this poo poo ASAP so I can just breathe it in, just inhale it -- Aaaaahhh, smell that Perl 6 fragrance.

Twlight
Feb 18, 2005

I brag about getting free drinks from my boss to make myself feel superior
Fun Shoe
hey everyone.

I have a template file that I want to copy, however the new file name will be supplied by the user. How can I get File::Copy to work with me? here is my code

code:
copy ("/location/db.emn-template","/location/<filename supplied by user>");
thanks!

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
I don't understand, is something not working for you? Do you not know how to capture user input? Is it not running?!

Twlight
Feb 18, 2005

I brag about getting free drinks from my boss to make myself feel superior
Fun Shoe

Triple Tech posted:

I don't understand, is something not working for you? Do you not know how to capture user input? Is it not running?!

oh I wasnt sure that it would take a file name as input for the copy command! sorry!

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?

Twlight posted:

oh I wasnt sure that it would take a file name as input for the copy command! sorry!

:psylon: Yes, it takes a filename as the second argument... :confused:
code:
copy $original_file, $destination;
Didn't you read the POD? :cry:

Twlight
Feb 18, 2005

I brag about getting free drinks from my boss to make myself feel superior
Fun Shoe
One more question. I'm writing a perl script to edit the named.conf file, however i need to edit 2 places in the file once in the middle of the file and again at the end (not exactly the end but near the end. How can I tell perl where exactly I want to edit based on this file?

here is hopefully a better explanation:

code:
  };
        zone "zone" in {
                type master;
                file "zone";
        };
<here i would like to add another zone file>
};
How is this done best?

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
All things in software are possible... :pseudo:

If you can promise that everything is split up by lines nicely, you can just loop through each line in the file, analyze where you want to stop, inject your new stuff, and then keep printing.

A better, "more right", more complicated way would be to have a script interpret and parse the entire conf file, modify the Perl object that represents it, and then serialize it again... That would be the most flawless way.

Twlight
Feb 18, 2005

I brag about getting free drinks from my boss to make myself feel superior
Fun Shoe

Triple Tech posted:

All things in software are possible... :pseudo:

If you can promise that everything is split up by lines nicely, you can just loop through each line in the file, analyze where you want to stop, inject your new stuff, and then keep printing.

A better, "more right", more complicated way would be to have a script interpret and parse the entire conf file, modify the Perl object that represents it, and then serialize it again... That would be the most flawless way.

the file follows this convention:
code:
   zone "zone" in {
                type master;
                file "zone";
        }; 
   zone "zone" in {
                type master;
                file "zone";
        }; 
}; <--- this is the end of the section I want to edit
and so on and so forth.
I want to inject a return at the second to last }; and add my input so I would dump the contents of the file into an array, then I get lost :(

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?

Twlight posted:

I want to inject a return at the second to last }; and add my input so I would dump the contents of the file into an array, then I get lost :(

Right, so you load up the original conf file and you loop through all of the lines. You analyze a line and decide what to do with it. If do nothing, then just tack it back on to a buffer (another scalar). You're essentially reconstructing the file.

At some point, your testing mechanism will pass, telling you that you're at that particular point in the file. Before you push the original content back to the scalar buffer, you inject your content by pushing it on first. Then push the original.

Now you have a new file represented in Perl-space. Close the file for reading, then reopen it for writing, write out your buffer, and blamo, original file + injection complete.

Twlight
Feb 18, 2005

I brag about getting free drinks from my boss to make myself feel superior
Fun Shoe

Triple Tech posted:

Right, so you load up the original conf file and you loop through all of the lines. You analyze a line and decide what to do with it. If do nothing, then just tack it back on to a buffer (another scalar). You're essentially reconstructing the file.

At some point, your testing mechanism will pass, telling you that you're at that particular point in the file. Before you push the original content back to the scalar buffer, you inject your content by pushing it on first. Then push the original.

Now you have a new file represented in Perl-space. Close the file for reading, then reopen it for writing, write out your buffer, and blamo, original file + injection complete.

so something like this?
code:
while(FILE){
$line = $_;

if($line =~m/ the location where im looking to add text/){
print OUTFILE (beast of a print line)
else{
print OUTFILE $line
}}

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Aren't your in and out file the same? If so, you would need to open, and then close. If not I guess your way with two filehandles is the same.
code:
my $buffer;
open CONF, $conf_file;
while (my $line = <CONF>) {
  if (/};/) {
    $buffer .= EOZ;
  zone "danger_zone" in {
    type master;
    file "danger_zone.txt";
  }; 
EOZ
  }

  # occurs unconditionally
  $buffer .= $line;
}
close CONF;

open CONF ">$conf_file";
print CONF $buffer;
close CONF;

Nevergirls
Jul 4, 2004

It's not right living this way, not letting others know what's true and what's false.

Triple Tech posted:

A better, "more right", more complicated way would be to have a script interpret and parse the entire conf file, modify the Perl object that represents it, and then serialize it again... That would be the most flawless way.

The Perl way is to have someone else do the heavy lifting for you.

Twlight
Feb 18, 2005

I brag about getting free drinks from my boss to make myself feel superior
Fun Shoe

wntd posted:

The Perl way is to have someone else do the heavy lifting for you.

I've only just started using perl, I know thats from CPAN and I know how to install things from CPAN, but I have no idea how this works.

ashgromnies
Jun 19, 2004

Twlight posted:

I've only just started using perl, I know thats from CPAN and I know how to install things from CPAN, but I have no idea how this works.

What don't you understand exactly? The perldoc is pretty explanatory...

You configure various handlers for when the parser finds different things(like the start of a config block, end of a config block, etc.) then tell it to parse a file and it will run the custom callback handlers you created when it needs to.

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?

ashgromnies posted:

What don't you understand exactly? The perldoc is pretty explanatory...

To someone that doesn't know Perl that well or knows it to varying degrees, perldoc isn't some godlike, infallible tutorial on all things...

Besides, this module just spits out events as it parses a file. I don't see how this is at all relevant to the original problem of modifying the config by injecting another paragraph.

...unless you want to use the stream of events to detect and reconstruct parts of the object, and then inject things that way... Which I wouldn't be infavor of because that just sounds so indirect, compared to my way. :colbert:

Twlight
Feb 18, 2005

I brag about getting free drinks from my boss to make myself feel superior
Fun Shoe
I figured it out :)

What I did was add a comment to the conf file I was trying to add data to, then I did a find/replace on that comment. The replacement would also re-add the comment below the new information making it ready for the next entry.

TiMBuS
Sep 25, 2007

LOL WUT?

You should have a look at Tie::File some time.
Lets you modify a file in-place, maps the lines to an array.
Pretty good, I use it often.

E: ONLY A DAY AND A HALF LATE

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Did you ever hear anyone say "design your modules like you would release them on CPAN"? Is there any part of that advice that's negative?

I've also been thinking of creating another movement... All the companies I've been at store their utility functions (example, for text munging) in Company::Utility::Text. And it's started to bother me. Why not design it like you'd really release it on CPAN, so it goes under Text::Something. I guess that ties into the first question... Like, I want to keep real business logic seperate from fake business logic. Is that so bad?

Although I do see the argument of "well, it's a module we developed internally, so we have to put it in our own namespace". True, but sometimes you create things that aren't really proprietary, or could easily be shared with the world.

Fenderbender
Oct 10, 2003

You have the right to remain silent.

Triple Tech posted:

Although I do see the argument of "well, it's a module we developed internally, so we have to put it in our own namespace". True, but sometimes you create things that aren't really proprietary, or could easily be shared with the world.

Most any application/module/plugin developed at a company is going to be an IP, even small things like a slightly more specific text parser. Everywhere I've worked has used their own namespace, and even namespaces specific to each individual group. It just adds one more layer of structure, in my opinion, and I see no reason to be rid of the practice.

ashgromnies
Jun 19, 2004

Fenderbender posted:

Most any application/module/plugin developed at a company is going to be an IP, even small things like a slightly more specific text parser. Everywhere I've worked has used their own namespace, and even namespaces specific to each individual group. It just adds one more layer of structure, in my opinion, and I see no reason to be rid of the practice.

Plus it keeps knowledge sharing a little easier - if you have a namespace specifically for the company, you know where to look for modules related to company or product-specific business logic.

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?

ashgromnies posted:

Plus it keeps knowledge sharing a little easier - if you have a namespace specifically for the company, you know where to look for modules related to company or product-specific business logic.

But that's the point of my post. The code in question isn't specific to the company, at all. It could be used by other people. It's like if Amazon made an ISBN parser... Where ISBNs aren't unique to Amazon the company. Instead of putting it in Amazon::ISBN, it should be in ISBN::Parser, as if it were a public facing module.

TiMBuS
Sep 25, 2007

LOL WUT?

Triple Tech posted:

But that's the point of my post. The code in question isn't specific to the company, at all. It could be used by other people. It's like if Amazon made an ISBN parser... Where ISBNs aren't unique to Amazon the company. Instead of putting it in Amazon::ISBN, it should be in ISBN::Parser, as if it were a public facing module.

Personally if this was a public module and Amazon made it, I wouldn't hold a grudge if it was released on CPAN under the name Amazon::ISBN. On the other hand I would also understand if they named it ISBN::Parser since it's general-purpose...
I guess what I'm trying to say is.. eh. It shouldn't really matter should it?

mdxi
Mar 13, 2006

to JERK OFF is to be close to GOD... only with SPURTING

TiMBuS posted:

Personally if this was a public module and Amazon made it, I wouldn't hold a grudge if it was released on CPAN under the name Amazon::ISBN. On the other hand I would also understand if they named it ISBN::Parser since it's general-purpose...
I guess what I'm trying to say is.. eh. It shouldn't really matter should it?

Exactly. While arguments could be made for module names (which is to say: CPAN hierarchies) being meanigful or not being meaningful, the truth is that they are not. There is no CPAN-police enforcing any sort of naming scheme or quality control standards (although the perl-qa project evaluates all modules uploaded to the CPAN for "kwalitee", which is a metric of how well-formed a module is according to current standards).

search.cpan.org is your friend (as are cpanratings and annocpan (both integrated into search.cpan)).

awesomepanda
Dec 26, 2005

The good life, as i concieve it, is a happy life.
I have perl 5.10 downloaded and extracted on to my desktop. I have no idea how to use it. I downloaded activeperl before and i was able to "install it" and run basic script on my computer. Can anyone show me how to get started with perl 5.10? The book programming perl i got and a lot of the tutorials on line never touch up on this fact.

awesomepanda fucked around with this message at 03:00 on Jan 8, 2008

mdxi
Mar 13, 2006

to JERK OFF is to be close to GOD... only with SPURTING

CrazyPanda posted:

I have perl 5.10 downloaded and extracted on to my desktop. I have no idea how to use it. I downloaded activeperl before and i was able to "install it" and run basic script on my computer. Can anyone show me how to get started with perl 5.10? The book programming perl i got and a lot of the tutorials on line never touch up on this fact.

I can't say for sure, but I think what you might have there is a source tarball, since you seem to indicate that it's not ActivePerl. You might be interested in Strawberry Perl, which is created and maintained by one of the Perl community's most dedicated hackers.

This still won't tell you exactly how to get going though. Personally, I'd recommend a copy of the Llama book (Beginning Perl), and I'd recommend that you skip chapter 1. If you already have references on hand, just realize that Strawberry Perl (or a Perl you build yourself) is not an IDE, it's just a language interpreter, the core modules, and some helper tools like 'cpan', which will fetch new modules for you from the CPAN -- the expectation is that you're going to write scripts in the editor of your choice and run them.

Sorry if any of this is too basic or redundant; I have no idea about your skill level, and I'm not a Windows user, so I can only speak to the universalities of the language.

Finally, since you're asking about Perl 5.10, you might wanna check out this pretty sweet little piece of work: Perl 5.10 For People Who Aren't Totally Insane, a deep but generally followable introduction to the new bits in 5.10

ashgromnies
Jun 19, 2004

mdxi posted:

I can't say for sure, but I think what you might have there is a source tarball, since you seem to indicate that it's not ActivePerl. You might be interested in Strawberry Perl, which is created and maintained by one of the Perl community's most dedicated hackers.

I find it much easier to install Cygwin with the perl runtimes and your editor of choice(vi is pretty nice for perl). Windows-native perl is kind of difficult to work with due to the lack of a strong command-line.

mdxi
Mar 13, 2006

to JERK OFF is to be close to GOD... only with SPURTING

ashgromnies posted:

I find it much easier to install Cygwin with the perl runtimes and your editor of choice(vi is pretty nice for perl). Windows-native perl is kind of difficult to work with due to the lack of a strong command-line.

That's exactly the purpose of the Strawberry Perl distro; it obviates the need to install Cygwin, or anything else. It's a fully self-contained Perl for Windows :)

Hmmm. Rereading your comment, I realize that you might be talking about perl-as-system-glue and not simply perl-the-language, in which case I'm sure you would want Cygwin as well. But if you want Just Perl, you don't need to fiddle with Cygwin installs anymore. I just wanted to point out that Strawberry Perl can act as a standalone distro because there has been some confusion about that issue in the past week as people's interest has been piqued due to the 5.10 accouncements.

ashgromnies
Jun 19, 2004

mdxi posted:

That's exactly the purpose of the Strawberry Perl distro; it obviates the need to install Cygwin, or anything else. It's a fully self-contained Perl for Windows :)

Hmmm. Rereading your comment, I realize that you might be talking about perl-as-system-glue and not simply perl-the-language, in which case I'm sure you would want Cygwin as well. But if you want Just Perl, you don't need to fiddle with Cygwin installs anymore. I just wanted to point out that Strawberry Perl can act as a standalone distro because there has been some confusion about that issue in the past week as people's interest has been piqued due to the 5.10 accouncements.

I just think it's much easier to deal with perl when you have a nice command line to fall back on.

Adbot
ADBOT LOVES YOU

TiMBuS
Sep 25, 2007

LOL WUT?

I use ubuntu. It's my fav distro and you cant change my mind >=(
However, it relies on the perl package provided for a lot of programs, so I can't uninstall it and manually build and install perl 5.10
I can only guess how long ubuntu will take to upgrade the perl package, but it probably won't be soon because they like to quadruple check the drat stuff they release has no bugs, plus they'll have to upgrade and repack all of their current packaged perl modules as well.

So, wat du I do? The only solution I can think of is making my own package and overriding the ubuntu one, but who knows how much stuff I could break doing that.. Maybe I should just wait?

  • Locked thread