So, I'm trying to automate the process of buying something on amazon (its part of a larger project). To do that I'm using WWW::Mechanize. But I keep getting this error "Can't call method "header" on an undefined value at /usr/local/share/perl5/WWW/Mechanize.pm line 2566." No clue what's going on, but here's my code. I looked at line 2566, but am not sure what is happening, since I'm not using that method (at least I don't think I am).

use strict; use WWW::Mechanize; use Crypt::SSLeay; #initialization my $loginPage="https://www.amazon.com/ap/signin?_encoding=UTF8&openid. +assoc_handle=amazon_checkout_us&openid.claimed_id=http%3A%2F%2Fspecs. +openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2 +F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=che +ckid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openi +d.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&ope +nid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com +%2Fgp%2Fbuy%2Fsignin%2Fhandlers%2Fcontinue.html%3Fie%3DUTF8%26eGCApp% +3D%26hasWorkingJavascript%3D0%26isEGCOrder%3D0%26oldCustomerId%3D%26o +ldPurchaseId%3D%26preInitiateCustomerId%3D%26purchaseInProgress%3D%26 +siteDesign%3D&pageId=amazon_checkout_us&showRmrMe=0&siteState=isRegul +arCheckout.1|IMBMsgs.|isRedirect.0"; my $addToCart="http://www.amazon.com/gp/product/handle-buy-box/ref=dp_ +start-bbf_1_glance"; #target my $target="http://www.amazon.com/Effective-Java-Edition-Joshua-Bloch/ +dp/0321356683/ref=sr_1_2?ie=UTF8&qid=1386465356&sr=8-2&keywords=java" +; my $fileToWrite; #login my $email="something"; my $pass="somethingelse"; #browser object my $mech=WWW::Mechanize->new(autocheck=>'1'); $mech->add_header(agent=>'Mozilla/5.0 (X11; Linux i686; rv:21.0) Gecko +/20100101 Firefox/21.0'); $mech->cookie_jar ( HTTP::Cookies->new ( 'file'=>'/mnt/sdb1/porteus/cookies/amazonSessionId.lwp', 'autosave'=>1, ) ); #main part, with dumping of statuses and content $mech->get($target); file("/file1.txt"); amazonSubmit($target); file("/file2.txt"); amazonSubmit($addToCart); file("/file3.txt"); login(); file("/file4.txt"); #subroutines sub amazonSubmit() {$mech->post(@_[0], [ 'type'=>'submit', 'value'=>'Go', 'class'=>'nav-submit-input', 'title'=>'Go', ] ); } sub login() { $mech->get($loginPage); $mech->set_visible($email, $pass); $mech->click_button('value'=>'"Sign in using our secure server"'); } sub file () { $fileToWrite=@_[0]; open (FILEHANDLE, ">\$fileToWrite"); print FILEHANDLE "$mech->content\n"; print FILEHANDLE "$mech->status"; close (FILEHANDLE); }

In reply to Error when using WWW::Mechanize by coderman9

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.