For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > December 2007 > testing for a file type









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author testing for a file type
Goldtech

2007-12-18, 7:00 pm

Hi,

If I have:

....
foreach (@ARGV) {
print "do something only to .mdb files";
}

I could use File::Basename's fileparse and test for the file extension
and put a big if statement around or in the foreach loop. So if a user
puts a non .mdb file argument on the cmd line it won't process and
prints a usage.

But I suspect in perl theres a more compact way of doing that?

Thanks.

Ankur Gupta

2007-12-18, 7:00 pm

On Dec 18, 2007 10:08 PM, goldtech <goldtech@worldpost.com> wrote:

> Hi,
>
> If I have:
>
> ...
> foreach (@ARGV) {
> print "do something only to .mdb files";
> }
>
> I could use File::Basename's fileparse and test for the file extension
> and put a big if statement around or in the foreach loop. So if a user
> puts a non .mdb file argument on the cmd line it won't process and
> prints a usage.
>
> But I suspect in perl theres a more compact way of doing that?


I don't think you need to use File::Basename.

You can simply do this.

foreach my $file (@ARGV) {
if ( $file !~ /\.mdb$/ ) {
print "$file is not a mdb file. Ignoring...\n";
next;
}

# code to pocess .mdb file
...
...
}

--
Ankur
Rob Dixon

2007-12-18, 7:00 pm

goldtech wrote:
> Hi,
>
> If I have:
>
> ...
> foreach (@ARGV) {
> print "do something only to .mdb files";
> }
>
> I could use File::Basename's fileparse and test for the file extension
> and put a big if statement around or in the foreach loop. So if a user
> puts a non .mdb file argument on the cmd line it won't process and
> prints a usage.
>
> But I suspect in perl theres a more compact way of doing that?


use strict;
use warnings;

if (grep { not /\.mdb\z/ } @ARGV) {
print "All parameters must be MDB files\n";
exit;
}

foreach (@ARGV) {
print "do something only to .mdb files";
}

HTH,

Rob
Chas. Owens

2007-12-19, 10:03 pm

On Dec 18, 2007 4:49 PM, Rob Dixon <rob.dixon@350.com> wrote:
snip
> if (grep { not /\.mdb\z/ } @ARGV) {
> print "All parameters must be MDB files\n";
> exit;
> }

snip

Or in Perl 5.10, coming to stores near you soon*, you can use the
smart match operator:

@ARGV ~~ /\.mdb\z/
or die "All parameters must be MDB files"

See smart matching in perldoc perlsyn or
http://search.cpan.org/dist/perl/po...ching_in_detail

* I just checked and in fact it was released yesterday.
Jenda Krynicky

2007-12-19, 10:03 pm

From: "Chas. Owens" <chas.owens@gmail.com>
> On Dec 18, 2007 4:49 PM, Rob Dixon <rob.dixon@350.com> wrote:
> snip
> snip
>
> Or in Perl 5.10, coming to stores near you soon*, you can use the
> smart match operator:
>
> @ARGV ~~ /\.mdb\z/
> or die "All parameters must be MDB files"
>
> See smart matching in perldoc perlsyn or
> http://search.cpan.org/dist/perl/po...ching_in_detail
>
> * I just checked and in fact it was released yesterday.


I did not install it yet so I can't check but I think you have it
wrong. According to the docs you point to

@ARGV ~~ /\.mdb\z/

is equivalent to

grep /\.mdb\z/, @ARGV

which is true whenever at least one item in the array matches the
regexp. So it would be

@ARGV ~~ /\.mdb\z/
or die "At least one of the parameters must be an MDB file";

Probably not what Rob (or whoever posted the original post) intended.

grep {not <condition>} @ARRAY

is not equivalent to

not grep {<condition>} @ARRAY

not even in boolean context.


@ARGV ~~ /\.mdb\z/
is
any(@ARGV) =~ /\.mdb\z/
not
all(@ARGV) =~ /\.mdb\z/

I think there should have been a !~~ operator as well.

Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

Chas. Owens

2007-12-20, 4:02 am

On Dec 19, 2007 7:01 PM, Jenda Krynicky <Jenda@krynicky.cz> wrote:
snip
> I did not install it yet so I can't check but I think you have it
> wrong. According to the docs you point to
>
> @ARGV ~~ /\.mdb\z/
>
> is equivalent to
>
> grep /\.mdb\z/, @ARGV
>
> which is true whenever at least one item in the array matches the
> regexp.

snip

Yeah, I jumped the gun.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com