Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Auto filling from a notepad

Posted on 2011-09-19
4
Medium Priority
?
161 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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 …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

721 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