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

Discussion in 'Perl' started by M. Duijkers, Dec 10, 2004.

  1. M. Duijkers

    M. Duijkers Guest

    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")
    M. Duijkers, Dec 10, 2004
    #1
    1. Advertising

  2. M. Duijkers

    Jason Hooper Guest

    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
    Jason Hooper, Dec 11, 2004
    #2
    1. Advertising

  3. M. Duijkers

    Joe Smith Guest

    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
    Joe Smith, Dec 12, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alfonso Morra
    Replies:
    11
    Views:
    713
    Emmanuel Delahaye
    Sep 24, 2005
  2. Michel Rouzic
    Replies:
    6
    Views:
    789
    A. Sinan Unur
    Dec 8, 2005
  3. bthumber
    Replies:
    2
    Views:
    430
    Alexey Smirnov
    Aug 26, 2009
  4. M. Duijkers

    having trouble with control structures

    M. Duijkers, Dec 10, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    94
    Tore Aursand
    Dec 15, 2004
  5. Isaac Won
    Replies:
    9
    Views:
    370
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page