Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messagechristopher.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";
}
Post Follow-up to this messageOn 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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.