Skip to content

file name with leading and/or trailing space #95

@jackdeguest

Description

@jackdeguest

In URI version 5.10, if a file name is provided that has leading and or trailing spaces, those will be removed since URI::file::Base::new() calls URI->new to generate the object, and URI explicitly states it removes leading and trailing spaces.

See code below as an example:

#!/usr/local/bin/perl
use URI::file;
use File::Spec;
use v5.16;

my $file = ' some file ';
my $u = URI::file->new_abs( $file );
say "file path is: '$u'";
my @dirs = File::Spec->splitdir( $u->file( 'Unix' ) );
say $dirs[-1] eq $file ? 'ok' : ( "not ok: '" . $dirs[-1] . "', expected '$file'" );
exit(0);

__END__

This will yield something like:

file path is: 'file:///home/joe/some%20file'
not ok: 'some file', expected ' some file '

whereas, it should return: file:///home/joe/%20some%20file%20

and $u->file should return ' some file '

The mitigation is to set back the path after the object has been instantiated, such as:

use URI::file;
use Cwd;
my $file = ' some file ';
my $u2 = URI::file->new( $file );
$u2->path( $file );
$u2 = $u2->abs( Cwd::cwd );
say "'" . $u2->file( 'Unix' ) . "'";

which would correctly yield '/home/joe/ some file '

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions