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
Fenderbender
Oct 10, 2003

You have the right to remain silent.

TiMBuS posted:

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?

What I did was do a source install to /opt/perl and just changed the shebang to #!/opt/perl/bin/perl. This is only temporary until 5.10 is made available for an upgrade.

Adbot
ADBOT LOVES YOU

TiMBuS
Sep 25, 2007

LOL WUT?

Hm, feels a bit hackish to do it like that, plus ill have to change the shebang once 5.10 is out from the repositories..

But hey, that's probably easier than making my own package.

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!

TiMBuS posted:

Hm, feels a bit hackish to do it like that, plus ill have to change the shebang once 5.10 is out from the repositories..

But hey, that's probably easier than making my own package.

You can built it from source in your home directory, then alias perl=/home/timbus/bin/perl5.10. This way you can run 5.8.8 with ./script and 5.10.0 with perl script. Also, your one-liners will be 5.10.0.

Donkey Darko
Aug 13, 2007

I do not lust for blood or death. I prepare for the warrior's call.
Install Perl 5.10 to /opt/perl and do the following:

code:
sudo ln -s /opt/perl/perl /usr/bin/perl10
You can then either run scripts using Perl 5.10 from the command line with perl10 [scriptname] or you can add /usr/bin/perl10 to the shebang. When 5.10 is finally added to Bunty's repos, all you need to do is make that symlink point to /usr/bin/perl and your scripts will still work without any editing.

HTH

Donkey Darko fucked around with this message at 11:02 on Jan 14, 2008

Stabby McDamage
Dec 11, 2005

Doctor Rope
If I understand Ubuntu right, perl 5.10 will never be in the current Ubuntu distribution, since each distribution doesn't advance major package versions, just minor updates. So I believe you'll see it if you install Ubuntu 8.04 in 3 months.

It may become available through the backports repository, which is disabled by default.

If you really need Perl 5.10, you probably need it for something specific, so install it to /opt or your home directory and start your scripts with #!/opt/perl/bin/perl or somesuch. When the upgrade comes, just change the header lines.

TiMBuS
Sep 25, 2007

LOL WUT?

I don't really need it, I just really want it :3

I nearly went and installed perl over the top of the old one, but common sense prevailed and I instead installed it to /opt/.

Now to go abuse the crap out of 'say'.

Fenderbender
Oct 10, 2003

You have the right to remain silent.

TiMBuS posted:

I don't really need it, I just really want it :3

I nearly went and installed perl over the top of the old one, but common sense prevailed and I instead installed it to /opt/.

Now to go abuse the crap out of 'say'.

Throw this at the beginning of a 5.8 script:
code:
$\ = "\n";
:3: This has been a life-saver in the past. Every print will have $\ appended to the end of it. So make $\ a newline and you effecively have 'say'.

Fenderbender fucked around with this message at 09:36 on Jan 15, 2008

TiMBuS
Sep 25, 2007

LOL WUT?

Fenderbender posted:

Throw this at the beginning of a 5.8 script:
code:
$\ = "\n";
:3: This has been a life-saver in the past. Every print will have $\ appended to the end of it. So make $\ a newline and you effecively have 'say'.

But then I would have to undef $\ when using print for other needs. =(

I personally like having two keywords for output, because 'say' works exactly how it sounds, and 'print' can now function for purposes that don't require autoformatting.

Personal preference, I guess.

There Will Be Penalty
May 18, 2002

Makes a great pet!

TiMBuS posted:

But then I would have to undef $\ when using print for other needs. =(

code:
print "fred";        # doesn't print a newline
{
    local $\ = "\n"; # can't use my here
    print "barney";  # prints a newline
}
print "wilma";       # doesn't print a newline

TiMBuS
Sep 25, 2007

LOL WUT?

Ah yeah, nearly forgot about local.
I mostly use local to undef $/ which makes it faster to glob through files.

But it's not exactly a convenient workaround to locally scope $\ just to add an automatic newline.

In the end I guess I just like say.

Fenderbender
Oct 10, 2003

You have the right to remain silent.

TiMBuS posted:

Ah yeah, nearly forgot about local.
I mostly use local to undef $/ which makes it faster to glob through files.

But it's not exactly a convenient workaround to locally scope $\ just to add an automatic newline.

In the end I guess I just like say.

Say is definitely really cool, have you seen Perl6::Say? :3:

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Anybody going to YAPC in Chicago this year? I think I'd like to convince my employer to let me go but I'm new here still...

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!

Triple Tech posted:

Anybody going to YAPC in Chicago this year? I think I'd like to convince my employer to let me go but I'm new here still...

Don't know yet. I'd like to go.

I will be speaking at Frozen Perl next month though!

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?

Sartak posted:

I will be speaking at Frozen Perl next month though!

Oh yeah? A lightning talk probably... About what? Who are you really... Are you like a famous Perl guy? At least you've got balls to talk, that's gotta count for something.

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!

Triple Tech posted:

Oh yeah? A lightning talk probably... About what? Who are you really... Are you like a famous Perl guy? At least you've got balls to talk, that's gotta count for something.

I'm giving a twenty minute talk on Template:: Declare. It looks like this:

code:
template foo => page {
    div {
        ol {
            for (@bar) {
                li { $_ }
            }
        }
    }
}
Way way nicer than anything else I've used for generating HTML and XML.

edit: drat smilies.

another edit: Also, the only community I'm at all famous in is NetHack (where I go by the name Eidolos). I'm lucky enough to work on RT and Jifty, and I try to help with Moose when I can.

Filburt Shellbach fucked around with this message at 15:55 on Jan 16, 2008

npe
Oct 15, 2004
Holy crap, how come I didn't know about this a little while ago when I needed a very simple way to generate XML output (and was getting very annoyed at the overhead of XML::Twig). :mad:

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
Any clues to what could be causing this?
code:
Net::SSLeay object version 1.30 does not match bootstrap parameter 1.25 at
    /usr/lib/perl5/5.8.8/i386-linux-thread-multi/DynaLoader.pm line 251, <DATA> line 225.
Compilation failed in require at
    /usr/lib/perl5/vendor_perl/5.8.8/IO/Socket/SSL.pm line 17, <DATA> line 225.
BEGIN failed--compilation aborted at
    /usr/lib/perl5/vendor_perl/5.8.8/IO/Socket/SSL.pm line 17, <DATA> line 225.
Compilation failed in require at
    /usr/lib/perl5/vendor_perl/5.8.8/Net/LDAP.pm line 156, <DATA> line 225.
Line 17 is just
code:
use Net::SSLeay;
so I don't know where the 1.25 parameter to bootstrap could be coming from. I've tried changing it to
code:
use Net::SSLeay 1.30;
but get the same error. Both Net::SSLeay and Net::LDAP are the latest, RedHat-supplied versions for i386. I'll appreciate even the slightest guesses toward a solution.

Fenderbender
Oct 10, 2003

You have the right to remain silent.

Mustach posted:

Any clues to what could be causing this?
code:
Net::SSLeay object version 1.30 does not match bootstrap parameter 1.25 at
    /usr/lib/perl5/5.8.8/i386-linux-thread-multi/DynaLoader.pm line 251, <DATA> line 225.
Compilation failed in require at
    /usr/lib/perl5/vendor_perl/5.8.8/IO/Socket/SSL.pm line 17, <DATA> line 225.
BEGIN failed--compilation aborted at
    /usr/lib/perl5/vendor_perl/5.8.8/IO/Socket/SSL.pm line 17, <DATA> line 225.
Compilation failed in require at
    /usr/lib/perl5/vendor_perl/5.8.8/Net/LDAP.pm line 156, <DATA> line 225.
Line 17 is just
code:
use Net::SSLeay;
so I don't know where the 1.25 parameter to bootstrap could be coming from. I've tried changing it to
code:
use Net::SSLeay 1.30;
but get the same error. Both Net::SSLeay and Net::LDAP are the latest, RedHat-supplied versions for i386. I'll appreciate even the slightest guesses toward a solution.

The only thing I can guess is that you have shared objects with mismatched versions?

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
Yeah, sounds like you need to reinstall Net::SSLeay so that the XS components are properly linked.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
Thanks. I already tried reinstalling each module individually, then all at once, but maybe the order messed them up (which would be ridiculous if true). I'll try again tomorrow, anyhow.

Update: No dice after reinstalling them.

Mustach fucked around with this message at 21:54 on Jan 18, 2008

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
If you can read its output, try strace perl -c failing_script.pl and see where Perl is loading its libraries from (look for stat and open calls in particular). That will probably give you an idea of why it's failing, at the very least you can try blasting the mismatched versions and installing them again.

mister_gosh
May 24, 2002

I have a problem with encoding. The resulting file must be UTF-8.

I am opening a file handle, doing something with each line and then printing it out.

code:
open (IN, "C:\\in2.xml") || die("couldn't open C:\\in2.xml for reading\n");
open (OUT, ">C:\\out2.xml") || die("couldn't open file for writing\n");

while(<IN>) {
  my $line = $_;
  chomp($line);

  #Encode::from_to($line, "utf8", "utf8");  # Line 8
  Encode::from_to($line, "iso-8859-1", "utf8"); # Line 9
 
  # OTHER STUFF ...

  print OUT "$line\n";
}
close IN;
close OUT;
Oftentimes, my file is iso-8859-1 so I use the Encode::from_to routine to convert it.

Other times, that makes things worse because it is already in utf8.

Is there a way to test if the line is already utf8? I think I need a good encode tutorial.

npe
Oct 15, 2004

quote:


Is there a way to test if the line is already utf8? I think I need a good encode tutorial.

Not really. I think some software tries to guess (there's a famous bug in notepad about this) but there is no foolproof way. A string of utf-8 characters can be interpreted as a (somewhat nonsensical) string of iso-8859 characters.

mister_gosh
May 24, 2002

What does the extra dollar sign mean in a variable?

For example: $$variableName

leedo
Nov 28, 2000

mister_gosh posted:

What does the extra dollar sign mean in a variable?

For example: $$variableName

That would dereference a reference to a scalar. Example:
code:
my $foo = "uhh";
my $bar = \$foo;
print $$bar; #prints "uhh"

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
It might also be a symbolic reference:

code:
> $foo = "bar";
> $var = "foo";
> print $$var;
bar
This is pretty horrible programming practice and shouldn't be used on a widespread basis, if at all.

fansipans
Nov 20, 2005

Internets. Serious Business.

SpeedFrog posted:

It might also be a symbolic reference:

code:
> $foo = "bar";
> $var = "foo";
> print $$var;
bar
This is pretty horrible programming practice and shouldn't be used on a widespread basis, if at all.

Also if you're using strict and warnings as you should at the top of every file:
code:
use strict;
use warnings;
then Perl will barf at you with the code above, because symbolic references are pretty smelly:
code:
Can't use string ("foo") as a SCALAR ref while "strict refs" in use at -e line 1.

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
I have drunk deeply of the Catalyst + DBIx::Class koolaid and am looking forward to overhauling the 80,000 lines of garbage I get paid to nurse.

Mario Incandenza fucked around with this message at 14:27 on Feb 7, 2008

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?

SpeedFrog posted:

I have drunk deeply of the Catalyst + DBIx::Class koolaid and am looking forward to overhauling the 80,000 lines of garbage I get paid to nurse.

Oh good, good you can tell me about how Catalyst works then. When I access a URL of a website that's running on Catalyst, what is the server actually hitting? Do all the URLs get submitted as an argument to a dispatcher, or are there individual .cgi files laying around?

I'd like to move my company's site to a framework but I'm not sure how this fancy stuff works.

more falafel please
Feb 26, 2005

forums poster

Triple Tech posted:

Oh good, good you can tell me about how Catalyst works then. When I access a URL of a website that's running on Catalyst, what is the server actually hitting? Do all the URLs get submitted as an argument to a dispatcher, or are there individual .cgi files laying around?

I'd like to move my company's site to a framework but I'm not sure how this fancy stuff works.

All I know about Catalyst is that I went to school with the author of this book and he's the crazy shut-in nerd type who's so much smarter than you or any of his professors that he doesn't need to do his work because it's below him, which is why going to school is dumb and they SHOULD just kick him out for failing all his classes.

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?

more falafel please posted:

All I know about Catalyst is that I went to school with the author of this book and he's the crazy shut-in nerd type who's so much smarter than you or any of his professors that he doesn't need to do his work because it's below him, which is why going to school is dumb and they SHOULD just kick him out for failing all his classes.

Right... So does the nerd get shut in before or after the model interacts with the view? :D

leedo
Nov 28, 2000

more falafel please posted:

All I know about Catalyst is that I went to school with the author of this book and he's the crazy shut-in nerd type who's so much smarter than you or any of his professors that he doesn't need to do his work because it's below him, which is why going to school is dumb and they SHOULD just kick him out for failing all his classes.
Well, I know he at least gets out for the local Perl Mongers meetings. I think he is in charge of organizing YAPC'08 as well.

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?

Triple Tech posted:

Oh good, good you can tell me about how Catalyst works then. When I access a URL of a website that's running on Catalyst, what is the server actually hitting? Do all the URLs get submitted as an argument to a dispatcher, or are there individual .cgi files laying around?

I'd like to move my company's site to a framework but I'm not sure how this fancy stuff works.
Since it's based around fast-runtime at the expense of slow compilation, you're better off using Catalyst in conjunction with FastCGI or mod_perl - otherwise you'll be adding a second or two onto your response time. It ships with a HTTP::Server engine for use during development.

The controllers contain actions, which are just methods that have subroutine attributes defining the URLs they can handle. There's plenty of syntactic sugar for interacting with the models and views, and I'm really impressed with how easy it was to plug in authentication and other stuff, generally it was a simple case of installing a CPAN module and adding a couple of lines to the YAML config file.

jrockway's book is okay but it's really just an intro, you'll get just as much out of the POD as you will from the book (except for maybe the REST and Jemplate stuff which are pretty snazzy).

Here's a controller that handles AJAX-style logins:
code:
package MyApp::Controller::Login;
use base "Catalyst::Controller';

# matches http://dev.blah.com/login, optional trailing slash
sub login :Path {
  my ( $self, $c ) = @_;

  if ($c->user) {
    $c->res->redirect($c->uri_for('/home'));
  } else {
    $c->stash->{template} = 'login.tt2';
  }
}

# matches http://dev.blah.com/login/submit
sub submit :Local {
  my ($self, $c) = @_;

  my $q = $c->req->params;

  if ($c->login(@$q{qw/username password/})) {
    $c->stash->{success} = 1;
  }
  else {
    $c->stash->{success} = undef;
    $c->stash->{errors} = { username => 'bad login details.', password => 'bad login details.' };
  }

  $c->detach('View::JSON');
}

Mario Incandenza fucked around with this message at 04:01 on Feb 8, 2008

leedo
Nov 28, 2000

SpeedFrog posted:

code:
# matches [url]http://dev.blah.com/login/submit[/url]
sub submit :Local {
  my ($self, $c) = @_;

  my $q = $c->req->params;

  if ($c->login([b]@$q{qw/username password/}[/b])) {
    $c->stash->{success} = 1;
  }
  else {
    $c->stash->{success} = undef;
    $c->stash->{errors} = { username => 'bad login details.', password => 'bad login details.' };
  }

  $c->detach('View::JSON');
}
Whoa, I had never seen that syntax before (using a list of hash keys)... cool!

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!

leedo posted:

Whoa, I had never seen that syntax before (using a list of hash keys)... cool!

Yeah, hash slices are neat. Here are some more idioms:

code:
@to{@keys} = @from{@keys};
@to{keys %from} = values %from;  # much faster than %to = (%to, %from);
@exists{@items} = ();
delete @score{'bad', 'worse', 'worst'};

ProvostZak
Dec 31, 2000

it's a huge step
Not entirely sure if this is okay to post here, since it's a little long, but it seems to fit.

I posted a thread on this topic a while back (http://forums.somethingawful.com/showthread.php?threadid=2699687) and ya'll were very helpful, though my extremely poor perl skills meant I couldn't make much use of all the help given. But I have gotten a little bit better now...

In a nutshell, I wanted to break a csv file filled with long paragraphs of text in rows into groups of single and multiple words, and then count the iterations of each word set. So for example, the previous two paragraphs would break down as follows:

    I
    posted
    a
    thread

    I posted
    posted a
    a thread

    I posted a
    posted a thread
    a thread on


And I initially planned for the script to count the number of times words or phrases appeared. I have since realised that I can use Access to cover the relatively hard part of the script (the counting of entries), which leaves me with the more straightforward problem of getting the damnable sentences to split at the correct point. Here's the (no doubt quite ugly) script I was able to write:

code:
$filename = "samplefile.txt";
open ( FILE, $filename) or die "Cannot open file: $!";

 while ( $data = <FILE> ) {

 $data =~ tr/A-Z/a-z/;

 @values = split(/\s/, $data);

 foreach $val (@values)

 {
       open (OUTFILE, '>>testfile.txt');
       print OUTFILE "$val\n";
       close (OUTFILE);
 }
}
exit 0;
This code does the simplest bit of my request: it breaks on whitespace, thus giving me the first set of the list I need. The challenge though, which I have yet to crack, is getting the damnable thing to break on word pairs, triples etc. I have tried a lot of different things that seems like they might work, such as /w+\s\w+/ but I get weird results – either it just breaks on the end of each row (basically adding an empty row between every full paragraph entry) or it replaces all words that aren't directly adjacent to a character like "-" or "." with a blank paragraph break.

I'm also not sure how to get it to 'fall back' after its done a pair. So for example, if it gets "I posted" how am I going to get it to give me "posted a" instead of moving onto the 'next' pair, "a thread"...

Any perl expert wisdom?

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Untested.

code:
my @words = ...;
my(@doubles, @triples);

for (my $i = 0; $i < @doubles; $i++) {
  push @doubles, [@words[$i .. $i + 1]];
  push @triples, [@words[$i .. $i + 2]];
}

Mario Incandenza
Aug 24, 2000

Tell me, small fry, have you ever heard of the golden Triumph Forks?
COBOL programmers can write greate COBOL in any language :q:
code:
if ((( $SessionType == 1 && $LoginData{'AccountType'} eq "Pre-paid" &&
( ( ($LoginData{'Balance'} * 100) < ($VAR{'OrderPrice'} * 100) &&
$EnableModules{'Domains'} == 0 ||
( ($LoginData{'TestBalance'} * 100) < ($VAR{'OrderTotal'} * 100) &&
$EnableModules{'Domains'} == 1 ) ) )
 || ($SessionType == 3 && $VAR{'OrderTotal'} > 0)) && $ENV{'HTTPS'} ne "on" ) {
    $VAR{'s'} = "https://$ENV{'HTTP_HOST'}$LoginData{'SSLPort'}$ENV{'SCRIPT_NAME'}?SID=$FORM{'SID'}&Step=$FORM{'Step'}";
    $Page = $Pages[0];
}

Triple Tech posted:

Untested.

This works a bit better:

code:
# don't use tr to transform case
$data = lc($data);

...

my $i = 0;
while (1) {
  my ($double, $triple) = ($i + 1, $i + 2);

  $double < @words and push @doubles, [ @words[$i .. $double] ];
  $triple < @words and push @triples, [ @words[$i .. $triple] ];

  last if ++$i > @words;
}
It would be possible in one regex but you'd need to get pretty stoned first.

Mario Incandenza fucked around with this message at 05:32 on Feb 9, 2008

Kidane
Dec 15, 2004

DANGER TO MANIFOLD
i'm using getopt::long to get command line arguments for my script. my problem is this, I want the script to die (or at least show my usage message) if someone passes a bad argument to the script. however, when someone passes a good argument then a bad argument the script executes, albeit with the message "Unknown option: foo".

is there a way to catch this kind of exception?

GOOD:
code:
[root@nickforsale ~]# ./fixstats.pl -aasd
Unknown option: aasd

Usage: ./fixstats.pl [--domain=<domain> | --all | --verbose | --help]

--domain        : Run statistics on specified domain only. May not be used in conjunction with --all.
--all           : Run statistics on all domains. May not be used in conjunction with --domain.
--verbose       : Verbose mode. Displays domains as they are processed.
--help          : This message.
BAD:
code:
[root@nickforsale ~]# ./fixstats.pl --domain=blah.com -q
Unknown option: q
got domain: -blah.com-
the main program loop starts after my 'got domain' debug thingie, and i would rather people are passed to the usage script when they enter a bad argument no matter what. i will paste my terrible code now:

code:
GetOptions(\%options, "domain:s", "all", "help", "verbose");

if ($options{verbose}){
       $verbose = 1;
}
if ($options{help}) {
       show_syntax();
}
if ($options{all}) {
       $alldomains = 1;
}
if ($options{domain}) {
       if ($options{all}) {
               show_syntax();
       }
       if (length($options{domain}) == 0) {
               show_syntax();
       }
       print "got domain: -$options{domain}-\n";
       $alldomains = 0;
       @vhosts = ($options{domain})
}
if (not %options) {
       show_syntax();
}

Adbot
ADBOT LOVES YOU

npe
Oct 15, 2004
There might be a better way to do this, but using Getopt::Long::Configure to set pass_through (which preserves unspecified options in @ARGV without warning) lets you test to see if any are left. I tried the following:

code:
[~] $ cat test.pl
use strict;
use Getopt::Long;
Getopt::Long::Configure('pass_through');

my %options;
GetOptions(\%options, "domain:s", "all", "help", "verbose");

if ($options{help} or scalar @ARGV) {
    print "help!\n";
    exit;
}

print "run time!\n";

[~] $ perl test.pl --domain=blah --what
help!
[~] $ perl test.pl --domain=blah
run time!

  • Locked thread