2

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.

Community
  • 1
  • 1
Rini
  • 197
  • 2
  • 7
  • Please provide more details about how you ran it. (e.g., complete code, Perl version.) – Rob Kennedy Oct 05 '09 at 21:01
  • 1
    Wow, I never realized this method worked like that. This is a nice method for performing a quick-and-dirty combinatorial permutation on sets! – Ether Oct 05 '09 at 21:14
  • Whenever I run into these things, where I think a function isn't doing what I think it should do, I read the latest documentation. That would have been a big clue for you. :) – brian d foy Oct 05 '09 at 21:43
  • 1
    I read the documentation (via perldoc.perl.org) for File::Glob and for glob, but neither seems to explain this. The only mention I see is GLOB_NOCHECK (and/or GLOB_NOMAGIC), which seem to be 'options' that make it return the pattern when there are no matches. Is that what is happening? Is there some indicator I'm missing that shows these options to be enabled by default? – Rini Oct 05 '09 at 22:08

3 Answers3

5

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
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 2
    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 Oct 05 '09 at 21:41
  • 1
    Well, I deleted my answer. This seems to be the correct one. A hint: according to perlport, don't count on globbing. – Leonardo Herrera Oct 05 '09 at 21:54
  • Also, the utf8 hex codepoint syntax \x{xxxx} doesn't work on 5.005. – ysth Oct 06 '09 at 04:50
2

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
  • 219,201
  • 40
  • 422
  • 469
  • Hmmm. It appears to "work" on my v5.8.7 machine, but not on the v5.005_02. New feature, perhaps? – Rini Oct 05 '09 at 21:09
  • 4
    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 Oct 05 '09 at 21:16
  • Looks like I missed out on some rep by posting that as a comment rather than a response. Upvoters sure are fickle :) – Ether Oct 05 '09 at 23:10
1

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";
    }
}