Home > Archive > PERL Beginners > January 2006 > problem using -f file operator
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 |
problem using -f file operator
|
|
|
| Hello,
I am trying to use the -f operator in the following manner:
# some $item manipulation here
.....
if ( -f $item )
{
unlink($item) || carp "Warning: couldn't remove file:
'$item'\n$!";
carp "Remove File: $item \n" if $debug;
}
elsif ( -d $item )
{
rmtree($item) || carp "Warning: couldn't remove dir:
'$item'\n$!";
carp "Remove Dir: $item \n" if $debug;
}
elsif ( -e $item )
{
unlink($item) || carp "Warning: couldn't remove item:
'$item'\n$!";
carp "Remove Item: $item \n" if $debug;
}
else
{
carp "Ignore Item: $item\n" if $debug;
}
the problem is that for a valid file, full path in $item perl doesn't
enter the: if (-f $item)
and I get the: "Ignore Item" message.
I run perl 5.005_03 on unix,
as I mentioned the file exists, using unix file command on it issues:
commands text
The directory is readable & writable to me.
moreover using: perl -e '$item = "/.....Eth_Stum.eu"; unlink($item);'
works!
can anybody see what's wrong here ?
many thanks,
Oded Haim-Langford
| |
| Paul Lalli 2006-01-10, 4:01 am |
| HL wrote:
> Hello,
>
> I am trying to use the -f operator in the following manner:
>
> # some $item manipulation here
That's rather vague. Why not simply tell us what $item contains?
> ....
> if ( -f $item )
> {
> unlink($item) || carp "Warning: couldn't remove file:
> '$item'\n$!";
> carp "Remove File: $item \n" if $debug;
> }
> elsif ( -d $item )
> {
> rmtree($item) || carp "Warning: couldn't remove dir:
> '$item'\n$!";
> carp "Remove Dir: $item \n" if $debug;
> }
> elsif ( -e $item )
> {
> unlink($item) || carp "Warning: couldn't remove item:
> '$item'\n$!";
> carp "Remove Item: $item \n" if $debug;
> }
> else
> {
> carp "Ignore Item: $item\n" if $debug;
> }
>
> the problem is that for a valid file, full path in $item perl doesn't
> enter the: if (-f $item)
> and I get the: "Ignore Item" message.
That's not the whole message, though. Your carp'ed message above
includes the value of $item. Please copy and paste the entire error
message. Does that value of $item correspond to what you think it
should be?
>
> I run perl 5.005_03 on unix,
> as I mentioned the file exists, using unix file command on it issues:
> commands text
> The directory is readable & writable to me.
> moreover using: perl -e '$item = "/.....Eth_Stum.eu"; unlink($item);'
> works!
You have a file in your root directory whose name begins with 6
periods? I find that just a bit unlikely.
> can anybody see what's wrong here ?
No, because you haven't shown us everything we should see.
Paul Lalli
| |
|
| Hi, sorry for the missing information it is my first post here,
I'll try to be more thorough this time.
First, here is the script it is rather short:
#!/usr/local/bin/perl -w
#=======================================
========================================
use Carp;
use Cwd;
use File::Path;
use Getopt::Long;
use strict;
#=======================================
========================================
my @tools; # contain the tools for filtering execution
my $model; # contain the name of the model (e.g. 051226)
my $file; # full path to filter definition file.
my $debug = 1;
Getopt::Long::config('default');
GetOptions('tool=s' => \@tools,
'model=s' => \$model,
'file=s' => \$file,
'h|help' => \&usage);
usage() unless( defined $file && defined $model);
my $rmPath = $ENV{'PROJ_HOME'};
open (IN, $file) || confess "Error: can't open $file for reading.
$!\n";
if ( scalar @tools == 0 )
{
@tools = ('MODELS', `find $ENV{PROJ_HOME}/VERIF_TOOLS -maxdepth 1
-type d`);
splice(@tools,1,1);
foreach ( @tools )
{
chomp;
}
}
while ( my $filter = <IN> )
{
chomp($filter);
next if ( $filter =~ m/^.*\s#/ || $filter !~ m/\w/ ); # line is
remarked or empty
foreach ( @tools ) # e.g. MODELS VERIF_TOOLS/Envir
{
next if ( $filter =~ m/^MODEL/ && m/^VERIF_TOOLS/ );
next if ( $filter =~ m/^VERIF_TOOLS/ && m/^MODEL/ );
my $item = "$rmPath/$filter";
my $tool = $_;
# should be different path for MODELS or VERIF_TOOLS
$item =~ s%($tool)%$1/Model_$model% if ( $tool eq 'MODELS' );
if ( $tool =~ m%VERIF_TOOLS/(\w+)% )
{
$tool = $1;
$item =~ s%($tool/)%$1${tool}_$model/%;
}
# carp "Filter: $item\n" if $debug;
if ( -f $item )
{
unlink($item) || carp "Warning: couldn't remove file:
'$item'\n$!";
carp "Remove File: $item \n" if $debug;
}
elsif ( -d $item )
{
rmtree($item) || carp "Warning: couldn't remove dir:
'$item'\n$!";
carp "Remove Dir: $item \n" if $debug;
}
elsif ( -e $item )
{
unlink($item) || carp "Warning: couldn't remove item:
'$item'\n$!";
carp "Remove Item: $item \n" if $debug;
}
else
{
carp "Ignore Item: $item\n" if $debug;
}
}
}
close (IN);
#=======================================
========================================
sub usage
{
print "\n$0 -tool <name> -model <name> -file <fullpath>
-model: Set name for the tool for which the filters are applied.
-tool : The directory to which the filters are relative.
-file : Set the filter file.
E.g.: pvcs_filter -tool MODELS -model 051227 -file
~/PVCS/filters/filter.txt
pvcs_filter -tool VERIF_TOOLS/Envir -model 051227 -file
filter.txt
\n";
exit 0;
}
Second, when the script is executed $item contains a full path to
existing file:
$item =
'/project/galileo101/orion2v/VERIF_TOOLS/Random/Random_051226/Gunit/Eth_Stum.eu'
The content of $item doesn't contain references to perl variables or
unique characters '@' '$' that may cause some string manipulation. only
regular valid unix full path to a file.
In the case discussed above the problem relates to a regular ascii
file.
Third, running the script remove files from the specified directory:
'/project/galileo101/orion2v/VERIF_TOOLS/Random/Random_051226/Gunit'
but not the: 'Eth_Stum.eu' file.
changing the files' name or the files' content (and leaving the same
name) didn't help get
it removed.
and last here is the files' contents:
#### CONFIG SELECTORS SECTION ####
# $cfg->{'GMII_Ipg'} = new Selector('Random', '12-20');
# $cfg->{'MII_Ipg'} = new Selector('Random', '24-40');
#### CONFIG GEN SELECTORS SECTION ####
# $cfg_gen->{'ConfType'} = new Selector ('0:1' , 'Default','Random' );
| |
|
| problem solved,
it appears I had white spaces at the end of the file name given to -f
operator.
|
|
|
|
|