bradcathey has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monasterians,

Just tried to run what I thought would be a simple little module: Net::UPS which returns UPS services, rates, and enroute times. But I getting this lovely error:

Error executing run mode 'e': Can't use an undefined value as an ARRAY reference at /usr/share/perl5/Net/UPS.pm line 237

I'm confused what might be causing this. Ideas?

My code snippet (99% of it right out of the CPAN docs)

my $zip_to = "60446"; my $zip_from = "60187"; my $weight = 2.26"; my $package = Net::UPS::Package->new(); $package -> weight($weight); #dumped here my $services = $ups->shop_for_rates($zip_from, $zip_to, $package); while (my $service = shift @$services ) { $message .= sprintf("%-22s => \$.2f", $service->label, $service->t +otal_charges); if ( my $days = $service->guaranteed_days ) { sprintf("(delivers in %d day%s)\n", $days, ($days > 1) ? "s" : + ""); } else { "\n"; } }

Results of Data::Dumper:

$VAR1 = '60446'; $VAR2 = '43214'; $VAR3 = bless( [ undef, undef, undef, undef, undef, undef, '2.26' ], 'Net::UPS::Package' );

Last line (237) of this snip from UPS.pm is producing error message

sub shop_for_rates { my $self = shift; my ($from, $to, $packages, $args) = @_; unless ( $from && $to && $packages ) { croak "shop_for_rates(): usage error"; } unless ( ref $from ) { $from = Net::UPS::Address->new(postal_code=>$from); } unless ( ref $to ) { $to = Net::UPS::Address->new(postal_code=>$to); } unless ( ref $packages eq 'ARRAY' ) { $packages = [$packages]; } $args ||= {}; $args->{mode} = "shop"; $args->{service}||= "GROUND"; return [sort{$a->total_charges <=>$b->total_charges} @{$self->requ +est_rate($from, $to, $packages, $args)}]; }

—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Replies are listed 'Best First'.
Re: Net::UPS causing undefined array ref error
by grep (Monsignor) on Dec 19, 2006 at 02:33 UTC
    @{$self->request_rate($from, $to, $packages, $args)} is the array ref your error message is referring to. So that means the call to $self->request_rate failed. But that error message is still not very informative. I would run it through the perl debugger (perl -d script) just hit 'c' and you'll see the entire error stack.

    grep
    1)Gain XP 2)??? 3)Profit

      I was able to Dump just before the line in question.

      Does this help? I guess I'm not sure if there should be any undef's, but you'd think the docs would point that out.

      print Dumper ($from, $to, $packages, $args); $VAR1 = bless( [ undef, undef, '60446', undef, undef, undef ], 'Net::UPS::Address' ); $VAR2 = bless( [ undef, undef, '65432', undef, undef, undef ], 'Net::UPS::Address' ); $VAR3 = [ bless( [ undef, undef, undef, undef, undef, undef, '2.26' ], 'Net::UPS::Package' ) ]; $VAR4 = { 'mode' => 'shop', 'service' => 'GROUND' };

      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
        Does this help? I guess I'm not sure if there should be any undef's, but you'd think the docs would point that out.
        Not really. You should run it through the debugger and get the whole error stack. That will illuminate a lot more.

        grep
        1)Gain XP 2)??? 3)Profit

Re: Net::UPS causing undefined array ref error
by Popcorn Dave (Abbot) on Dec 19, 2006 at 04:47 UTC
    This is a real long shot, but I do a lot of UPS shipping by hand ( and I will be looking at this module now! ) and I do know that when you put the weight in the UPS book, you're supposed to round it up to the next pound.

    Have you tried your test using 3 instead of 2.26? If it's failing in the request as grep thought then that may be your culprit.

    HTH!

    Revolution. Today, 3 O'Clock. Meet behind the monkey bars.
Re: Net::UPS causing undefined array ref error
by zentara (Cardinal) on Dec 19, 2006 at 19:05 UTC
    I just looked at the module, and it seems to require a UPS id, password, and accesskey, which go into ~.upsrc, ( as described in the INSTALL file). Do you have this setup? I ask, because I see no mention of it in your code.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

      Thanks for looking. And you make a good point--sorry it wasn't in my code here, but it was in my original. I tried 2 separate accounts and a couple of different methods. All failed similarly.

      Production code:

      my $userid = "brad"; my $password = "xxxxxx"; my $accesskey = "686GJ8GJK1230HJL";

      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
        I just got this module and have been playing with it. I haven't used perl for a long time, and didn't remember how to debug, so this thread was helpful :) Thanks. I also found after debugging for a while that in my case the request_rate method was failing because, according to UPS, "This measurement system is not valid for the selected country." Perhaps you are getting a similar error... To make it display any error you're getting, you can insert the following lines into UPS.pm at line 210...

        210:
        211: unless (defined $services) {
        212: croak("rate(): services error - " . $self->errstr());
        213: }
        214:

        Then

        make clean
        perl Makefile.PL
        make
        make install

        and perhaps you'll be a little closer :)