Skip to content

Commit

Permalink
basename() and dirname() return strings. Add parent()
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Henning Thorsen committed Jan 7, 2017
1 parent 7e1e85f commit fa2188d
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 14 deletions.
6 changes: 4 additions & 2 deletions lib/Mojo/File.pm
Expand Up @@ -21,14 +21,14 @@ use Scalar::Util 'blessed';

our @EXPORT_OK = ('path', 'tempdir');

sub basename { $_[0]->new(scalar File::Basename::basename ${$_[0]}) }
sub basename { scalar File::Basename::basename ${$_[0]} }

sub child {
my $self = shift;
return $self->new($self, @_);
}

sub dirname { $_[0]->new(scalar File::Basename::dirname ${$_[0]}) }
sub dirname { scalar File::Basename::dirname ${$_[0]} }

sub list_tree {
my ($self, $options) = (shift, shift // {});
Expand Down Expand Up @@ -75,6 +75,8 @@ sub new {
return $self;
}

sub parent { $_[0]->new(scalar File::Basename::dirname ${$_[0]}) }

sub path { __PACKAGE__->new(@_) }

sub slurp { Mojo::Util::slurp(${shift()}) }
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Home.pm
Expand Up @@ -46,7 +46,7 @@ sub list_files {
->to_array;
}

sub mojo_lib_dir { path(__FILE__)->dirname->child('..')->to_string }
sub mojo_lib_dir { path(__FILE__)->parent->parent->to_string }

sub new { @_ > 1 ? shift->SUPER::new->parse(@_) : shift->SUPER::new }

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/IOLoop/Server.pm
Expand Up @@ -17,8 +17,8 @@ use constant TLS_WRITE => TLS ? IO::Socket::SSL::SSL_WANT_WRITE() : 0;

# To regenerate the certificate run this command (18.04.2012)
# openssl req -new -x509 -keyout server.key -out server.crt -nodes -days 7300
my $CERT = path(__FILE__)->dirname->child('resources', 'server.crt')->to_string;
my $KEY = path(__FILE__)->dirname->child('resources', 'server.key')->to_string;
my $CERT = path(__FILE__)->parent->child('resources', 'server.crt')->to_string;
my $KEY = path(__FILE__)->parent->child('resources', 'server.key')->to_string;

has reactor => sub { Mojo::IOLoop->singleton->reactor };

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Server/Hypnotoad.pm
Expand Up @@ -49,7 +49,7 @@ sub run {
# Preload application and configure server
my $prefork = $self->prefork->cleanup(0);
$prefork->load_app($app)->config->{hypnotoad}{pid_file}
//= path($ENV{HYPNOTOAD_APP})->dirname->child('hypnotoad.pid')->to_string;
//= path($ENV{HYPNOTOAD_APP})->parent->child('hypnotoad.pid')->to_string;
$self->configure('hypnotoad');
weaken $self;
$prefork->on(wait => sub { $self->_manage });
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -208,7 +208,7 @@ sub _multipart {
if (my $file = delete $value->{file}) {
$file = Mojo::Asset::File->new(path => $file) unless ref $file;
$part->asset($file);
$value->{filename} //= path($file->path)->basename->to_string
$value->{filename} //= path($file->path)->basename
if $file->isa('Mojo::Asset::File');
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Command/cpanify.pm
Expand Up @@ -19,7 +19,7 @@ sub run {
"https://$user:$password\@pause.perl.org/pause/authenquery" => form => {
HIDDENNAME => $user,
CAN_MULTIPART => 1,
pause99_add_uri_upload => path($file)->basename->to_string,
pause99_add_uri_upload => path($file)->basename,
SUBMIT_pause99_add_uri_httpupload => ' Upload this file from my disk ',
pause99_add_uri_uri => '',
pause99_add_uri_httpupload => {file => $file},
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -1552,7 +1552,7 @@ automatically installed with the modules.
my $self = shift;

# Switch to installable home directory
$self->home->parse(path(__FILE__)->dirname->child('MyApp')->to_string);
$self->home->parse(path(__FILE__)->parent->child('MyApp')->to_string);

# Switch to installable "public" directory
$self->static->paths->[0] = $self->home->rel_file('public');
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -1157,7 +1157,7 @@ when C<register> is called.
my ($self, $app) = @_;

# Append "templates" and "public" directories
my $base = path(__FILE__)->dirname->child('AlertAssets');
my $base = path(__FILE__)->parent->child('AlertAssets');
push @{$app->renderer->paths}, $base->child('templates')->to_string;
push @{$app->static->paths}, $base->child('public')->to_string;
}
Expand Down
5 changes: 2 additions & 3 deletions lib/Mojolicious/Lite.pm
Expand Up @@ -12,16 +12,15 @@ sub import {
$ENV{MOJO_EXE} ||= (caller)[1];

# Reuse home directory if possible
local $ENV{MOJO_HOME} = path($ENV{MOJO_EXE})->dirname->to_string
unless $ENV{MOJO_HOME};
local $ENV{MOJO_HOME} = path($ENV{MOJO_EXE})->dirname unless $ENV{MOJO_HOME};

# Initialize application class
my $caller = caller;
no strict 'refs';
push @{"${caller}::ISA"}, 'Mojo';

# Generate moniker based on filename
my $moniker = path($ENV{MOJO_EXE})->basename->to_string;
my $moniker = path($ENV{MOJO_EXE})->basename;
$moniker =~ s/\.(?:pl|pm|t)$//i;
my $app = shift->new(moniker => $moniker);

Expand Down
6 changes: 5 additions & 1 deletion t/mojo/file.t
Expand Up @@ -36,6 +36,10 @@ is path('file.t')->to_abs->basename, basename(abs_path 'file.t'), 'same path';
# Dirname
is path('file.t')->to_abs->dirname, dirname(abs_path 'file.t'), 'same path';

# Parent
is path('file.t')->to_abs->parent->to_string, path('file.t')->to_abs->dirname,
'same path';

# Temporary directory
my $dir = tempdir;
my $path = "$dir";
Expand All @@ -53,7 +57,7 @@ ok -d $subdir, 'directory exists';
# List tree
is_deeply path('does_not_exist')->list_tree->to_array, [], 'no files';
is_deeply path(__FILE__)->list_tree->to_array, [], 'no files';
my $lib = path(__FILE__)->dirname->child('lib', 'Mojo');
my $lib = path(__FILE__)->parent->child('lib', 'Mojo');
my @files = map { path($lib)->child(split '/') } (
'BaseTest/Base1.pm', 'BaseTest/Base2.pm',
'BaseTest/Base3.pm', 'DeprecationTest.pm',
Expand Down

0 comments on commit fa2188d

Please sign in to comment.