Angle Brackets vs. foreach

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I recently encountered a performance problem which was fixed by


foreach $file(@files)

Can anyone explain the reason for the performance hit?  My program took
3 minutes using the <> operator, vs. about 2 seconds using foreach.

I realize that the angle brackets operator <> is not really for list
iteration.  But when you evaluate it in scalar context, it does return
the contents of the list, one by one.

What is Perl doing that causes such a hit?



Re: Angle Brackets vs. foreach

On 13 Apr 2005, wrote:

Quoted text here. Click to load it

It does, but only after passing them through the glob() function.
This is what slows you down.  It does make sense sometimes, if you
expect wildcards in your file names and want to make sure they get

Look at "perldoc -f glob" and go from there.


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- The #1 Newsgroup Service in the World! 120,000+
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Re: Angle Brackets vs. foreach

On Wed, 13 Apr 2005 08:37:27 -0700, Nathan.Neff wrote:

Quoted text here. Click to load it

Not really. You changed the code so that it did something different which
took less time. Probably. It's hard to say for sure.

The while and the foreach do different things.

From "perldoc perlop":

"If what's within the angle brackets is neither a filehandle nor a simple
scalar variable containing a filehandle name, type-glob, 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."

As I understand the above, @files will be used as a pattern to be globbed.
This means the elements of @files will be joined with the value of $" and
the resulting string will be globbed. The default value of $" is a space,
which will result in such a string that the effect will be that each
element of @files will be treated as a pattern to be globbed. IFF no
filename is returned as a result of globbing a particular pattern, will
the pattern itself be returned.

So the number of iterations of the while loop above depends on the
contents of @files and the names of the files in the current directory for
your executing script.

For example, if your CWD contains these files: 1.txt 2.txt 3.dat
then these values of @files will return the given lists when globbed using

@files = ('txt') returns ('txt') # no match (no wildcard)
@files = ('*txt') returns ('1.txt','2.txt') # 2 matches
@files = ('*txt','dat') returns ('1.txt','2.txt','dat') # no wildcard
@files = ('*txt','*dat') returns ('1.txt','2.txt','3.dat')

Quoted text here. Click to load it

This merely iterates over the elements of @files in the standard way.

Quoted text here. Click to load it

Not exactly. See above.

Quoted text here. Click to load it

It's hard to say without knowing the contents of your loop.


Site Timeline