perl 中glob函式的使用

miguelmin發表於2009-01-17

Example

Try out following example: Here glob will retun all the files which begin with "perl_g"

#!/usr/bin/perl

(@file_list) = glob "perl_g*";

print "Returned list of file @file_listn";
[@more@] Syntax

glob EXPR

glob

Definition and Usage

Returns a list of files matching EXPR as they would be expanded by the standard Bourne shell. If the EXPR does not specify a path, uses the current directory. If EXPR is omitted, the value of $_ is used.

From Perl 5.6 on, expansion is done internally, rather than using an external script. Expansion follows the csh (and any derivatives, including tcsh and bash) style of expansion, which translates as the following:

  • Files beginning with a single period are ignored unless EXPR explicitly matches.
  • The * character matches zero or more characters of any type.
  • The ? character matches one character of any type.
  • The [..] construct matches the characters listed, including ranges, as per regular expressions.
  • The ~ characters matches the home directory; ~name matches the home directory for the user name.
  • The {..} construct matches against any of the comma-separated words enclosed in the braces.

Return Value

  • In scalar context undef on error otherwise First file in the list of expanded names

  • In list context Empty list on error otherwise List of expanded file names.

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16723161/viewspace-1016238/,如需轉載,請註明出處,否則將追究法律責任。

相關文章