having trouble with control structures

M

M. Duijkers

Heya... currently working my way through the book "learning perl". Just
having trouble with control structures.I want to do smth simple but can only
come with a bulky and ugly answer. What I want my program to do is "recieve
filenames from stdin ina loop, and when you recieve the string "QUIT" then
quit.
This is how I did it, but I just KNOW there is a better and easier way for
such a common task. Any sugs really appriciated
Mike.

#!perl -w
do
{
print "Enter filename or QUIT to stop: ";
$filename = <STDIN>;
chomp $filename;
if ($filename ne "QUIT")
{
if (! -e $filename)
{
print "file does not exist\n";
last;
}
if (-r $filename)
{
print "file is readable \n";
}
else
{
print "file is NOT readable \n";
}
if (-w $filename)
{
print "file is writeable\n";
}
else
{
print "file is NOT writeable \n";
}
if (-x $filename)
{
print "file is executable\n";
}
else
{
print "file is NOT excecuteable \n";
}
}
else
{
exit 0;
}
}while ($filename ne "QUIT")
 
P

Paul Lalli

M. Duijkers said:
Heya... currently working my way through the book "learning perl". Just
having trouble with control structures.I want to do smth simple but
can only

What is "smth"? Please do not use slang. Not everyone who reads this
group is a native English speaker.
come with a bulky and ugly answer. What I want my program to do is "recieve
filenames from stdin ina loop, and when you recieve the string "QUIT" then
quit.
This is how I did it, but I just KNOW there is a better and easier way for
such a common task. Any sugs really appriciated

same with "sugs".
Mike.

#!perl -w
do
{
print "Enter filename or QUIT to stop: ";
$filename = <STDIN>;
chomp $filename;
if ($filename ne "QUIT")
{

<snip a bunch of irrelevant file tests>
-- Please post a *short* but complete program that demonstrates your
problem, eliminating all other factors.
}
else
{
exit 0;
}
}while ($filename ne "QUIT")

So basically you want to know how to make this do-while loop without the
extra test for 'QUIT'?

while (chomp (my $filename=<STDIN>)){
last if $filename eq 'QUIT'){
#file tests...
}

Paul Lalli
 
T

Tore Aursand

#!perl -w
do
{
print "Enter filename or QUIT to stop: ";
$filename = <STDIN>;
chomp $filename;
if ($filename ne "QUIT")
{
if (! -e $filename)
{
print "file does not exist\n";
last;
}
if (-r $filename)
{
print "file is readable \n";
}
else
{
print "file is NOT readable \n";
}
if (-w $filename)
{
print "file is writeable\n";
}
else
{
print "file is NOT writeable \n";
}
if (-x $filename)
{
print "file is executable\n";
}
else
{
print "file is NOT excecuteable \n";
}
}
else
{
exit 0;
}
}while ($filename ne "QUIT")

Here is my suggestion for a solution;

#!/usr/bin/perl
#
use strict;
use warnings;

while ( <DATA> ) {
chomp;
last if ( $_ eq 'QUIT' );

if ( -e ) {
my %status = ('readable' => -r,
'writeable' => -w,
'executable' => -x);

my @status = map { ( $status{$_} ) ? $_ : 'NOT ' . $_ } keys %status;
print "'$_' is " . join( ', ', @status ) . "\n";
}
else {
print "'$_' doesn't exist!\n";
}
}
 
T

Tore Aursand

while ( <DATA> ) {

Should have been <STDIN> instead of <DATA>, of course; I tested it with a
list of filenames in the __DATA__ section of my Perl script.


--
Tore Aursand <[email protected]>
"Writing is a lot like sex. At first you do it because you like it.
Then you find yourself doing it for a few close friends and people you
like. But if you're any good at all, you end up doing it for money."
(Unknown)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top