Perl Mechanize

WWW-Mechanize


I used Perl mechanize to test my web application. Found it awesome. If you have plans to test your web app then, give a try with mechanize. It will make your job easier. 
Thanks to Andy Lester . Download WWW::Mechanize from here


lets see a small example with www::mechanize


Load necessary modules.
#!/usr/bin/perl -w
use strict;
use WWW::Mechanize;


Create agent
# create agent
my $mech = WWW::Mechanize->new();
$mech->get("http://search.cpan.org");
if(! $mech->success) {
   print "Failed to load\n";
}


Get the list of module category URL 

# Get all the links from the page that matches the given pattern.
my @links = $mech->find_all_links(url_regex => qr/modlist\/(.*)/);
foreach my $modlist (@links) {
 print("The URL is " . $modlist->url_abs . "\n");
}

The output of the above will be like this



The URL is http://search.cpan.org/modlist/Archiving_Compression_Conversion
The URL is http://search.cpan.org/modlist/File_Name_System_Locking
The URL is http://search.cpan.org/modlist/Option_Parameter_Config_Processing
The URL is http://search.cpan.org/modlist/Bundles
The URL is http://search.cpan.org/modlist/Graphics
The URL is http://search.cpan.org/modlist/Perl6
The URL is http://search.cpan.org/modlist/Commercial_Software_Interfaces
The URL is http://search.cpan.org/modlist/Internationalization_Locale
The URL is http://search.cpan.org/modlist/Pragmas
The URL is http://search.cpan.org/modlist/Control_Flow_Utilities
The URL is http://search.cpan.org/modlist/Language_Extensions
The URL is http://search.cpan.org/modlist/Security
The URL is http://search.cpan.org/modlist/Data_and_Data_Types
The URL is http://search.cpan.org/modlist/Language_Interfaces
The URL is http://search.cpan.org/modlist/Server_Daemon_Utilities
The URL is http://search.cpan.org/modlist/Database_Interfaces
The URL is http://search.cpan.org/modlist/Mail_and_Usenet_News
The URL is http://search.cpan.org/modlist/String_Language_Text_Processing
The URL is http://search.cpan.org/modlist/Development_Support
The URL is http://search.cpan.org/modlist/Miscellaneous
The URL is http://search.cpan.org/modlist/User_Interfaces
The URL is http://search.cpan.org/modlist/Documentation
The URL is http://search.cpan.org/modlist/Networking_Devices_IPC
The URL is http://search.cpan.org/modlist/World_Wide_Web
The URL is http://search.cpan.org/modlist/File_Handle_Input_Output
The URL is http://search.cpan.org/modlist/Operating_System_Interfaces


Now lets see how to use forms. On top of the page you can see the search form. Lets search for mechanize. To do that,



1  #search something
2  $mech->submit_form(
3    fields => {
4        query => 'mechanize'
5     }
6   );






In line number 4 query is the name of the form field.





< \input type="text" name="query" value="mechanize" size="35"> 

Fetch all the urls from search output
my @search_links = $mech->find_all_links();

Get all the URL that ends with .pm extension 
foreach my $search_list (@search_links) {
    if ($search_list->url_abs =~ /(.*).pm/) {
        print "I found mechanize module and the URL is $1 \n";
    }
}


Note: @search_links bring only the 1st page's URLs. The actual search output have more than 6-7 pages.


Using Fork 
If you think the test takes more time then you can use fork to create a child process.


Example to create a child process:





 if ( !defined ($child_pid = fork()) ) {
  die "cannot fork: $!";
  }
  elsif ($child_pid == 0) {
  # I'm the child
  }
  else {
  # I'm the parent
  } 

If you have a array with huge number of URLs, you can split that into two and process them in child and parent. This will make the task easier.

Source code:


#!/usr/bin/perl -w


use strict;
use WWW::Mechanize;


# create agent
my $mech = WWW::Mechanize->new();
$mech->get("http://search.cpan.org");
if(! $mech->success) {
   print "Failed to load\n";
}


# Get all the links from the page that matches the given pattern.
my @links = $mech->find_all_links(url_regex => qr/modlist\/(.*)/);


foreach my $modlist (@links) {
 print("The URL is " . $modlist->url_abs . "\n");
}


#search something
$mech->submit_form(
    fields => {
        query => 'mechanize'
    }
);


my @search_links = $mech->find_all_links();
foreach my $search_list (@search_links) {
    if ($search_list->url_abs =~ /(.*).pm/) {
        print "I found mechanize module and the URL is $1 \n";
    }
}

See also:
Test::WWW::Mechanize
Test::WWW::Mechanize::Catalyst

Savior to google notebook

Google Notebook has announced that is has stopped development. Which means new users cant sign-up for this feature but the existing users can still use the service. Google notebook is knows for its: simple, design, effective, friendly, and well suited to a getting things done.Its one of the best tool of google and i use it extensive. Keep it alive. Petition to save Google Notebook. So now its time look for alternatives. I found some good alternatives for google notebook.
Evernote is worth a try. It handles web links, text notes, pictures, voice memos. Overall its a rich media note-taking system. With evernote its easy to import your google notebook notes.
Zoho Notebook is very similar to googe notebook. It handles text, image, audio, video etc. It also has a version control, Zoho Notebook plugin for firefox. You can even import your google notebook.
To import your notes from Google Notebook, select a notebook, click “export”, choose the Atom option, and save to disk. Repeat this step for each notebook you want to import into Evernote. When you’re done,
Login into the Evernote website
click on Settings, find the “Import into Evernote” section, click the link, and then select Google Notebook. You’ll need to import notebooks one by one.
Below is the video to help yourself

Login into the Zoho Notebook website
Click Import Google notebook, on top of the page. Start importing notes one by one.
Thats simple.
Have a look at this to try some other note taking applications.

Want to try Catalyst

Catalyst is open-source perl based elegent MVC framework(http://dev.catalystframework.org/wiki/). Check out http://www.perl.com/lpt/a/930 to know more about Catalyst.

Lets see the steps to install catalyst in a easy way.


Download svndump.gz from http://handelframework.com/downloads/
Extract/unzip the items from svndump.gz



For windows:

open a command prompt
-------------------------
  • Goto -> Run->cmd


Change directory to CatInABox/bin
------------------------------------


> cd CatInABox/bin folder
Run catalyst.bat
----------------
Run catalyst.bat along with your Application name. Say my application name is 'Testapp', i would run


> catalyst.bat Testapp
















Now you are done with creating your application. To start the server, change directory to your Testapp/script and start the server.


cd Testapp/script

perl testapp_server.pl
Now your application is up and running. To view it. Goto browser and check
http://localhost:3000 . you should be able to view the application.
For unix flavors:
Repeat the same procedure for unix boxes. Instead of catalyst.bat use catalyst.pl


catalyst.pl Testapp
Which will create the same structure as in windows.



To create a TT view, run:
$ script/testapp_create.pl view TT TT
This creates the lib/Testapp/View/TT.pm module, which is a subclass of Catalyst::View::TT.



To create a new Controller, run:(say i am creating controller User)
$ perl script/testapp_create.pl controller User
This will create "/lib/TestApp/Controller/User.pm" and "/t/controller_user.t"dot(.) t files are used for testing.
To create a database Model
Before creating model, read: http://www.perlmonks.org/?node_id=700283
You need a database, it is recommened to use SQLite, if your application is small. Since its a "embedded" database. so the database exists as a single file and requires no server to run.
$ $ perl script/testapp_create.pl model TestDb DBIC::Schema \ Testapp::Schema::TestDb create=dynamic \ dbi:SQLite:/tmp/database