tags:

views:

182

answers:

3

This answer indicates that glob can sometimes return 'filenames' that don't exist.

@deck = glob "{A,K,Q,J,10,9,8,7,6,5,4,3,2}{\x{2660},\x{2665},\x{2666},\x{2663}}";

However, that code returns an empty list when I run it.

What am I missing?


This run was from the command prompt using -e, on Windows XP, with ActiveState Perl version 5.005_02. Running from a saved script on the same machine yields the same results.

Running with -e on Windows XP, with ActiveState Perl v5.8.7, does return an array.

+2  A: 

That works correctly on my machine, v5.10.0.

#!/usr/bin/perl

@deck = glob "{A,K,Q,J,10,9,8,7,6,5,4,3,2}{\x{2660},\x{2665},\x{2666},\x{2663}}";

print @deck

gives as output:

A♠A♥A♦A♣K♠K♥K♦K♣Q♠Q♥Q♦Q♣J♠J♥J♦J♣10♠10♥10♦10♣9♠9♥9♦9♣8♠8♥8♦8♣7♠7♥7♦7♣6♠6♥6♦6♣5♠5♥5♦5♣4♠4♥4♦4♣3♠3♥3♦3♣2♠2♥2♦2♣
Carl Norum
Hmmm. It appears to "work" on my v5.8.7 machine, but not on the v5.005_02. New feature, perhaps?
Rini
From perldoc-f glob: "Beginning with v5.6.0, this operator is implemented using the standard "File::Glob" extension." I expect the pre-File::Glob implmentation worked quite differently.
Ether
Looks like I missed out on some rep by posting that as a comment rather than a response. Upvoters sure are fickle :)
Ether
+5  A: 

Perl version 5.005_02 is ancient (in Perl terms). That version probably has a different implementation of glob that doesn't return names of files that don't exist. As you've noticed, later versions of Perl work differently.

Greg Hewgill
Indeed. Versions of Perl prior to 5.6 did not use the File::Glob that comes with Perl. And, not only if 5.005_02 ancient, it's even ancient in the 5.005 series.
brian d foy
Well, I deleted my answer. This seems to be the correct one. A hint: according to perlport, don't count on globbing.
Leonardo Herrera
Also, the utf8 hex codepoint syntax \x{xxxx} doesn't work on 5.005.
ysth
+1  A: 

It works well for me - i.e. it generates deck of cards, I'm using perl 5.8.8.

But. Using glob for this seems to be strange - I mean - sure, it's possible, but glob is a tool to match files which is not guaranteed to actually check for the files, but nobody says that it will not match the files in the future!

I would definitely go with another approach. For example like this:

my @cards = qw( A K Q J 10 9 8 7 6 5 4 3 2 );
my @colors = ( "\x{2660}", "\x{2665}", "\x{2666}", "\x{2663}" );

my @deck = map {
    my $card = $_;
    map { "$card$_" } @colors
} @cards;

Or, if you find the map{map} too cryptic:

my @deck;
for my $card ( @cards ) {
    for my $color ( @colors ) {
        push @deck, "$card$color";
    }
}
depesz