http://www.perlmonks.org?node_id=666411

Wanna lose weight comfortably? Here’s a script that I wrote to i) test the WWW::Mechanize module with forms and to ii) make it easy for me to update entries in www.fitday.com. Note that this script adds complex food that you eat regularly to your daily food recording; in this case it is a chicken wrap. You will have to add the ingredients yourself first through the FitDay search pages. Then you can add them to the array in the code below. After running the script change the amounts you consumed.
 
use strict; use WWW::Mechanize; use HTML::Form; use HTTP::Cookies; #use LWP; my $mech = WWW::Mechanize->new( autocheck => 1 ); my $form_name="loginForm"; my $Fname; #################################################################### # EDIT below ################################################################### # # my $username = "username"; my $password = "pw123456789"; my @myFoods = ( 'Cheese, natural, Cheddar or American type', 'Tortilla, whole wheat', 'Lettuce, raw', 'Chicken, ground', 'Cucumber, raw', 'Olives, black', 'Pepper, raw, NFS', 'Peppers, jalapeno, raw' ); # # #################################################################### # From hereon it should work automatically -STOP EDITING! # Goes to the fitday web site and logs in, deals with cookie #################################################################### $mech->get("http://www.fitday.com/WebFit/Index.html"); die "Can't get to the Fitday Page: ", $mech->response->status_line unless $mech->success; print "Found www.Fitday.com\n" if $mech->success(); $mech->form($form_name); die "Can't get the form: ", $mech->response->status_line unless $mech->success; my $cookie_jar = HTTP::Cookies->new( file => "cookies.txt", autosave => 1, ); $mech->cookie_jar($cookie_jar); $mech->field('LoginName',$username); $mech->field('Password',$password); print "Populated $form_name\n" if $mech->success(); my $results = $mech->submit(); print "Submitted $form_name\n" if $mech->success(); #################################################################### # After log in, find food #################################################################### foreach $Fname (@myFoods) { print "\nLet's add $Fname\n"; $mech->get("http://www.fitday.com/WebFit/FoodSearch.asp"); $mech->success or die "Can't find the search page"; print "Found Foodsearch page\n" if $mech->success(); #submit food to search filed $mech->field('Search',$Fname); my $results = $mech->submit(); $mech->success or die "Can't post to the search page"; print "Found Food: $Fname.\n" if $mech->success(); #find code for specific food on web-page, and if found, add th +e food using the URL my $page = $mech->content; if ($page =~ /FoodToAdd=(.*)\"><im/) { my $result = $1; $result=~ s/^\s*//; $result=~ s/\s*$//; #Add food via URL: $result= "http://www.fitday.com/WebFit/DayFoodsTab.asp?F +oodToAdd=$result"; $mech->get($result); $mech->success or die "Can't add food"; print "Added Food: $Fname.\n" if $mech->success(); } }