Dec 23, 2010

Globbing

Globbing a Directory:

The shell (or whatever your command-line interpreter is) takes a solitary asterisk ( * ) command-line argument and turns it into a list of all of the filenames in the current directory. So, when you say rm * , you'll remove all of the files from the current directory. (Don't try this unless you like irritating your system administrator when you request the files to be restored.) Similarly, [a-m]*.c as a command-line argument turns into a list of all filenames in the current directory that begin with a letter in the first half of the alphabet and end in .c , and /etc/host* is a list of all filenames that begin with host in the directory /etc . (If this is new to you, you probably want to read some more about shell scripting somewhere else before proceeding.)

  • The expansion of arguments like * or /etc/host* into the list of matching filenames is called globbing

  • Perl supports globbing through a very simple mechanism: just put the globbing pattern between angle brackets or use the more mnemonically named glob function.
 
Example: 

 1.) @a = </etc/host*>; 
 2.) @a = glob("/etc/host*");
 
 3.) @files = <*>;
     foreach $file (@files) 
     {
        print $file . "\n";
     }  

No comments:

Post a Comment