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
Mithaldu
Sep 25, 2007

Let's cuddle. :3:

TheHeadSage posted:

I've been given the task to write the new perl DB Backup management script at work, as clients now want database backups every half hour or 15 minutes instead of the 1 hour we had beforehand. Now, there are about 30 websites on the server, each with a db size of about 300mb.

Set up another server with whatever DB you're using and set the first one up to replicate to the second one (google for guides). That way you get a backup that's constantly up-to-date and can be switched over to with a minimum of effort in case the first one shits the bed.

Using a Perl script to backup a 300MB database every 15 minutes is silly.

Adbot
ADBOT LOVES YOU

S133460
Mar 17, 2008
hello
Also have a look at XtraBackup from Percona.

Roseo
Jun 1, 2000
Forum Veteran
Does anyone know of any sockets wrapper that will handle retries, timeouts, etc, and just let me pass a couple of subroutine references to use to encode the outgoing transmissions with and use to determine how many bytes to read and decode the incoming transmissions? I've got a client module that works when the network conditions are fine but is running into issues with packetloss, etc.

Ideally, I'd like something I can use or subclass that I can give a string, let it handle the details of making sure the transmission/reception of response is fault-tolerant, and give me the resulting string back.

Alternately, I'm trawling the web for decent references in what possible error conditions and states that I need to handle using IO::Socket::INET there are. If anyone knows of decent resources alone these lines it'd be much appreciated.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
I used AUTOLOAD for the first time today. Yesterday I used magic goto.




I feel dirty :ohdear:

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
Which goto form is magic? You mean goto EXPRESSION which calculates the label to jump to, right?

If so I'd like to see that code. I've never seen that beast in the wild.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
goto &function

It's somewhat similar to the usage in Help.pm



e: basically I'm writing some code to eliminate boilerplate in cheesebot, the git repo of which I'm puting up soonish

Blotto Skorzany fucked around with this message at 04:19 on Oct 13, 2009

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
Oh, pfft, do not feel dirty about goto CODEREF. It's just tail call optimization.

AUTOLOAD though? Boy, you so nasty.

Mark Kidd
Feb 15, 2006
Aside from a CS course several years ago taught in perl I have never had any particular reason to use the language, and I probably won't be using it much in the future. I am stuck on a small issue in modifying Plans Calendar, and I think (assume?) this will be very easy for anyone who works in the language.

Plans has a very useful feature where it will generate a list of upcoming events for the next X days. The annoying thing is that it does not provide a parameter to limit the number of upcoming events; for the template I am developing I would like to have up to Y events for the X upcoming days.

So far I have been able to pass the script a new parameter and have set up a foreach loop which will break when Y events have been reached. My trouble is that I need to sort the array of events by date before limiting it to Y in number so that it returns the first Y events rather than Y events in the order they were added to the database.

Here's the code as it stands right now, where I get $results back with Y events but those events are not in chronological order:

code:
  $results .= "date_format = '$date_format';\n";

  my %all_calendar_ids;

  foreach $calendar_id (@cal_ids) {
    next if ($calendars{$calendar_id}{id} eq "");

    foreach $local_background_calendar_id (keys %{$calendars{$calendar_id}{local_background_calendars}}) {
      $all_calendar_ids{$local_background_calendar_id} = 1;
    }
    $all_calendar_ids{$calendar_id} = 1;
  }
  
  foreach $calendar_id (keys %all_calendar_ids) {
    $results .= "calendars['$calendar_id'] = new Calendar({".&calendar2javascript($calendars{$calendar_id})."});\n";
  }

  my @temp;
  if ($background_calendars_mode ne "none") {
    @temp = keys %all_calendar_ids;
  } else {
    @temp = @cal_ids;
  }
  
  &load_events($start, $end, \@temp);
  &normalize_timezone;

  $results .= "all_calendars = new Array('".(join "','", keys %all_calendar_ids)."');\n";

	my $event_count_iterator = 0;

	foreach $event_id (keys %events) {
		if($event_count_iterator >= $maximum_events) {
			last;
		}
		$event_count_iterator++;

		$results .= "upcoming_events['$event_id'] = new Event({".&event2javascript($events{$event_id})."});\n";
		$results .= "upcoming_events_order.push('$event_id'); \n";
	}

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
Eurgh, that's really terrible Perl. The snippet you posted doesn't point to any obvious date attributes, so it's kinda hard to tell you how to modify it to sort by them.

Mark Kidd
Feb 15, 2006

atomicstack posted:

Eurgh, that's really terrible Perl. The snippet you posted doesn't point to any obvious date attributes, so it's kinda hard to tell you how to modify it to sort by them.

Ouch, not what I was hoping to hear. But a thought-provoking comment nonetheless. I am digging around in the source and I see now that the sorting is done client-side in javascript. I still think that it would be better to sort and limit the output on the server side but I'd be satisfied to get this working correctly for the time being. Maybe it's going to be harder than I thought though.

I'm working through that JS code right now to find some meaningful excerpts and it is pretty hard to summarize the presentation logic. Maybe that is just Plans being consistent in being squirrelly.

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
I was going to suggest checking the load_events sub to see if you could modify the query string used to retrieve events from the database, but then I took a look at some of the code from the backend and decided to go do something else

Mark Kidd
Feb 15, 2006

atomicstack posted:

I was going to suggest checking the load_events sub to see if you could modify the query string used to retrieve events from the database, but then I took a look at some of the code from the backend and decided to go do something else

It is probably not pertinent to mention that I am using 7.10 (the new version 8 still seems a little wobbly).

I am sad that it seems like Plans is not the answer to my situation. It has not been easy to find a calendar that I can just plug in to whatever project I am working on. I have used a couple in the past, and none of them just work the way Plans (almost) does for my circumstances.

I still have this notion that if I can isolate the sorting logic in the JS maybe there is a way to modify the script to do this one simple thing. Thanks for the effort so far though.

Mithaldu
Sep 25, 2007

Let's cuddle. :3:
Here's the deal: You can very rapidly asses the quality of any Perl code by looking at the first few lines. Do you see "use strict;" and possibly "use warnings;" anywhere? If not, it's poo poo code. This whole thing might as well be in PHP right now.

As such you really have only two paths: 1. Learn Perl and clean it up (using Perl Best Practices as guidance anyone should be able to) 2. Pay someone else to clean it up.

Mark Kidd
Feb 15, 2006

Mithaldu posted:

Here's the deal: You can very rapidly asses the quality of any Perl code by looking at the first few lines. Do you see "use strict;" and possibly "use warnings;" anywhere? If not, it's poo poo code. This whole thing might as well be in PHP right now.

As such you really have only two paths: 1. Learn Perl and clean it up (using Perl Best Practices as guidance anyone should be able to) 2. Pay someone else to clean it up.


Fair enough. I am compelled to point out the irony that I want to use this script to integrate with a PHP CMS.

If I wasn't working full time I'd be inclined to learn perl and clean it up. On the other hand, I'm interested in using Plans in my workplace, so I'd be willing to entertain any proposals (in this thread or via PM) on how much time billable time it would take one of you fine folks to clean it up on my behalf.

Unless of course we are talking about adding "use strict;" to the top and then spending a couple hundred hours rebuilding the whole thing. That would be outside my financial means. (Please feel free to do so as a volunteer though -- they evidently could use a hand. I'll beta test!)

Mark Kidd
Feb 15, 2006
It makes me a little bit sad, but I went ahead and implemented this on the client side. At least it works for now.

Mithaldu
Sep 25, 2007

Let's cuddle. :3:
Not sure if this has been posted here before, but chromatic's blog is well worth following. Especially his latest post should be read by everyone, literally: http://www.modernperlbooks.com/mt/2009/11/from-novice-to-adept-the-act-of-naming.html

octobernight
Nov 25, 2004
High Priest of the Christian-Atheist Church
I have a question about how unix system commands work in perl. I have my PATH environment variable set to the directory of a program I want to run. I can type:

> my_program
my_program executed

However, if I try the same thing in my perl script with this line:

print `my_program`;

I get the error message, "Can't exec "my_program": No such file or directory at ../test.pl line 11." Why is this so?

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
It's running it from the current working directory. It has nothing to do with your environment variables.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Triple Tech posted:

It's running it from the current working directory. It has nothing to do with your environment variables.

What? No, UNIX doesn't exec programs from the cwd unless "." is a member of the PATH environment variable. It has everything to do with his PATH.

octobernight: Have your perl program print the contents of $ENV{"PATH"} to verify that it is correct; there may be some subtlety in how you are starting your perl program that is preventing it from inheriting the environment you think it is.

Vanadium
Jan 8, 2005

But it will not execute stuff from the current working directory if the current working directory is not in the PATH. :confused:

(e:f;b)

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
I might be talking out of my bum. I'm sorry. :(

octobernight
Nov 25, 2004
High Priest of the Christian-Atheist Church

ShoulderDaemon posted:

What? No, UNIX doesn't exec programs from the cwd unless "." is a member of the PATH environment variable. It has everything to do with his PATH.

octobernight: Have your perl program print the contents of $ENV{"PATH"} to verify that it is correct; there may be some subtlety in how you are starting your perl program that is preventing it from inheriting the environment you think it is.

I had perl print the $ENV{"PATH"} and it did show up correctly. I also checked and "." is on my PATH environment variable. I'm really stumped whether this is a perl problem or a unix problem.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

octobernight posted:

I had perl print the $ENV{"PATH"} and it did show up correctly. I also checked and "." is on my PATH environment variable. I'm really stumped whether this is a perl problem or a unix problem.

I'd check for something like an invisible unicode character or similar in the `` in your perl program. If you use something like `/full/path/to/my_program` does it work?

Also, having "." in your PATH is not normally considered a good idea.

octobernight
Nov 25, 2004
High Priest of the Christian-Atheist Church

ShoulderDaemon posted:

I'd check for something like an invisible unicode character or similar in the `` in your perl program. If you use something like `/full/path/to/my_program` does it work?

Also, having "." in your PATH is not normally considered a good idea.

I checked for any invisible characters and couldn't find any. I also tried using the full path, and it worked correctly. Very strange. Here's the error message I am receiving:

#This works
print `~/programs/my_program/bin/my_program -help`;

#This doesn't work
print `my_program -help`
Can't exec "my_program": No such file or directory at ./test.pl line 15.

#This works on the unix command line in any other directory
> my_program -help

I also removed the "." from my path. I only added to test if that could've been a problem. Thanks for all your help, though. It's just that this problem doesn't make too much sense to me.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

octobernight posted:

(environment variable shenanigans)

Well, when perl handles backticks it invokes a subshell; it's possible that the subshell's init process is overwriting your PATH variable.

Check the output of print `echo \$PATH` and report if that is correct.

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
FindBin (part of the core) will be useful here, if the script you're trying to run is located in the same directory as your Perl:
code:
use FindBin;

my $full_path = "$FindBin::Bin/my_program";
system $full_path;

Mario Incandenza fucked around with this message at 07:34 on Nov 9, 2009

octobernight
Nov 25, 2004
High Priest of the Christian-Atheist Church

ShoulderDaemon posted:

Well, when perl handles backticks it invokes a subshell; it's possible that the subshell's init process is overwriting your PATH variable.

Check the output of print `echo \$PATH` and report if that is correct.

I checked that, and the program does display on the correct path. Since I'm on such a tight time constraint, and I am only verifying another project, I think I'll hardcode in the path for now. I will need to figure out why this isn't working later. Thank you for all your help, though.

Mario Incandenza posted:

FindBin (part of the core) will be useful here, if the script you're trying to run is located in the same directory as your Perl:

I will try this later tonight once I read more about how FindBin works. Thanks for the link.

CanSpice
Jan 12, 2002

GO CANUCKS GO
So suppose I have an array of ranges. Something like
code:
$VAR1 = [
          [
            '214',
            '222'
          ],
          [
            '322',
            '331'
          ],
          [
            '101',
            '109'
          ],
          [
            '211',
            '215'
          ]
        ];
I would like to merge them together, so that the above example would have three ranges, 101:109, 211:222, and 322:331. Any suggestions on modules to use?

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
Number::Range

CanSpice
Jan 12, 2002

GO CANUCKS GO

Filburt Shellbach posted:

Number::Range

gently caress yeah. Thanks.

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
Don't you just love it when a reference is accidentally numified and passed through to your merchant account as a $166m charge against a dummy card number and the bank locks down your payment gateway as a result? :sigh:

White-Lightning
May 28, 2004

Code posted:

@output = `./cs216program5 $search $searchinfo`;
print "output = @output";
@output = split ( "_" , @output);
print "output after split = @output";

Output posted:

output = _223315_Daniels_Tom_April 02, 1990_2E_DNA1.data_
output after split = 1

I have no idea why I get a 1 back after the split. Could there be something wrong with the way I am using split? I tried splitting on /_/ too but that does the same thing.

I have also tried
@output = split ( "_" , `./cs216program5 $search $searchinfo`);
but nothing seems to come back.

White-Lightning fucked around with this message at 00:11 on Dec 4, 2009

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

White-Lightning posted:

I have no idea why I get a 1 back after the split. Could there be something wrong with the way I am using split? I tried splitting on /_/ too but that does the same thing.

I have also tried
@output = split ( "_" , `./cs216program5 $search $searchinfo`);
but nothing seems to come back.

@output is an array, but split expects a scalar in the second position, and because it's a builtin (and, thus, prototyped) it will coerce your array to scalar context, which does not concatenate its members (as you may expect) but instead simply calculates the length of the array.

So, your code is the same as @output = split( "_", length @output );, which obviously is @output = split( "_", 1 ); as the backticks returned a single element in this case, and is clearly not what you want.

Here's the fix:
code:
$output = `./cs216program5 $search $searchinfo`;
print "output = $output";
@output = split( "_", $output );
print "output after split = @output";
Note that $output and @output are different variables, which happen to (confusingly) share the same name and are distinguished by sigil only.

White-Lightning
May 28, 2004
code:
$output = `./cs216program5 $search $searchinfo`;
print "output = $output";
@output = split( "_", $output );
print "output after split = @output";
If I do it like this it will not get past
$output = `./cs216program5 $search $searchinfo`;

so I tried it as
@output = split( "_", `./cs216program5 $search $searchinfo` );
print "output after split = @output";
but that doesnt return anything.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

White-Lightning posted:

If I do it like this it will not get past
$output = `./cs216program5 $search $searchinfo`;

You need to be a bit more clear about what you mean by "will not get past". Does the program just stop there? It sounds like there's a bug in your cs216program5 code.

White-Lightning
May 28, 2004

ShoulderDaemon posted:

You need to be a bit more clear about what you mean by "will not get past". Does the program just stop there? It sounds like there's a bug in your cs216program5 code.

sorry, this is a cgi, the source of the webpage has nothing in it after this first display of output. the cs216program5 runs fine, it will return the data exactly as i need it, which is _223315_Daniels_Tom_April 02, 1990_2E_DNA1.data_

I can run it from the command line and it returns _223315_Daniels_Tom_April 02, 1990_2E_DNA1.data_

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

White-Lightning posted:

sorry, this is a cgi, the source of the webpage has nothing in it after this first display of output. the cs216program5 runs fine, it will return the data exactly as i need it, which is _223315_Daniels_Tom_April 02, 1990_2E_DNA1.data_

I can run it from the command line and it returns _223315_Daniels_Tom_April 02, 1990_2E_DNA1.data_

You can run CGI's from the command line as well as long as you're using CGI.pm to handle getting query parameters. You should either do that or check your server's error logs for more information, because there are any number of things that could be going wrong, and you've got basically nothing to go on.

Also, note that the CWD of your CGI may not be the directory it resides in when executed by the webserver, so ./ may not be the correct path to the cs216program5 program.

leedo
Nov 28, 2000

Mario Incandenza posted:

Don't you just love it when a reference is accidentally numified and passed through to your merchant account as a $166m charge against a dummy card number and the bank locks down your payment gateway as a result? :sigh:

Haha. The catalyst app that I wrote for CC processing successfully charged a card for 20k recently. I guess they actually owed us that much, but we're supposed to get paid by money order for anything that large. Oops, guess I should add an upper limit :o

thehandtruck
Mar 5, 2006

the thing about the jews is,
I'm new to coding and was asked to code a script that parses a text file A, and outputs text file B. Text file B contains every line from text file A that started withe number one. I got the READDATA part, the entering argument part, and increase line incrementally by 1 part, but I can't figure out how to actually tell it to say "OK output this line only if it starts with a line". Any help would be appreciated.

edit:
code:
while (<>) {
        if ( 1|output/(.*),v\s| ) {
                $file = $1;
        }
Is that the right track?

thehandtruck fucked around with this message at 19:03 on Dec 20, 2009

Adbot
ADBOT LOVES YOU

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
You on unix? if so this would work
code:
#!/usr/bin/env perl
while (<>) {
    print if /^1/;
}
(run it as ./your_program.pl A.txt > B.txt )



Aaand if you're not on unix doing the file io in perl isn't too hard
code:
use warnings;
use strict;

open my $outfile, '>', 'B.txt' or die $!;
while (<>) {
    print $outfile $_ if /^1/;
}
(run as perl your_program.pl A.txt )

The 'use warnings' and 'use strict' pragmas are a habit to get into Now, they will catch so many stupid errors for you it's not even funny

Blotto Skorzany fucked around with this message at 19:25 on Dec 20, 2009

  • Locked thread