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 = "../htdocs/_carts/sql/data/orders.tsv";
my @words = split (/\//, $file_path);
(my $file_name = $words[-1]) =~ s/\.[^.]+$//;    # $filename = orders

-----

my $file_path = "../htdocs/_carts/sql/data/catalog.tsv";
(my $file_name = substr($file_path, rindex($file_path,"/") + 1, 
length($file_path)-rindex($file_path,"/") - 1)) =~ s/\.[^.]+$//;

-----

use File::Spec;
 my $file_path = "../htdocs/_carts/sql/data/categories.tsv";
 my ($volume, $directories, $file_name) = File::Spec->splitpath($file_path);
 $file_name =~ s/\.[^.]+$//;

-----

use File::Basename;
 my $file_path = "../htdocs/_carts/sql/data/customers.tsv";
 my $filename = fileparse($file_path, qr/\.[^.]*/);    # $filename = customers


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to