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

Greetings all,
I was talking with a guy at work today and he had something like this:
opendir AREA, "/files1/logs"; @job_logs = grep /job_log/, readdir AREA; print "Got my list of files\n"; while (<@job_logs>) { print "File: $_\n"; }
I'd never seen the angle operator used on an array before, but it seems to do the right thing, just really slowly. Does anyone have any ideas as to what is happening? I couldn't find any references anywhere. I've since suggested something like this:
while(</files1/logs/job_log*> { print "Log: $_\n"; }
So, the question is more academic at this point.

thanks,
thor

Replies are listed 'Best First'.
Re: Why is @array so slow?
by japhy (Canon) on Feb 26, 2002 at 03:09 UTC
    Here's a clue:
    while (<john jacob jingleheimer schmidt>) { print "name = $_\n"; }
    Bizarre, eh?

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a (from-home) job
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Why is @array so slow?
by dws (Chancellor) on Feb 26, 2002 at 05:14 UTC
    I'd never seen the angle operator used on an array before, but it seems to do the right thing, just really slowly.

    You're stepped into a subtle one.   while (<@job_logs>) might do what you expect it to, but not for the reasons you think. Consult perlop, and read up on file globs.

      I've been a bit busy the last couple of days, so I apologize for the late response. I just read perlop, and, if I may, I'd like to bounce my understanding of what is happening off of those more knowledgable.

      When I pass anything beside a file handle or a simple scalar to the <> operator, it passes the stuff inside the operator to the glob function. I'd imagine globbing 6000+ elements is a bit slow.

      What I don't understand is why japhy's example works. I would expect that it would try to glob (john jacob jingleheimer schmidt), not see anything like that in the directory, and evaluate to nothing. Oh well.<
        What I don't understand is why japhy's example works. I would expect that it would try to glob (john jacob jingleheimer schmidt), not see anything like that in the directory, and evaluate to nothing.

        glob() either mimics the shell or delegates to it for pattern expansion. Consider the difference between

        % echo foo foo %
        and
        % echo foo* echo: No match. %
        when there are no files begining with "foo" in the current directory.