You should use
$base = (split(/\./,$fname))[0];
and better solution is File::Basename | [reply] [d/l] |
| [reply] |
| [reply] |
use File::Basename;
my $filename = "../../path/to/test.txt";
my $basename = basename( $filename, '.txt', '.dat');
print $basename, $/;
__END__
test
If you omit the extension list arguments, or if $filename's extension is not in the list, basename() will return the basename with extension.
F::B also offers the functions dirname(), which extracts just the directory part, and fileparse(), which returns a list of ($basename, $dirname, $extension)
| [reply] [d/l] |
You're using single quotes inside single quotes; moreover, the dot is a special character in a regexp, you have to escape it: perl -e '
$fname = "test.txt"; # Double quotes!
$base = (split(/\./,$fname))[0]; # Escape dot!
print "$base\n";'
Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf
Don't fool yourself.
| [reply] [d/l] |
You need to escape the '.' in your split:
$base = ( split(/\./,$fname) )[0];
Adding -Mstrict and -w (always a good idea), the following works fine for me:
perl -Mstrict -w -e '
my $fname = "test.txt";
my $base = ( split(/\./,$fname) )[0];
print "$base\n";'
Update: And, as others have mentioned, you have single quotes inside single quotes, too :)
Hope that helps ..
| [reply] [d/l] [select] |
$fname = 'test.txt';
$base = (split/\./, $fname)[0];
print "$base\n";
| [reply] [d/l] |
Your dot is matching anything, you wanted to match a dot explicitly
$base = (split(/\./,$fname))[0];
UPDATE: Too slow, that's probably enough replies now :-)
---
my name's not Keith, and I'm not reasonable.
| [reply] [d/l] |
The delimiter for split is being parsed locally as a regular expression wildcard which is not very useful in this context. Either escaping it with backslash /\./ or enclosing in single quotes '.' will do the trick, although the latter will be more difficult in this case given that the whole program is also enclosed in single quotes.
| [reply] |