Random said:
Hi i am trying to get $tmpdir to contain the "directory" instead of a
fully pathed file name ... i.e. truncate the file name.
For that task, I'd recommend using the dirname() function from the
File::Basename module. It's standard, so you should aready have it.
Read about it by typing "perldoc File::Basename" at the command prompt.
Here's a quick code sample that does exactly what you want:
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
print "Type a filename: ";
my $file = <STDIN>;
# This next line is the line that does what you want:
my $tmpdir = dirname($file);
print "The directory is: $tmpdir\n";
__END__
By using the File::Basename module, you remove any need for regular
expressions and any dependence on code that hasn't already been tested
by many people in the Perl community. The code that does what you want
in the sample program above is only one line long, so why write our own
(longer) code that may initially suffer from several bugs?
I am trying the below code ... but $1 is always empty ... help please
my $tmpdir = $file;
if ( $tmpdir =~/([.]*)[\\]{1}/)
{
print("MATCH\n");
$tmpdir = $1;
print ("[--->]".$1."<----]\n");
}
else
{
print("NO MATCH\n");
$tmpdir = $tmpdir;
}
Uh... what is the purpose of this line, exactly?:
Do you know what this line even does? If so, why did you put it in?
To be honest, it seems like you are fairly new to Perl programming.
I would recommend purchasing (or checking-out from your local library)
a book named "Learning Perl" (published by O'Reilly). It makes the
assumption that the reader does not know any Perl programming, and
teaches the beginner Perl programmer many programming concepts that all
decent Perl programmers are expected to know (like regular expressions,
which you seem to want to use but don't know how to use correctly).
Personally, I don't think any programmer has become a "real Perl
programmer" until he/she is familiar with at least 90% of that book.
And I'm guessing that you never read that book, otherwise you wouldn't
have made some of the regular expression mistakes that you did.
Don't feel bad if you've never read the "Learning Perl" book. Every
one of us, at some point in our lives, had not read that book. Pick it
up, read it, work through the examples, and you'll find that you can
read and write Perl code much easier and better than ever before.
I hope this helps.
-- Jean-Luc