not finding <file>  the 2nd time it's called

Posted on 2007-10-19
Last Modified: 2010-03-05
I have a list of files that needs processing.  
I created a sub routine that processes each file.
The 2nd time the sub is called, the line: my $fileIn = <$dirfile >  returns an undef.
I then get a can't find file error.

Here is a snippet of sub code I'm using

#! /usr/bin/perl
$|=1 ;  
$dirfile = "./final_erc/*.log" ;
&process_file ;
$dirfile = "./final_drc/*.log" ;
&process_file ;

sub process_file ;
   my $fileIn = <$dirfile> ;  #  < returns undef on 2nd call
   my $fileout = $dirfile . "_sum" ;
   open (LOG , "<$fileIn") or die "$!\n" ;
} # sub

Question by:ISGDude
    LVL 17

    Expert Comment

    Does ./final_drc exist?  Are there any files matching *.log in this directory?  Do you chdir() anywhere in the rest of the script between the first and second call to the process_file sub?

    Also, "./final_erc/*.log" would be expected to return a list of results (all the files matching *.log).  Is there a reason you're populating a scalar with this, rather than, say, an array?

    Author Comment

    The files exists.  If I swap the order, the 2nd one still fails.

    I do not use chdir.  The main reason is that I couldn't get the equivalent of cd .. to work and had to move on to the next problem.  

    Only one log file exists in each directory.  
    I  have the array checking code rem'ed out for now, until I get the basic functions to work.  

    Author Comment

    I put   sleep 5 ;
    at the end of the sub and it still fails on the 2nd time thourgh.

    LVL 84

    Accepted Solution


    sub process_file {
       my ($fileIn) = <{$dirfile}> ;  #  < returns undef on 2nd call
       my $fileout = $fileIn . "_sum" ;
       open (LOG , "<$fileIn") or die "$fileIn $!\n" ;

    Author Comment

    That works.
    What is it doing and why do you need it?
    LVL 84

    Expert Comment

    In scalar context, glob iterates through
                   such filename expansions, returning undef when the list is
    ($fileIn) =
    puts it in array context
    LVL 84

    Expert Comment

          If what's within the angle brackets is neither a filehandle nor a sim-
           ple scalar variable containing a filehandle name, typeglob, or typeglob
           reference, it is interpreted as a filename pattern to be globbed, and
           either a list of filenames or the next filename in the list is
           returned, depending on context.  This distinction is determined on syn-
           tactic grounds alone.  That means "<$x>" is always a readline() from an
           indirect handle, but "<$hash{key}>" is always a glob().  That's because
           $x is a simple scalar variable, but $hash{key} is not--it's a hash ele-

           One level of double-quote interpretation is done first, but you can't
           say "<$foo>" because that's an indirect filehandle as explained in the
           previous paragraph.  (In older versions of Perl, programmers would
           insert curly brackets to force interpretation as a filename glob:
           "<${foo}>".  These days, it's considered cleaner to call the internal
           function directly as "glob($foo)", which is probably the right way to
           have done it in the first place.)  For example:

               while (<*.c>) {
                   chmod 0644, $_;

           is roughly equivalent to:

               open(FOO, "echo *.c | tr -s ' \t\r\f' '\\012\\012\\012\\012'|");
               while (<FOO>) {
                   chmod 0644, $_;

           except that the globbing is actually done internally using the standard
           "File::Glob" extension.  Of course, the shortest way to do the above

               chmod 0644, <*.c>;

           A (file)glob evaluates its (embedded) argument only when it is starting
           a new list.  All values must be read before it will start over.  In
           list context, this isn't important because you automatically get them
           all anyway.  However, in scalar context the operator returns the next
           value each time it's called, or "undef" when the list has run out.  As
           with filehandle reads, an automatic "defined" is generated when the
           glob occurs in the test part of a "while", because legal glob returns
           (e.g. a file called 0) would otherwise terminate the loop.  Again,
           "undef" is returned only once.  So if you're expecting a single value
           from a glob, it is much better to say

               ($file) = <blurch*>;


               $file = <blurch*>;

           because the latter will alternate between returning a filename and
           returning false.

           If you're trying to do variable interpolation, it's definitely better
           to use the glob() function, because the older notation can cause people
           to become confused with the indirect filehandle notation.

               @files = glob("$dir/*.[ch]");
               @files = glob($files[$i]);
    LVL 17

    Expert Comment

    Which is why I was asking whether you should really wanted to be reading it into @fileIn rather than $fileIn...

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Suggested Solutions

    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now