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

I am not a Perl developer. However, my client has an application that is embedded within a set of Perl routines - so that forces me to understand the basics.

I'm fine with most of the code except for a polling condition established by a WHILE loop based on the following DEFINED condition:

while (defined ($Variable = <$Path{Directory}\\*$FileExtension{TestFil +e}>))

My question: When is the above DEFINED condition true?

(Ancillary question: Is my following narrative understanding of the statement correct? - Perform while a file - any file - matching the pattern Path\*.Extension is recognized?)

I have performed numerous searches on the many Perl sites, but have had not luck. Any thoughts on where I can find an answer?

Thanks for your assistance/direction!

LazrD (first time in the monastery)

Retitled by Steve_p from 'Need direction'.

Replies are listed 'Best First'.
Re: When is defined true?
by runrig (Abbot) on Feb 07, 2005 at 20:38 UTC
    Just so that you are not confused later, the <EXPR> operator is documented in perlop. It can be a shorthand for either glob (reading filenames from a directory) or readline (reading lines from a file/IO handle), and these functions are documented in perlfunc.

    Also, the defined function is there because, if there were a file named "0" (zero) returned from glob(), then the while loop would end otherwise since "0" is false (but defined).

      Excellent replies. Thank you!

      I guess my only remaining issue would be with the $Variable = <EXPR> syntax.

      $Variable, when tested, is always null. I would think that by setting it equal to <EXPR>, it would take on the value of the file name. Correct?
      But seeing as how it is set in a function (the defined function), isn't its value always going to be null (or undefined) upon completion of the function?

      How do I test or display the contents of $Variable after it changes within the function? (Does that make sense the way I worded it?)

      LazrD

        guess my only remaining issue would be with the $Variable = <EXPR> syntax.

        $Variable, when tested, is always null. I would think that by setting it equal to <EXPR>, it would take on the value of the file name. Correct?

        In that case, EXPR is probably being interpreted as a filehandle (there are rules whether it calls glob() or readline(), check perlop), so you might be reading an unopened filehandle (or there's no lines in the file or you're calling glob and returning no file names), and you would get a warning to that effect (about unopened filehandles, that is) if you put this at the top of every script you write (and it's good habit, though don't just blindly do it on critical production scripts that don't have it yet, it'll most likely break'em):

        use strict; use warnings;
        If the scripts you are using don't have this, then I'd suspect the quality of them (or the age of them if you're still using Perl 4).
        How do I test or display the contents of $Variable after it changes within the function? (Does that make sense the way I worded it?)
        I'm not sure what you mean...you can print "$Variable\n"; Do you mean something else?

        Documentation for all these functions should already be installed on any system that perl is installed on. (with "perldoc" at the command line and/or html docs under the Perl directory if ActiveState perl is installed).

        Update: I'm thinking I may have misinterpreted that first question. In the while loop (in the original post), if $Variable is undefined, then I would temporarily add a print "$Path{Directory}\\*$FileExtension\n"; to see what pattern is being matched (maybe it's not what you expect), and/or maybe a use Cwd; print cwd(),"\n"; (maybe you're not in the directory you think you are).

Re: When is defined true?
by TedYoung (Deacon) on Feb 07, 2005 at 20:25 UTC

    Welcome

    Your understanding of the statement is correct. The <> operator returns files that match the specified pattern, in this case one at a time. Eventually, there will be no files left, in which case the <> operator returns undef (undefined). The function defined returns true as long as you pass it a value other than undef. So, in this case, the defined function returns false when there are no more files, effectivly ending the loop.

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)