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

I have been wrestling with this one for 2 days now, trying to come up with a rule that works. Let me describe exactly what I'm trying to accomplish:

my $dir = "/home/limo/Perl/2000/0827/steves/router/configs";
#new "yyyy/mmdd" are created daily

"Enter date of config file using < yyyy/mmdd >: ";  
chomp ( $date_cfg = <STDIN>);
# date of file that user wants to process

#code here which grabs the "2000/0827" directories: works fine
#code here that converts "2000/0827" to "08/27/2000" via s///
#stores it in "$new_date" for writing to STDOUT: works fine

print "Looking for Juniper router configuration file(s) from: $new_date\n";
Here's where my problem lies: I need the program to take the previous input from the user in "yyyy/mmdd" format, and plug it into $dir, and list files in the chosen directory.

This bombs:

opendir(DIR, $dir/$date_cfg)  or die "Can't read from $dir/date_cfg: $!\n";


as well as:

opendir(DIR, $dir/$new_date)  or die "Can't read from $dir/date_cfg: $!\n";
The program lists directory files just fine, if I hard code the path; I'm just having problems passing the correct directory argument to the code.
  • Comment on Search for file using element of path as file name

Replies are listed 'Best First'.
(Ovid) Re: Search for file using element of path as file name
by Ovid (Cardinal) on Aug 27, 2000 at 07:47 UTC
    You need to enclose $dir/$new_date in quotes. Otherwise, Perl assumes that you are trying to perform division with two scalars.
    opendir (DIR, "$dir/$date_cfg") or die "Can't read from $dir/date_cfg: + $!\n";
    In fact, ordinarily you would have spotted this problem right away. Perl will generate an error similar to the following:
    Argument "new_date" isn't numeric for division at somescript.cgi line +15. Illegal division by zero at somescript.cgi line 15.
    The "isn't numeric" warning occurs if you use the -w switch, but does not stop the program. However, since Perl will do string/numeric conversions on the fly, it will usually result in a division by zero error, which does kill the program. Only because you are using numeric data in your second argument are you not getting an error.

    Cheers,
    Ovid

Re: Search for file using element of path as file name
by tye (Sage) on Aug 27, 2000 at 09:02 UTC

    Please put your code between <code> and </code> as describe in the site documentation and as I mentioned to you two or three times in the chatterbox. Your <s and >s seem to be getting stripped from your code because you aren't quoting your code the proper way.

            - tye (but my friends call me "Tye")
      Huh? Actually, nowhere in this post did I use the < or > symbols. Please refer to "page source" if your browser supports it. I HAVE been using tags to display code, again as evidenced by my post. Please tell me what's wrong with the way it is displayed, and I will happily change my evil ways.

        Did you read your own posting? It contained this:

        "Enter date of config file using : "; chomp ( $date_cfg = );

        If you didn't use < nor > then your code is seriously broken and won't even compile. It seemed clear that your second line was originally something like:

        chomp ( $date_cfg = <STDIN> );

        And having such things stripped is only one consequence of you ignoring simple instructions. Try the "d/l code" link under your posting. It doesn't download any code, does it? I was quite specific in mentioning <code> and </code> tags. I'm sorry I didn't take the time to explain in detail why I recommended what I did.

                - tye (but my friends call me "Tye")