Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl (http://www.velocityreviews.com/forums/f17-perl.html)
-   -   newbie having trouble with control structures: loop till recieve string QUIT (http://www.velocityreviews.com/forums/t25426-newbie-having-trouble-with-control-structures-loop-till-recieve-string-quit.html)

M. Duijkers 12-10-2004 08:04 PM

newbie having trouble with control structures: loop till recieve string QUIT
 
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")




Jason Hooper 12-11-2004 04:39 AM

Re: newbie having trouble with control structures: loop till recievestring QUIT
 
M. Duijkers wrote:
> 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

Joe Smith 12-12-2004 11:02 AM

Re: newbie having trouble with control structures: loop till recievestring QUIT
 
M. Duijkers wrote:
> 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


All times are GMT. The time now is 02:27 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.