Belgin Fish
Belgin Fish

Reputation: 19837

Remove Part of String Perl

I have this in perl

  return "$file->{srv_cgi_url}/dl.cgi/$hash/$fname";

where

$file->{srv_cgi_url}

returns

http://s1.site.com/cgi-bin/

how can I remove the trailing /cgi-bin/ from the string? :)

Thanks!

Upvotes: 2

Views: 780

Answers (2)

Ashley
Ashley

Reputation: 4335

While substitution can work, it’s fragile and difficult to extend and maintain. I strenuously recommend learng to use URI, URI::QueryParam, and Path::Class instead (the last is not used in this example but important and related).

use warnings;
use strict;
use URI;

my $file;
$file->{srv_cgi_url} = "http://s1.site.com/cgi-bin/";

my $srv_cgi_uri = URI->new( $file->{srv_cgi_url} );

my $hash = "some";
my $fname = "path.ext";

$srv_cgi_uri->path("/dl.cgi/$hash/$fname");

print $srv_cgi_uri, "\n";

__END__
http://s1.site.com/dl.cgi/some/path.ext

Upvotes: 1

Alex
Alex

Reputation: 5893

Like this:

my $new = $file->{srv_cgi_url};
$new =~ s{/cgi-bin/}{};

That is all. See perldoc perlre for details.

Upvotes: 1

Related Questions