File::Basename problem

Hello!=0AI'm trying to use File::Basename, but it is not behaving well:=0A=
=0Atessio [at] pacman:~/tmp/ASAP$ ls=0A=0Abook.div book.tex erotic.div erotic=
..tex erotyc.tex problem source.a source.c source.o=0A=0A=0Atessio [at] pacm=
an:~/tmp/ASAP$ cat problem=0A=0Ause 5.010;=0A=0Ause strict;=0A=0Ause warnin=
gs;=0A=0Ause File::Basename;=0A=0A=0A=0Amy %sufixTable =3D (=0A=0A=09'.div'=
=3D> '.tex',=0A=0A=09'.o' =3D> '.c',=0A=0A=09'.a' =3D> '.c',=0A=0A);=0A=0A=
=0A=0Aopendir my $DIR, '.' or die "Unable to open dir: $!\n";=0A=0Amy [at] list=
=3D readdir $DIR or die "Unable to read dir: $!\n";=0A=0A=0A=0Aforeach my =
$item ( [at] list ) {=0A=0A=09next if $item eq '.';=0A=0A=09next if $item eq '.=
..';=0A=0A=0A=0A=09# BUG! fileparse is doing it wrong!=0A=0A=09my ( $file, $=
dir, $sufix ) =3D fileparse( $item, %sufixTable );=0A=0A=09=0A=0A=09print "=
$file -> $sufix\n";=0A=0A}=0A=0A=0Atessio [at] pacman:~/tmp/ASAP$ perl problem=
=0A=0Aerot -> ic.tex=0A (ERRO)=0Abook -> .div=0A=0Aerot -> yc.tex=0A (=
ERRO)=0Aproblem -> =0A=0Abook -> .tex=0A=0Asource -> .c=0A=0Asource -> .a=
=0A=0Asource -> .o=0A=0Aerot -> ic.div=0A (ERRO)=0Atessio [at] pacman:~/tmp/AS=
AP$=0A=0A---=0A=0ANote that it is not working with erotic and erotyc file n=
ames.. Why?=0A=0A=0A=0A

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
precheca123 [ Fr, 04 März 2011 17:14 ] [ ID #2056193 ]

Re: File::Basename problem

On 11-03-04 11:14 AM, Téssio Fechine wrote:
> my ( $file, $dir, $sufix ) = fileparse( $item, %sufixTable );

I don't think fileparse takes a hash as the final arguments. It would
be converted to an array and both the keys and the values used.

Also, from `perldoc File::basename`:

If [at] suffixes are given each element is a pattern (either a string or a
"qr//") matched against the end of the $filename. The matching portion
is removed and becomes the $suffix.

That mean all the periods in the hash will match any character.

Try:

my [at] sufixTable = ( '\\.div', '\\.tex', '\\.o', '\\.c', '\\.a', );


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shawn H Corey [ Fr, 04 März 2011 17:22 ] [ ID #2056194 ]

Re: File::Basename problem

Can't I use the hash in list context with something like itsNotRegex(%sufix=
Table) ?=0AI use this hash in a program that find related suffix to know if=
a file has a source ('.o' =3D> '.c').. and keeping the list of suffixes in=
another is variable is a kind of rework. =0AThanks for the help!=0A=0A> D=
e: Shawn H Corey <shawnhcorey [at] gmail.com>=0A> Assunto: Re: File::Basename pr=
oblem=0A> Para: beginners [at] perl.org=0A> Data: Sexta-feira, 4 de Mar=E7o de 2=
011, 13:22=0A> On 11-03-04 11:14 AM, T=E9ssio Fechine=0A> wrote:=0A> > =A0=
=A0=A0 my ( $file, $dir, $sufix ) =3D=0A> fileparse( $item, %sufixTable );=
=0A> =0A> I don't think fileparse takes a hash as the final=0A> arguments.=
=A0 It would be converted to an array and both=0A> the keys and the values =
used.=0A> =0A> Also, from `perldoc File::basename`:=0A> =0A> If [at] suffixes a=
re given each element is a pattern (either a=0A> string or a "qr//") matche=
d against the end of the=0A> $filename.=A0 The matching portion is removed =
and becomes=0A> the $suffix.=0A> =0A> That mean all the periods in the hash=
will match any=0A> character.=0A> =0A> Try:=0A> =0A> my [at] sufixTable =3D ( =
'\\.div', '\\.tex', '\\.o', '\\.c',=0A> '\\.a', );=0A> =0A> =0A> -- Just my=
0.00000002 million dollars worth,=0A> =A0 Shawn=0A> =0A> Confusion is the =
first step of understanding.=0A> =0A> Programming is as much about organiza=
tion and=0A> communication=0A> as it is about coding.=0A> =0A> The secret t=
o great software:=A0 Fail early &=0A> often.=0A> =0A> Eliminate software pi=
racy:=A0 use only FLOSS.=0A> =0A> -- To unsubscribe, e-mail: beginners-unsu=
bscribe [at] perl.org=0A> For additional commands, e-mail: beginners-help [at] perl.o=
rg=0A> http://learn.perl.org/=0A> =0A> =0A> =0A=0A=0A

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
precheca123 [ Fr, 04 März 2011 17:48 ] [ ID #2056196 ]

Re: File::Basename problem

On 04/03/2011 16:48, Téssio Fechine wrote:
>
>> De: Shawn H Corey<shawnhcorey [at] gmail.com> wrote:
>
>> my ( $file, $dir, $sufix ) = fileparse( $item, %sufixTable );
>>
>> I don't think fileparse takes a hash as the final
>> arguments. It would be converted to an array and both
>> the keys and the values used.
>>
>> Also, from `perldoc File::basename`:
>>
>> If [at] suffixes are given each element is a pattern (either a
>> string or a "qr//") matched against the end of the
>> $filename. The matching portion is removed and becomes
>> the $suffix.
>>
>> That mean all the periods in the hash will match any
>> character.
>>
>> Try:
>>
>> my [at] sufixTable = ( '\\.div', '\\.tex', '\\.o', '\\.c',
>> '\\.a', );
>
> Can't I use the hash in list context with something like
> itsNotRegex(%sufixTable)? I use this hash in a program that find
> related suffix to know if a file has a source ('.o' => '.c').. and
> keeping the list of suffixes in another is variable is a kind of
> rework.

I suggest you write this

my ($file, $dir, $suffix) = fileparse($item, map quotemeta, %sufixTable);

which will correctly escape the dots in the regex.

HTH,

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Rob Dixon [ Fr, 04 März 2011 17:58 ] [ ID #2056198 ]
Perl » gmane.comp.lang.perl.beginners » File::Basename problem

Vorheriges Thema: yaml file conversion
Nächstes Thema: Class::Struct comparison & MOP