in reply to Strip part of url

use File::Basename; my $url = "http://google.com/folder/1234.html"; print basename($url, '.html');

- Miller

Replies are listed 'Best First'.
Re^2: Strip part of url
by afoken (Chancellor) on Feb 12, 2011 at 15:26 UTC

    What makes you think that File::Basename is the correct tool to handle URLs? It will fail at least on Mac OS.

    The correct way to handle URLs is to use the URI package.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      I agree about the URI recommendation but that code works fine on OS X.

        "Mac OS" ne "OS X". Mac OS has ":" as path delimiter, OS X is derived from Unix and thus has a "/" as path delimiter.

        File::Basename can change its idea of the operating system, so you can see the problem on every operating system:

        use File::Basename; my $url = "http://google.com/folder/1234.html"; print "Native: ",basename($url,'.html') +,"\n"; fileparse_set_fstype('Unix'); print "Unix: ", basename($url,'.html') +,"\n"; fileparse_set_fstype('MacOS'); print "MacOS: ", basename($url,'.html') +,"\n";

        Output on Windows:

        Native: 1234
        Unix: 1234
        MacOS: //google.com/folder/1234
        

        Abusing that feature to force Unix semantics doesn't really help:

        use File::Basename; my $url = "http://google.com/folder/1234.html?session=4247110815"; print "Native: ",basename($url,'.html') +,"\n"; fileparse_set_fstype('Unix'); print "Unix: ", basename($url,'.html') +,"\n"; fileparse_set_fstype('MacOS'); print "MacOS: ", basename($url,'.html') +,"\n";

        Output:

        Native: 1234.html?session=4247110815
        Unix: 1234.html?session=4247110815
        MacOS: //google.com/folder/1234.html?session=4247110815
        

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)