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
6174
Dec 4, 2004
My knowledge of Perl is limited as I really just use it as a duct tape language, or if a CPAN module looks convenient for whatever I'm trying to accomplish. I was writing a script last night that uses Net::Amazon to query prices and I think I found a bug, but before I harass the author I thought I'd post here for someone to confirm I'm not just doing something stupid.

code:
#!/usr/bin/perl

use strict;
use warnings;

use Net::Amazon;
use Net::Amazon::Request::ASIN;

#use Log::Log4perl qw(:easy);
#Log::Log4perl->easy_init($DEBUG);

my $ua = Net::Amazon->new(
    token       => "144RR0T0YM45X6SVKSR2",
    max_pages   => 5,
    strict      => 1,
);

my $req = Net::Amazon::Request::ASIN->new(
    asin => "0596000278",
);

my $resp = $ua->request($req);

if ($resp->is_success()) {
    for ($resp->properties()) {
        print "Product Name: ",$_->ProductName(),"\n",
            "List Price: ",$_->ListPrice(),"\tOur Price: ", $_->OurPrice(),
            "\tASIN: ",$_->ASIN(),"\n",
            "Availability: ",$_->Availability(),"\t";
        if ($_->SuperSaverShipping()) {
            print "Eligible for super saver shipping\n";
        } else {
            print "NOT eligible for super saver shipping\n";
        }
    }
} else {
    print "Error: ",$resp->message(),"\n";
}
(This uses the AWS Access key from here, but can/should be run under your own key if you have one)

Now the problem is the call to ListPrice() is always the same as OurPrice(), which is supposed to be the price Amazon is selling the item for. I think this is a bug because looking at Property.pm, lines 32 and 38 seem to be referring to the same data. Now it is possible that it is reassigned elsewhere, but I haven't found where if it does.

Adbot
ADBOT LOVES YOU

6174
Dec 4, 2004
How would I rewrite this C++ function in a Perlish manner?

code:
bool line_match(const string base, const string insert)
{
     if (base.substr(74, 8) == insert.substr(74, 8)) {
          if (base.substr(89, 8) == insert.substr(89, 8)) {
               if (base.substr(114, 8) == insert.substr(114, 8)) {
                    return true;
               }
          }
     }
     return false;
}

6174
Dec 4, 2004

Erasmus Darwin posted:

code:
sub line_match($$) {
  my ($base, $insert) = @_;

  return (substr($base,  74, 8) eq substr($insert,  74, 8)) &&
         (substr($base,  89, 8) eq substr($insert,  89, 8)) &&
         (substr($base, 114, 8) eq substr($insert, 114, 8));
}
It's also worth noting that the C++ version can be rewritten the same way.

Thanks. I was expecting something that didn't translate so directly.

6174
Dec 4, 2004
I've got a very confusing problem.

code:
$sfrma = $IA*(2*$jp+1)*$aval/($QT*8*$pi*$C*$nu*$nu)*$rhs;
#$sfrma = $IA*(2*$jp+1)*$aval/($QT*8*$pi*$C*$nu*$nu)*$rhs;
This works, however if I remove the commented line, it fails with an error 75 lines later. If both lines are commented things work, but obviously this won't work in the long run because I need the calculation of sfrma. If it makes a difference $IA, $QT, $C, $T, and $C2 are all constants, and $pi and $rhs is calculated by

code:
$pi = atan2(1,1)*4;
$rhs = exp(-$C2*$lowerE/$T)*(1-exp(-$C2*$nu/$T));
$aval, $lowerE, $jp, and $nu are all captured as substrings from $_. (Roughly speaking this script reads in a plain text file in one format, and converts it to another format)

To further confuse matters, the calculation of $sfrma was copied from another script doing a slightly different task and things worked there without the extra commented line.

Does anyone have any ideas what could be going wrong?

edit:

If I remove "$QT*" from the denominator of $sfrma, the calculation works. The constants are all defined like:
code:
$IA = 0.997473;
$QT = 80.362;
$C = 2.99792458e10;
$T = 296.00;
$C2 = 1.4388;

6174 fucked around with this message at 18:33 on May 7, 2008

6174
Dec 4, 2004

Zombywuf posted:

Looks like you're confusing the Perl parser. Try adding some whitespace, if nothing else, just for readability's sake.

I just tried maybe a dozen variations of adding whitespace at various locations (including whitespace around every operator) and the same error occurred.

This being the source (the parser being confused) would also be confusing to me because the line worked just fine from the script I copied it from (which roughly takes the same input format and outputs yet another different format).

Also for the record, these scripts weren't things I wrote initially, I just need to change a formula here and there.

6174
Dec 4, 2004

Triple Tech posted:

Try running your script through Deparse to see what's actually running. perl -MO=Deparse script.pl

I don't know what to make of the output of this:

code:
[6174@antelope mark.2008.04.15]$ perl -MO=Deparse ../convert
Number found where operator expected at ../convert line 144, near "case  2"
        (Do you need to predeclare case?)
Number found where operator expected at ../convert line 145, near "case  1"
        (Do you need to predeclare case?)
Number found where operator expected at ../convert line 146, near "case  0"
        (Do you need to predeclare case?)
syntax error at ../convert line 143, near ") {"
syntax error at ../convert line 145, near "case  1"
syntax error at ../convert line 146, near "case  0"
../convert had compilation errors.
use Switch;
sub by_wave {
    $a <=> $b;
}
[6174@antelope mark.2008.04.15]$ 
edit: The error given when the script is run is exactly as above less the lines "use Switch;" and onward.

The $sfrma calculation is on line 75 as posted above, and starting at 142 is:

code:
#Determine the first letter
switch ($np - $npp) {
    case  2  { $brpp = "S" }
    case  1  { $brpp = "R" }
    case  0  { $brpp = "Q" }
    case -1  { $brpp = "P" }
    case -2  { $brpp = "O" }
    else     { print "Invalid value of N' - N''\n"; exit}
}
Also line 3 is "use Switch;"

6174 fucked around with this message at 19:14 on May 7, 2008

6174
Dec 4, 2004

Zombywuf posted:

I suspect your problem lies there, Switch will be doing weird things to your syntax tree to add more syntax into Perl. It's a bit of a pain, but you're safer with elsif.

I think this is it. I just converted the switch to a bunch of if/elsif and it now parses correctly and runs.

Thanks for the help Zombywuf and Triple Tech.

6174
Dec 4, 2004
I'm having a problem with Perl. I've got a script using Archive::Zip. RedHat Issued a patch today for RHEL 4 (yes it is old, but it is what I've got to work with). http://rhn.redhat.com/errata/RHSA-2008-0522.html

After the upgrade a script that worked now fails.

code:
$ ../prepare_sondes_mssftp.pl source/ dest
 is only avaliable with the XS version at /usr/lib/perl5/site_perl/5.8.5/Compress/Zlib.pm line 9
BEGIN failed--compilation aborted at /usr/lib/perl5/site_perl/5.8.5/Compress/Zlib.pm line 9.
Compilation failed in require at /usr/lib/perl5/site_perl/5.8.5/Archive/Zip.pm line 11.
BEGIN failed--compilation aborted at /usr/lib/perl5/site_perl/5.8.5/Archive/Zip.pm line 11.
Compilation failed in require at ../prepare_sondes_mssftp.pl line 6.
BEGIN failed--compilation aborted at ../prepare_sondes_mssftp.pl line 6.
$ head ../prepare_sondes_mssftp.pl
#!/usr/bin/perl

use strict;
use warnings;

use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use File::Spec;

sub process_dir
{
$ 
Line 6 is "use Archive....". When I googled around, the only help I got was some people saying that Scalar::Util is somehow screwed up and the answer is to reinstall the module in CPAN.

So within CPAN I first upgraded CPAN, then did "test Archive::Zip" and all the related packages I could find (Scalar::Util, IO::Compress::Base, Compress::Raw::Zlib). CPAN now says all the things tested successfully, but the script is still not working. Anyone got any ideas on what to try/look for?

6174
Dec 4, 2004

Triple Tech posted:

How about a straight up use? perl -MArchive::Zip -e 1

code:
$ perl -MArchive::Zip -e 1
 is only avaliable with the XS version at /usr/lib/perl5/site_perl/5.8.5/Compress/Zlib.pm line 9
BEGIN failed--compilation aborted at /usr/lib/perl5/site_perl/5.8.5/Compress/Zlib.pm line 9.
Compilation failed in require at /usr/lib/perl5/site_perl/5.8.5/Archive/Zip.pm line 11.
BEGIN failed--compilation aborted at /usr/lib/perl5/site_perl/5.8.5/Archive/Zip.pm line 11.
Compilation failed in require.
BEGIN failed--compilation aborted.
$ 

Sartak posted:

More reasons why I'm frustrated with RHEL

I do have 5.8.5 on the box. Is the answer then to not let RedHat manage Perl? (ie remove the rpm and grab Perl and do a standard make; make install; )

Adbot
ADBOT LOVES YOU

6174
Dec 4, 2004

6174 posted:

I do have 5.8.5 on the box. Is the answer then to not let RedHat manage Perl? (ie remove the rpm and grab Perl and do a standard make; make install; )

I just finished manually compiling and installing 5.8.8 and everything works again. Stupid RedHat.

  • Locked thread