For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2005 > Reading multi line input from user









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Reading multi line input from user
Christopher L Hood

2005-04-25, 3:56 pm

I wish to read input from a user that has multiple lines with carriage
returns from <stdin> but not actually stop reading until a single # on a
line by itself. Is there a module / package / function that will aid in
this? I would like to do something like below:



Until(<STDIN> = "#") {

$incoming_lines = <STDIN>;

}

$storage_var = $incoming_lines;





This is most like NOT working code, it is a logic example so that you
might understand what I am trying to accomplish, so please no comments on
the actual code shown.



Chris Hood


Bob Showalter

2005-04-25, 3:56 pm

christopher.l.hood@verizon.com wrote:
> I wish to read input from a user that has multiple lines with carriage
> returns from <stdin> but not actually stop reading until a single #
> on a line by itself.


You can use perl's $/ variable to define an record separator. The default is
"\n", but you can change it.

Try this:

$/ = "\n#\n";

while (<STDIN> ) {
chomp;
print "You entered: $_\n";
}
Jay Savage

2005-04-25, 8:55 pm

On 4/25/05, christopher.l.hood@verizon.com
<christopher.l.hood@verizon.com> wrote:
> I wish to read input from a user that has multiple lines with carriage
> returns from <stdin> but not actually stop reading until a single # on a
> line by itself. Is there a module / package / function that will aid in
> this? I would like to do something like below:
>=20
> Until(<STDIN> =3D "#") {
>=20
> $incoming_lines =3D <STDIN>;
>=20
> }
>=20
> $storage_var =3D $incoming_lines;
>=20
> This is most like NOT working code, it is a logic example so that you
> might understand what I am trying to accomplish, so please no comments on
> the actual code shown.
>=20
> Chris Hood
>=20
>=20


Chris,

It really depends on what you want to do and how you want to get your input=
..

my @input =3D <STDIN>;
chomp @input;

is probably the simplest. It relys on your OS to supply EOF and
terminate stdin. On unix systems, that usually means pressinf Ctrl-D.

my @input;
while (<STDIN> ) {
chomp;
push @input, $_;
}

Is another option. This lets you parse the text as it's entered, so
you can easily do things like create a custom EOF:

while (<STDIN> ) {
chomp;
last if /^#$/;
push @input, $_;
}

You could also replace that push with:

$input .=3D " $_";

or something similar if you desperately wanted a scalar for some reason.

And finally, you could change the value of $/. Just make sure you've
properly localized it, and no other files will be read in the same
scope. Something like:

sub get_input {
local $/ =3D "\n#\n";
my $input =3D <STDIN>;
return $input
}

will work fine, but you can get into a lot of trouble very quickly by
doing something like:

$/ =3D "\n#\n";
my $input =3D <STDIN>;
open(FH, "<", "my/foo.txt");
while (<FH> ) {
do something if ((/something/) and ($input =3D~ /something else/));
}

HTH,

--jay
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com