Solved

Auto filling from a notepad

Posted on 2011-09-19
4
158 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Remove Malware code from PHP file 6 107
Migrate OTRS to newest version. 1 607
Awk Question 2 136
Perl Application JSON Receiving Strange Java Error 3 86
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…

739 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