Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Stripping parts of paths from directory names

by r.joseph (Hermit)
on Jan 06, 2001 at 13:19 UTC ( [id://50234]=perlquestion: print w/replies, xml ) Need Help??

r.joseph has asked for the wisdom of the Perl Monks concerning the following question:

I am just full of questions lately!! Here is another one!

From my directory search, I have entries such as this one:

/home/sites/site18/users/rjoseph/web/images/image1.jpg

The users web directory starts from the web sub-dir, and so I want to strip off everything above that, ie:

/home/sites/site18/users/rjoseph/

But, I want to have the above path stored in a varible, so I can change it on the fly to adapt to different directores. I tried doing this:
$file_path = '/home/sites/.../image1.jpg'; $path = '/home/sites/.../'; eval "$file_path =~ tr/$path//";
But that didn't work. How would I do this correctly? Thanks you so much!

R.Joseph

Replies are listed 'Best First'.
Re: Stripping parts of paths from directory names
by davorg (Chancellor) on Jan 06, 2001 at 13:48 UTC

    I'm not entirely clear what you mean, but does something like thid do the trick?

    my $file_path = 'home/sites/site18/users/rjoseph/web/images/image1.jpg'; my $path = '/home/sites/site18/users/rjoseph/'; $file_path =~ s/^$path//;
    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: Stripping parts of paths from directory names
by adamsj (Hermit) on Jan 06, 2001 at 13:53 UTC
    Well, you'd get something like what you want by doing this:
    $file_path =~ s#^$path##;
    instead of this:
    eval "$file_path =~ tr/$path//";
    (heck, you could even do this, if you were in a silly mood:
    ($file_path) = split /$path/, $file_path, 1;
    but I don't know that I'd recommend it)

    but you might want to look in CPAN for better solutions to your underlying problem--they're out there.

    By the way, you'll note that I used # instead of / for my separator--I almost always do that, especially when I've got lots of slashes in my pattern, as it makes it easier to see what's going on.

    Update: I fixed some typos and stuff I didn't like.

      Yep theres another solution out there and its File::Spec, part of a fairly standard distribution.

      The line wanted is $file_path = File::Spec->abs2rel( $file_path, $path );

Re: Stripping parts of paths from directory names
by Coyote (Deacon) on Jan 06, 2001 at 14:29 UTC
    I don't think the tr/// operator does what you think it does in this case. tr/// operates on characters rather than strings. Here's my approach to the problem if I understand it correctly:
    use strict; use warnings; my $file_path='/home/sites/site18/users/rjoseph/web/images/image1.jpg' +; my $break_point = '/web'; my @foo = split($break_point, $file_path); my $path = $break_point . pop(@foo); print $path;
    This prints: /web/images/image1.jpg
      Or in one line ... map {$path="/web$_"} split('/web','/home/sites/site18/users/rjoseph/web/images/image1.jpg');print $path Yields: /web/images/image1.jpg ---- Coyote (aka: Rich Anderson)
Re: Stripping parts of paths from directory names
by ChOas (Curate) on Jan 06, 2001 at 14:56 UTC
    This would be efficient (could use rindex if there is just a
    short path after the web part for even more efficiency but then you'd
    run into trouble with users having another 'web' subdir,so I'd
    recommend this):
    #!/usr/bin/perl -w use strict; my $FileName='/home/sites/site18/users/rjoseph/web/images/image1.jpg'; my $Stripped=substr $FileName,0,index($FileName,'web'); print "Stripped to: $Stripped\n";

    GreetZ!,
      ChOas

    print "profeth still\n" if /bird|devil/;
Re: Stripping parts of paths from directory names
by mr.nick (Chaplain) on Jan 06, 2001 at 19:47 UTC
    I'll just add my $0.02 to this by giving an example:

    my $file='/home/sites/site18/users/rjoseph/web/images/image1.jpg'; 
     
    if ($file=~/^(.+)(\/web.+)$/) { 
      my $part1=$1; 
      my $part2=$2; 
     
      print "$part1\n$part2\n"; 
    } 
    
    
    Produces the output of
    /home/sites/site18/users/rjoseph
    /web/images/image1.jpg
    
    
    Now, the fun part begins when you have an image named "weblink.jpg" or something similiar. But I'll leave that as a exercise to the reader.
Re: Stripping parts of paths from directory names
by a (Friar) on Jan 07, 2001 at 10:49 UTC
    I may be missing the point altogether, but you might not want to have the path listed at all. Instead, put in a cgi script that takes the web dir path and returns the image from the real path. That is, the URL
    http://mysite.com/cgi-bin/get-image/image1.jpg
    would execute the cgi-bin script "get-image" w/ a PATH_INFO (?) env var ($ENV{PATH_INFO in your script) of "/image1.jpg". At that point, get-image can do any sort of security/un-tainting (e.g.
    my $path-info = $ENV{PATH_INFO} my $path = "/home/usrs/rjoseph/images"; $path-info =~ s/^[^/]// # strip of preceeding noise $path-info =~ s#[^/\w\.]//g # strip out any non-word dot chars
    and then say:
    my $file_path = "$path/$path-info"; if ( -s $file_path ) { open(FILE, $file_path) or die ...
    So if you're writing back URLs to your web users, you can do:
    foreach my $file ( @image_list ) { $file =~ s#$path##; # remove the physical path part my $file_name = $file; $file_name = s/\.\w+$//; # strip off extension print "<a href=\"http://mysite.com/cgi-bin/get-image/$file">Image: $ +file_name</a>"; }
    where @image_list has your real file names in it. You may want to take a look at CGI.pm file upload freaking me out

    a

(jeffa) Re: Stripping parts of paths from directory names
by jeffa (Bishop) on Jan 07, 2001 at 11:48 UTC
    I am going to take a different aproach than every one else, although a did see how %ENV can be very helpfully.

    Warning: Apache Head Talking
    Traditionally, web servers allow users to host web 'sites' by giving them a directory that is publicly accessible, usually named 'public_html' - looks like yours is called 'web'. I am going to assume the rest (fingers crossed).

    What I don't understand is why you want to use the tr/// or even the s/// operators (or substr or split . . .) to derive the two paths. There is nothing wrong with simply 'hard coding' this kind of data - it's not difficult to change, and you don't need to waste CPU time (albeit extremely minimal) doing it.

    For example, you know that the disk path is:

    $disk_path = '/home/sites/site18/users/rjoseph/web/';
    and the URL path is probably:
    $url_path = 'http://www.your_box.com/~rjoseph/';
    Notice that 'web' is not included, and a tilde is added to the user name.

    If you wanted to make this portable across different users of the same box, grab the user's name from %ENV:

    $disk_path = '/home/sites/site18/users/' . $ENV{USER} . '/web/'; $url_path = 'http://www.your_box.com/~' . $ENV{USER} . '/';
    You can always get to the images by using:
    $img_path = $url_path . 'images/';

    And by no means am I saying that you should disregard what everybody before me posted. On the contrary, they are showing you many different ways of using regular expressions, string operations, and array operations to 'munge' data. Practice them. :)

    Jeff

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://50234]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-26 05:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found