attach a prefix to a filename
Anonymous Monk
created: 2006-01-03 04:32:15
I want help on how to attach a prefix to a filename by mainting its absoulte path. my code:
my $tempvalue = "c:/reports/check/test.txt";
my @split = split( '/',$tempValue );
	    $split[$#split] = $/"design.".$1/e;
            print "\t",join( '/',@split ),"\;  \\\n" ;
Please suggest me a better way of doing this. so the result should look like c:/reports/check/design.test.txt Thanks in advance.
Re: attach a prefix to a filename
created: 2006-01-03 04:45:07
Use [cpan://File::Basename], it will work well under almost any OS. The docs are quite clear ;)

Update: ok, an example:

#!/usr/bin/perl
use strict;
use warnings;
use File::Basename qw( fileparse );
 
my $tempvalue = 'c:/reports/check/test.txt';
my ($name, $path) = fileparse($tempvalue);
my $designvalue = $path . "design." . $name;
print "'$tempvalue' => '$designvalue'\n";
 
__END__
 
'c:/reports/check/test.txt' => 'c:/reports/check/design.test.txt'

Flavio
perl -ple'$_=reverse' << Don't fool yourself.

Re: attach a prefix to a filename
created: 2006-01-03 04:46:53

You can use the module [CPAN://File::Basename] to accomplish your requirement

use File::Basename;

my $tempvalue = "c:/reports/check/test.txt";
$file = basename($tempvalue);
$path = dirname($tempvalue);
$fullpath = $path.'/'.design.$file;

print "$fullpath\n";

Prasad

Re: attach a prefix to a filename
created: 2006-01-03 04:48:30

I would suggest using [mod://File::Basename]:

use File::Basename;

my $path = 'c:/reports/check/test.txt';
fileparse_set_fstype('MSWin32'); # or is it 'MSDOS'?
my ($name, $base, $suffix) = fileparse($path, '.txt');
$name = 'design.' . $name;

print join '', $base, $name, $suffix, "\n";
Re: attach a prefix to a filename
created: 2006-01-03 06:05:52
But you don't *have* to use [mod://File::Basename] if you didn't want to you :o)

If you know you're not going to be changing platforms or needing the flexibility of a directory path splitter that handles different directory seperator characters (i.e. \ on Windows and / on Unix etc) and just want something fast and simple you could use something like this instead:

my $tempvalue = "c:/reports/check/test.txt";

my @split = split( '/', $tempvalue );

my $file = pop @split;

print "\t", join( '/', @split ), "/design.$file\n";
Re^2: attach a prefix to a filename
created: 2006-01-03 08:18:51
[cpan://File::Basename] comes 'for free' with perl (with the lower p), at least in recent installations. This gives you the advantage to write clean code, not to reinvent the wheel and avoid common pitfalls (which make the [cpan://File::Basename] solution simpler IMHO). And lets you not worry about portability, which may not be an issue today, but who knows?

OTOH, you're definitively right on the fast side:

#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw( cmpthese );
 
my $tempvalue = 'c:/reports/check/test.txt';
cmpthese( -5, {
   file_basename => sub { file_basename($tempvalue); },
   fast_and_simple => sub { fast_and_simple($tempvalue); },
});
 
 
sub file_basename {
   my $tempvalue = shift;
   require File::Basename; # Hits performance only once
   my ($name, $path) = File::Basename::fileparse($tempvalue);
   return $path . "design." . $name;
}
 
sub fast_and_simple {
   my $tempvalue = shift;
   my @split = split( '/', $tempvalue );
   my $file = pop @split;
   return join '/', @split, "design.$file";
}
 
__END__

                    Rate   file_basename fast_and_simple
file_basename    74479/s              --            -58%
fast_and_simple 175981/s            136%              --
I dared to make the necessary modifications to your code in order to put it into the benchmark :)

Flavio
perl -ple'$_=reverse' << Don't fool yourself.

Re: attach a prefix to a filename
created: 2006-01-03 10:31:26
could be this?
perl -e '$\="\n";$tempvalue = "c:/reports/check/test.txt"; print $tempvalue if $tempvalue =~ s/\/([^\/]+)$/\/design.$1/g'

perlmonks.org content © perlmonks.org and Anonymous Monk, Fang, frodo72, prasadbabu, serf, smokemachine

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03