Solved

Auto filling from a notepad

Posted on 2011-09-19
4
156 Views
Last Modified: 2012-06-27
Hi,

In the below script, when I tried to get input from a notepad.txt and paste it on a url, I can see only the first line of the notepad only posted, can someone help me in fixing this?
______________________________________________
use WWW::Mechanize;
$mech = WWW::Mechanize->new();
$input1 = "TITLE";
open(FILE,"notepad.txt");
@input2 = <FILE>;
$mech->get("URL");
$mech->form_with_fields(("subject","Content"));
$mech->field("subject",$input1);
$mech->field("Content",@input2);
$mech->submit_form();
____________________________________________________-
0
Comment
Question by:pravink22
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
nemws1 earned 500 total points
ID: 36561547
Notepad, being a Windows program, ends lines differently than what Perl is expecting (it has both a newline and a carriage return).

Try this:
use WWW::Mechanize;
$mech = WWW::Mechanize->new();
$input1 = "TITLE";
open(FILE,"notepad.txt");
@input2 = <FILE>;
my $input2_fixed = join('', @input2);    # join array into string
$input2_fixed =~ s/\r//g;                # remove all carriage returns
$mech->get("URL");
$mech->form_with_fields(("subject","Content"));
$mech->field("subject",$input1);
$mech->field("Content",$input2_fixed);   # submit fixed string
$mech->submit_form();

Open in new window

0
 

Author Comment

by:pravink22
ID: 36561935
Can you explain me the use of $input2_fixed =~ s/\r//g; ????
Without this also script is works gr8
0
 
LVL 23

Expert Comment

by:nemws1
ID: 36562022
I guess it doesn't matter, then.  The key must have been to pass a string instead of an array.  Either way, that line should just help (it shouldn't hurt).

This line is supposed to remove all the carriage returns.  They're special characters, like a newline or a Tab, but Perl can deal with them using the special character "\r" (yes, backslash, then r).  Just line "\n" is for a newline.  This regex:
 
$input2_fixed =~ s/\r//g;

Open in new window

Says take everything in $input2_fixed, apply a regex and apply it back to the variable "=~", the regex says to substitute "s" a carriage return (\r) with nothing (there are two slashes with nothing between them) and apply that change globally (the trailing 'g'), meaning don't just remove the *first* carriage return you find, but *all* of them.

Basically, it just searches through $input2_fixed and removes all of the carriage returns.  The resulting string is put back into $input2_fixed (so no temporary variable).
0
 
LVL 9

Expert Comment

by:parparov
ID: 36562320
You can even shorten this code by doing:
$/ = undef;
open(FILE,"notepad.txt");
my $input2_fixed = <FILE>;
close FILE;

Open in new window

instead of current lines 4-7
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question