I didn't get much response from beginners@ so I thought I'd ask this here. I thought I had it all figured out. Hash within hashes really made my code cleaner until I came up with this code: While looping, I was creating a key 'webloginpostdata' with value of anonymous hash as seen below;
$all_ap{$_}->{'webloginpostdata'} = { 'var:main' => 'menu', 'testwebcm' => 'webcm', 'login:command/username' => '', 'login:command/password' => '', 'var:connecting1' => '0' };
Now following the creation above, i'm creating another key 'webadminpostdata' which has a value of an anonymous hash again.
$all_ap{$_}->{'webadminpostdata'} = { 'var:main' => 'menu', 'var:style' => 'style5', 'settings/username' => $all_ap{$_}->{'webuser'}, 'settings/password' => $all_ap{$_}->{'webpass'}, 'settings/password_confirm' => $all_ap{$_}->{'we +bpass'}, 'settings/idle_timeout' => '30' };
Then there's two subroutines. The first one ("loginweb") is called. If successful, the next one ("changewebpass") is called
my $login_status = loginweb($all_ap{$_}->{'webloginurl'}, $all_ap{$_}- +>{'webloginpostdata'}); if ($login_status eq 'success'){ ## Call next subroutine my $setpassword = changewebpass($all_ap{$_}->{'webadminurl'}, $all_ap{ +$_}->{'webadminpostdata'}); if ($setpassword ne 'success'){ print "Can\'t change password\n"; }else{ print "New login created:\n\t username: $all_ap{$_}->{'webuser'} + \n\t password: $all_ap{$_}->{'webpass'}\n"; }
"loginweb" sub is really simple:
sub loginweb{ my $url = shift; my $postdata = shift; my $username; my $password; open PASSLIST, "appasswd.txt" or die $!; while (<PASSLIST>){ $username = (split / === /, $_)[0]; $password = (split / === /, $_)[1]; chomp ($username, $password); print "\nTrying username: \'$username\' password: \'$password\'"; $$postdata{'login:command/username'} = ($username eq '')?'':$usernam +e; $$postdata{'login:command/password'} = ($password eq '')?'':$passwor +d; my $res = $ua->request(POST $url, $postdata); if ($res->content =~ /Basic\s+Home\s+Menu/){ print " SUCCESS!!!\n"; return 'success'; }else{ next; } }
Finally, here's the "changewebpass"
sub changewebpass{ my $url = shift; my $postdata = shift; my $res = $ua->request(POST $url, $postdata); if ($res->is_success){ return 'success'; }else{ return 'failed'; } }
When I run my program, I get an error: "Need a field name at (eval 11) line 1" That's all. No mention of line number, or whatever. Perhaps that is because the error was not a syntax error but a perl module (HTTP::Request...etc.) error. Further investigation showed that after calling loginweb, my $all_ap($_}->{'webadminpostdata'} gets emptied. calling 'changewebaccess', nothing is being assigned to $postdata. That is why i'm getting that error. Any idea where to look into?? In my code before, I only had to declare the actual hashes:
my %webloginpostdata = (...); my %webadminpostdata = (...); </code ## Then pass their references to the sub routines: ie, <code> loginweb(\%webloginpostdata)
I didn't have any troubles doing so. Please enlighten me.

In reply to passing hash reference to HTTP::Request::Common destroys my other hash by markus_ben

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.