Mr_Cassata has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

This is my first time at Perl Monks, and I'm pretty new at Perl, but not that new, lol. I have a question, which should be easy to solve, but that I can't seem to find the answer to. :-(

Here is the question:

I'm making a MS-DOS search program, using glob, and I can't seem to get the questions to appear one after the other. This is just the core of the program. I'll add error messages later:

$stdin = <STDIN> ; sub ext { print "Name the extension of the file that you want to find:" ; $ext = <STDIN> ; $e = $ext ; } sub name { print "Name the filename of the file that you want to find:" ; $file = <STDIN> ; $f = $file ; } sub dir { print "Name the directory that you want to search:" ; $dir = <STDIN> ; $d = $dir ; } opendir(DIR, $dir) ; $s = "$f\.$e" ; $x = glob($s) ; print ("Here are the file(s) that you requested: $x") ;

I know it might not work, but this is a project that I started a while ago, and I just got to trying to make it work. :-)

Thanks everyone. :-)

Mr Cassata

Edit kudra, 2002-05-13 Changed title

Replies are listed 'Best First'.
Re: Newbie in need...
by alien_life_form (Pilgrim) on May 12, 2002 at 21:19 UTC
    Greetings,

    • You should chomp() everything you get from <stdin> to remove trailing <CR><NL>
    • "Adding error messages later" is not a good idea when opening files, directories etc, because the operations will then fail silently - and you'll never know that something is wrong, or what. You should therefore always
      open(FOO,"$foo") or die($!)
      even at early developmental stages.

    Cheers,
    alf
    You can't have everything: where would you put it?
Re: Newbie in need...
by beamsack (Scribe) on May 12, 2002 at 21:10 UTC
    Mr Cassata Try using the chomp operator to remove the trailing newline of the input string. Example:
    chomp($ext = <STDIN>);
Re: Newbie in need...
by Dog and Pony (Priest) on May 12, 2002 at 23:47 UTC
    When it comes to the questions, you aren't calling your sub routines, so how could they? Adding
    &dir; &name; &ext;
    before you are trying to use the variables will at least ask the questions... but before anything else, you need to turn on strict and use -w. Please.

    Then, decide which method you want to use. Either the combination opendir/readdir or glob. They don't have anything to do with each other (so scratch the opendir for now, and read up on glob, if that is your chosen way). That is one of the reasons it will not return any files from directory $dir.

    I furthermore ask why you are doing:

    $ext = <STDIN> ; $e = $ext ;
    when
    $e = <STDIN> ;
    does the job? In reality, I see no use for such a small program to use subroutines at all, or at least use one, slightly bigger instead for this. I'd also consider returning values from them, if you have them, instead of setting global variables.

    Hmm... that was a lot of complaining. :( Sorry about that, but well, you would have a lot easier time if you listened to some of it at least. :)


    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.
      In reponse to dog and pony's suggestion to add:
      &dir; &name; &ext;
      Note that calling subs via '&' will pass @_ to the sub. If you don't want this behavior, I recommend using something more like:

      dir(); name(); ext();  #instead

      Not that he's wrong.. :) Just my two cents. Once bread becomes toast, it can never be bread again.
Programming craftmanship and Perl
by talexb (Chancellor) on May 13, 2002 at 02:10 UTC
    This is looks like a good case for top-down programming.

    List each of the steps required to get the job done. Make these comments in your program. After each comment, write code that does that thing, either as a subroutine call or a statement. Step through the program using the debugger, confirming that the variables have the values that you expect.

    Note that none of this is specific to Perl -- it's more a matter of programmer craftmanship.

    Also, I think you'll find that glob returns a list -- you have assigned a list to a single scalar, so the result may not be what you expect. Reference: p.84, The Camel, 3rd Edition

    Finally, you will get many people telling you to use strict and to use the -w flag when writing code -- even the most trivial examples. They are right. :) It will save you more time than you can imagine. Listen to what Perl is asking you to fix, and fix it.

    Happy coding!

    --t. alex

    "Nyahhh (munch, munch) What's up, Doc?" --Bugs Bunny

A reply falls below the community's threshold of quality. You may see it by logging in.