l.frankline has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

The following code is working fine for listing *.xml files.

How could I list for two formats like *.xml and *.htm

Please suggest...thank u

use File::Find::Rule; my @files = File::Find::Rule->file() ->name( '*.xml' ) ->in( "." ); foreach $fname (@files) { print $fname; }
Regards,
Franklin
Don't put off till tomorrow, what you can do today.

jdporter fixed code tags

Replies are listed 'Best First'.
Re: Listing various types of files
by Corion (Patriarch) on Nov 16, 2005 at 15:27 UTC

    Maybe by adding a second call to retrieve .htm files?

    use File::Find::Rule; my @files = File::Find::Rule->file() ->name( '*.xml' ) ->in( "." ); foreach $fname (@files) { print $fname; } @files = File::Find::Rule->file() ->name( '*.htm' ) ->in( "." ); foreach $fname (@files) { print $fname; }

    Honestly, you seem to be asking quite basic questions that resemble quite a lot some homework questions or questions a corrector would ask of you if you copied some code from your coeds. That's also why I'm giving you a quite suboptimal solution. You can find a much better solution by reading the documentation of File::Find::Rule.

Re: Listing various types of files
by Roy Johnson (Monsignor) on Nov 16, 2005 at 16:12 UTC
    glob patterns are not limited to asterisks. Try -name( '*.{xml,htm}'). Note that File-Find-Rule also allows you to use a list and regular expression for the -name argument.

    RTFM


    Caution: Contents may have been coded under pressure.
Re: Listing various types of files
by ikegami (Patriarch) on Nov 16, 2005 at 16:23 UTC
    Alternatively, there's an or rule:
    use File::Find::Rule; my @files = File::Find::Rule->file() ->or( File::Find::Rule->name( '*.xml' ) ->name( '*.htm' ) ) ->in( "." ); foreach $fname (@files) { print $fname; }

    Ug, let's make that less horizontal:

    use File::Find::Rule; sub FFR () { 'File::Find::Rule' } my @files = FFR->file() ->or( FFR->name( '*.xml' ) ->name( '*.htm' ) ) ->in( "." ); foreach $fname (@files) { print $fname; }

    You probably don't need or for name since you can do ->name( '*.xml', '*.htm' ) and ->name( '*.{xml,htm}' ), but I included this as an example in case you needed to join other rules.

Re: Listing various types of files
by duff (Parson) on Nov 16, 2005 at 15:37 UTC

    Continuing with what Corion said (read the documentation), if you have a module installed on your system, you can access its documentation by using perldoc from your command line. For instance, to read the docs on File::Find::Rule, you would type:

    perldoc File::Find::Rule
    

    Also, to learn how to use perldoc you can type perldoc perldoc and to get a listing of all of the documentation for perl (not modules you may have on your system), type perldoc perl