Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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♣

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";
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top