Solved

Auto filling from a notepad

Posted on 2011-09-19
4
151 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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 video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now