Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: attach a prefix to a filename

by serf (Chaplain)
on Jan 03, 2006 at 11:05 UTC ( #520558=note: print w/replies, xml ) Need Help??

in reply to attach a prefix to a filename

But you don't *have* to use 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";

Replies are listed 'Best First'.
Re^2: attach a prefix to a filename
by polettix (Vicar) on Jan 03, 2006 at 13:18 UTC
    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 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 :)

    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://520558]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2017-06-28 09:47 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (631 votes). Check out past polls.