Pulling file name from path
Pulling file name from path
am 09.06.2011 16:46:31 von sono-io
I've written a 'create_sql_table' routine which starts off by =
pulling
the file name (sans suffix) from a path. I've posted four code snippets =
below.
All work fine, but I'm curious to know if there is a better (or more =
elegant) way
to do it?
The second example is one line shorter than the first and =
doesn't populate
an array to get the job done. Would that make it more efficient?
Considering the last two examples, I like File::Basename more. =
However,
is there any overhead using a module like this as opposed to the first =
two examples?
Or is there a better way that I haven't run across yet? It =
needs to be
multi-platform.
Thanks,
Marc
my $file_path =3D "../htdocs/_carts/sql/data/orders.tsv";
my @words =3D split (/\//, $file_path);
(my $file_name =3D $words[-1]) =3D~ s/\.[^.]+$//; # $filename =3D =
orders
-----
my $file_path =3D "../htdocs/_carts/sql/data/catalog.tsv";
(my $file_name =3D substr($file_path, rindex($file_path,"/") + 1, =
length($file_path)-rindex($file_path,"/") - 1)) =3D~ s/\.[^.]+$//;
-----
use File::Spec;
my $file_path =3D "../htdocs/_carts/sql/data/categories.tsv";
my ($volume, $directories, $file_name) =3D =
File::Spec->splitpath($file_path);
$file_name =3D~ s/\.[^.]+$//;
-----
use File::Basename;
my $file_path =3D "../htdocs/_carts/sql/data/customers.tsv";
my $filename =3D fileparse($file_path, qr/\.[^.]*/); # $filename =3D =
customers
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Pulling file name from path
am 09.06.2011 16:52:10 von Shawn H Corey
On 11-06-09 10:46 AM, sono-io@fannullone.us wrote:
> Or is there a better way that I haven't run across yet? It needs to be
> multi-platform.
Use File::Spec and File::Basename together. They offer platform
independence.
use File::Spec;
use File::Basename;
my $file_path = "../htdocs/_carts/sql/data/categories.tsv";
my ($volume, $directories, $file_name) =
File::Spec->splitpath($file_path);
my $filename = fileparse($file_name, qr/\.[^.]*/); # $filename =
customers
--
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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Pulling file name from path
am 09.06.2011 17:42:26 von sono-io
On Jun 9, 2011, at 7:52 AM, Shawn H Corey wrote:
> Use File::Spec and File::Basename together.
=46rom what I read, I thought they did the same thing, but from =
your post it appears that there may be differences. What's the =
advantage in using both instead of one or the other? Are they not =
platform independent on their own? I've only been able to run tests on =
a Mac.
Marc=
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Pulling file name from path
am 09.06.2011 17:50:35 von Shawn H Corey
On 11-06-09 11:42 AM, sono-io@fannullone.us wrote:
> On Jun 9, 2011, at 7:52 AM, Shawn H Corey wrote:
>
>> Use File::Spec and File::Basename together.
>
> From what I read, I thought they did the same thing, but from your post it appears that there may be differences. What's the advantage in using both instead of one or the other? Are they not platform independent on their own? I've only been able to run tests on a Mac.
>
> Marc
File::Spec::splitpath() does not parse the suffixes.
File::Basename::parsefile() does not parse the volume.
What you use depends on your application's needs.
--
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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/