Link to home
Start Free TrialLog in
Avatar of pravink22
pravink22

asked on

Auto filling from a notepad

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();
____________________________________________________-
ASKER CERTIFIED SOLUTION
Avatar of Nem Schlecht
Nem Schlecht
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pravink22
pravink22

ASKER

Can you explain me the use of $input2_fixed =~ s/\r//g; ????
Without this also script is works gr8
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).
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