newbie having trouble with control structures: loop till recieve string QUIT

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")
 
J

Jason Hooper

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
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.

It could be somewhat more perlish this way:

while (1)
{
print "Enter filename or QUIT to stop: ";

chomp($filename = <STDIN>);

last if $filename eq "QUIT";

unless (-e $filename)
{
print "File does not exist\n";
last;
}

print "file is readable\n" if (-r $filename);
print "file is not readable\n" unless (-r $filename);

print "file is writeable\n" if (-w $filename);
print "file is not writeable\n" unless (-w $filename);

print "file is executableable\n" if (-x $filename);
print "file is not executableable\n" unless (-x $filename);
}

- Jason
 
J

Joe Smith

M. Duijkers said:
if (-r $filename)
{
print "file is readable \n";
}
else
{
print "file is NOT readable \n";
}

print 'file is ', (-r $filename ? 'readable' : 'NOT readable'), "\n";

Any further questions should be posted to comp.lang.perl.misc
instead of here (comp.lang.perl).
-Joe
 

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

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top