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

Hi Folks

if i take the code outside a package it works perfectly. Once put into a package i do the following which works great

my $get_account = accounts->new($ss_username,$ss_password);
the call works fine and getAccountInfo gets called.
The only thing that isn't working is the call to ret_data2 once curl is complete.
The code works outside a package..just not within a package...the ret_data2 is never called even though $curl->errbuff is 0.

Your thoughts?
use WWW::Curl::Easy; use Data::Dumper; use XML::DOM; { package accounts; my $ss_username; my $ss_account_id; my $ss_email_address; my $ss_password; my $self = { ss_username => $ss_username, ss_password => $ss_password, ss_account_id => $ss_account_id, ss_email_address => $ss_email_address }; # Anonymous hash reference holds instance attrib +utes sub new { my($class, $ss_username,$ss_password) = @_; # Class name is i +n the first parameter bless($self, $class); # Say: $self is a $class my $ret_list = getAccountInfo(); print $ret_list."\n"; return $self; } sub getAccountInfo { my @authHeader = ('Accept: application/xml', 'Content-Type: application/xml'); # Setting the options my $curl = new WWW::Curl::Easy; $curl->setopt(CURLOPT_HEADER,0); $curl->setopt(CURLOPT_HTTPHEADER, \@authHeader); $curl->setopt(CURLOPT_SSL_VERIFYPEER, 0); $curl->setopt(CURLOPT_USERPWD, 'myusername:mypassword'); $curl->setopt(CURLOPT_URL, "https://app.streamsend.com/ audiences"); $curl->setopt(CURLOPT_RETURNTRANSFER,1); $curl->setopt(CURLOPT_WRITEFUNCTION,\&ret_data2); my $result = $curl->perform(); my $err = $curl->errbuff; print "err = ".$err."\n"; $curl->dispose(); } ###################################################################### +## # ret_data2 # is called once curl is completed ###################################################################### +## sub ret_data2 { print "ret_data\n"; my $chunk = shift; #this will get the xml print $chunk; $xp = new XML::DOM::Parser(); $doc = $xp->parse($chunk); $self->{ss_account_id} = $doc->getElementsByTagName("id")->item(0)- >getFirstChild->getNodeValue; $self->{ss_email_address} = $doc->getElementsByTagName("from-email- address")->item(0)->getFirstChild->getNodeValue; return 0; } } 1;

Replies are listed 'Best First'.
Re: Perl Package & Curl
by Anonymous Monk on Dec 15, 2008 at 15:09 UTC
    You need to put
    use WWW::Curl::Easy; use Data::Dumper; use XML::DOM;
    in package because of Exporter (all those modules export something)

    Why wait to bless your single possible object in sub new?

      What the AM is trying to tell you is you should've written
      { package accounts; use warnings; use strict; use WWW::Curl::Easy; use Data::Dumper; use XML::DOM; . . .
      since, as written, the OP would have the used modules export into the main namespace, not the accounts namespace as required ... an error about which the strictures would've let you know PDQ.

      A user level that continues to overstate my experience :-))
Re: Perl Package & Curl
by Anonymous Monk on Dec 15, 2008 at 15:11 UTC