SPUG: regular expression difficulty, plus compiled perl

Christopher Maujean ChrisM at courtlink.com
Tue Mar 28 14:19:15 CST 2000


I know File::Basename was mentioned before, but you can use it to get all of
the information you might ever need:

#!perl

use strict; # ALWAYS ALWAYS ALWAYS!

# imports the dirname, basename, fileparse_set_fstype, and fileparse subs
use File::Basename; 

# the full path we want to parse
my $fullpath = 'c:\windows\system\deletebillg.exe';

my ($name,$path,$suffix) = fileparse($fullpath,'.*');

#	$name = 'deletebillg'
#	$path = 'c:\windows\system\'
# 	$suffix = '.exe'

__END__

=pod

from ActivePerl Help on File::Basename:

fileparse 

The fileparse() routine divides a file specification into three parts: a
leading path, a file name, and a suffix. The path contains everything up to
and including the last directory separator in the input file specification.
The remainder of the input file specification is then divided into name and
suffix based on the optional patterns you specify in @suffixlist. Each
element of this list is interpreted as a regular expression, and is matched
against the end of name. If this succeeds, the matching portion of name is
removed and prepended to suffix. By proper use of @suffixlist, you can
remove file types or versions for examination. 

You are guaranteed that if you concatenate path, name, and suffix together
in that order, the result will denote the same file as the input file
specification. 

=cut


-----Original Message-----
From: Ryan Erwin [mailto:ryan at dbedge.com]
Sent: Tuesday, March 28, 2000 11:20 AM
To: Todd Wells; spug-list at pm.org
Subject: Re: SPUG: regular expression difficulty, plus compiled perl


OK, OK, OK...
I said that I was experiencing extreme sleep
deprivation...
When I read your post, I thought that you wanted
to get just the file name, not the directory it
was stored in...
#!/usr/bin/perl -w

my $test = 'c:\dir1\subdir1\subdir2\my.exe';
print "\$test == $test\n\n";
$test =~ s#.*\\##;
print "\$test == $test\n\n";

which this will do quite nicely...
if you just wanted the path, try using $1 and
capturing parens...
#!/usr/bin/perl -w

my $test = 'c:\dir1\subdir1\subdir2\my.exe';
print "\$test == $test\n\n";
$test =~ m#(.*\\)#;
print "\$1 == $1\n\n";

If you wanted to get just the path

Enjoy 8-}

Ryan Erwin
ESPUG Emperor

BTW: we are just 1 day away from the next ESPUG
meeting where we will be discussing more Object
Oriented Perl!  Check out espug-site
http://espug.pm.org for the details...
----- Original Message -----
From: Todd Wells <toddw at wrq.com>
To: 'Skahan, Vince' <Vince.Skahan at pss.boeing.com>;
<spug-list at pm.org>
Sent: Tuesday, March 28, 2000 9:24 AM
Subject: RE: SPUG: regular expression difficulty,
plus compiled perl


> Yes, dirname() is what I was looking for, just
overlooked it in the cookbook
> -- thank you!
>
> Thanks to the academy and everyone who
contributed...oh wait, wrong speech.
>
> But I'm still interested to know how I would
parse the same info out of a
> variable containing
"c:\dir1\subdir1\subdir2\my.exe", basically how to
trim
> the filename off the end.
>
> About the other solutions...
>
> SOLUTION 1:
> =================================
> $test1 =~ s#.*\\##;    # replace everything
>                        # before the last \
>                        # with nothing...
> =================================
> This is the exact opposite of what I wanted to
do, if you test the code it
> actually ends up with $test1 = "";
>
> SOLUTION 2:
> =================================
> split(/\\/,$0);
> pop @_;
> $test1 = join "\\", at _;
> =================================
> This assumes my path is only one directory deep,
not going to work.
>
>
>
> -----Original Message-----
> From: Skahan, Vince
[mailto:Vince.Skahan at pss.boeing.com]
> Sent: Tuesday, March 28, 2000 7:19 AM
> To: 'spug-list at pm.org'; 'Todd Wells'
> Subject: RE: SPUG: regular expression
difficulty, plus compiled perl
>
>
>
>
> I'd use dirname() rather than roll my own. Any
reason why doing it the
> unix way doesn't suffice ?
>
> The following worked for me on my NT system
using ActiveState
> perl build 522...
>
> #!perl
> #
> # see page 328 of the PerlCookbook
> #
> use File::Basename;
> $dir=dirname($0);
> print "I'm called $0\n";
> print "I live in $dir\n";
>
> When I save the script as c:\tmp\foo.pl and run
it, it reports back
> output that looks like:
>
> I'm called C:\TMP\foo.pl
> I live in C:\TMP
>
> --
> -------- Vince.Skahan at boeing.com ------
http://bcstec.ca.boeing.com/~vds/
> -------------
>                  Boeing Shared Services Group -
Technical Services
>                     outside Boeing -
http://www.halcyon.com/vince
>
>
> > ----------
> > From: Todd Wells[SMTP:toddw at wrq.com]
> > Sent: Monday, March 27, 2000 8:20 PM
> > To: 'spug-list at pm.org'
> > Subject: SPUG: regular expression difficulty,
plus compiled perl
> >
> > I'm using ActiveState Perl on NT4 and trying
to set a variable to be equal
> > to the directory name that the script was ran
in.
> >
> > So, I look at $0 which contains the name of
the script I ran... in this
> case
> > "D:\PERL\COUNTP~1.PL"
> >
> > But when I try to strip the filename off the
end, I can't seem to do it.
> >
> > print "First: $0\n";
> > ($test1 = $0)=~ m#.*\\#;
> > #$test1 =~ s/(.*\\)/$1/; # I tried this too,
and it didn't work
> > either.
> > print "Second: $test1\n";
> >
> > The output:
> > First: D:\PERL\COUNTP~1.PL
> > Second: D:\PERL\COUNTP~1.PL
> >
> > What's going on here?  Is the .* just being
_really_ greedy?  I'm
> expecting
> > $test1 to be "D:\PERL\"
> > I know this is silly, but I can't figure out
the problem here.
> >
> > BTW, someone at the last SPUG meeting
mentioned they were playing around
> > with compiled Perl using ActiveState.  I can't
seem to find any specifics
> on
> > how to do this.  Perlfaq3 mentions it, but
gives no specifics on how to
> > actually do it, nor can I find any at Malcolm
Beattie's site.
> >
> > - Todd
> >
>
>  - - - - - - - - - - - - - - - - - - - - - - - -
 - - - - - - - - - - - - -
> >      POST TO: spug-list at pm.org       PROBLEMS:
owner-spug-list at pm.org
> >  Seattle Perl Users Group (SPUG) Home Page:
http://www.halcyon.com/spug/
> >  SUBSCRIBE/UNSUBSCRIBE: Replace "action" below
by subscribe or unsubscribe
> >            Email to majordomo at pm.org: "action"
spug-list your_address
> >
> >
>
>  - - - - - - - - - - - - - - - - - - - - - - - -
 - - - - - - - - - - - - -
>      POST TO: spug-list at pm.org       PROBLEMS:
owner-spug-list at pm.org
>  Seattle Perl Users Group (SPUG) Home Page:
http://www.halcyon.com/spug/
>  SUBSCRIBE/UNSUBSCRIBE: Replace "action" below
by subscribe or unsubscribe
>            Email to majordomo at pm.org: "action"
spug-list your_address
>
>
>  - - - - - - - - - - - - - - - - - - - - - - - -
 - - - - - - - - - - - - -
>      POST TO: spug-list at pm.org       PROBLEMS:
owner-spug-list at pm.org
>  Seattle Perl Users Group (SPUG) Home Page:
http://www.halcyon.com/spug/
>  SUBSCRIBE/UNSUBSCRIBE: Replace "action" below
by subscribe or unsubscribe
>            Email to majordomo at pm.org: "action"
spug-list your_address
>
>
>
>


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace "action" below by subscribe or unsubscribe
           Email to majordomo at pm.org: "action" spug-list your_address


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace "action" below by subscribe or unsubscribe
           Email to majordomo at pm.org: "action" spug-list your_address





More information about the spug-list mailing list