How does glob return "filenames" that are not there?

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, this code returns an empty list when I run it.

What am I missing?


This launch was done from the command line using -e, on Windows XP, with the ActiveState Perl version 5.005_02. Executing from a saved script on the same computer gives the same results.

Working with -e on Windows XP with ActiveState Perl v5.8.7 returns an array.

+2


source to share


3 answers


Perl version 5.005_02 is ancient (in Perl terms). This version probably has a different implementation glob

that doesn't return filenames that don't exist. As you can see, later versions of Perl work differently.



+5


source


This 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♣

      

+2


source


This works well for me - i.e. creates a deck of cards, I am using perl 5.8.8.

But. Using glob for this seems odd - I mean - of course it is possible, but glob is a file matching tool that does not guarantee actual file validation, but no one says it will not be file compatible in the future!

I would definitely go with a different approach. For example, for example:

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 {map} too cryptic:

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

      

+1


source







All Articles