Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^8: Working with Binary Numbers

by CountZero (Bishop)
on Sep 25, 2007 at 20:48 UTC ( [id://641038]=note: print w/replies, xml ) Need Help??


in reply to Re^7: Working with Binary Numbers
in thread Working with Binary Numbers

Beginning with perl 5.6.0 glob uses File::Glob which despite its name is not limited to be used on files.

Actually the argument to glob uses a little language of its own which describes the pattern to search for with meta-characters: (from the docs)

\ Quote the next metacharacter [] Character class {} Multiple pattern * Match any string of characters ? Match any single character ~ User name home directory The metanotation a{b,c,d}e is a shorthand for abe ace ade. Left to rig +ht order is preserved, with results of matches being sorted separatel +y at a low level to preserve this order. As a special case {, }, and +{} are passed undisturbed.

blokhead (ab)uses this pattern-description language by replacing each '-' with {1, 0} to give that placeholder a possible value of 1 OR 0.

glob("1{1,0}1") will happily expand to 111 and 101, since unless you include a wildcard character (* or ?) or a character class in your pattern it will NOT go to the filesystem to give you back the list of files but rather expands immediately to all possible values which are allowed by the pattern. It seems to be a (useful) side-effect of the pre-filesystem lookup pre-processing of the pattern to expand {pat,pat,...} strings.

Updated: as per tye's comments

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^9: Working with Binary Numbers (" )
by tye (Sage) on Sep 26, 2007 at 14:50 UTC
    glob(1{1, 0}1) will happily expand to 111 and 101

    To be clearer, make that glob("1{1,0}1") since my quotes and your space each make a big difference.

    - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://641038]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-19 17:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found