File-Fu

 view release on metacpan or  search on metacpan

lib/File/Fu.pm  view on Meta::CPAN

=head2 program_name

The absolute name of your program.  This will be relative from the time
File::Fu was loaded.  It dies if the name is '-e'.

  my $prog = File::Fu->program_name;

If File::Fu was loaded after a chdir and the $0 was relative, calling
program_name() throws an error.  (Unless you set $0 correctly before
requiring File::Fu.)

=head2 program_dir

Returns what typically corresponds to program_name()->dirname, but
just the compile-time cwd() when $0 is -e/-E.

  my $dir = File::Fu->program_dir;

=cut

{
# fun startup stuff and various logic:
my $prog = $0;
my $name_sub;
my $dir_sub;
if(lc($prog) eq '-e') {
  my $prog_dir = Cwd::cwd();
  $dir_sub  = eval(qq(sub {shift->dir("$prog_dir")}));
  $name_sub = eval(qq(sub {croak("program_name => '$prog'")}));
}
else {
  if(-e $prog) {
    my $prog_name = __PACKAGE__->file($prog)->absolutely;
    my $prog_dir = $prog_name->dirname;
    $name_sub = eval(qq(sub {shift->file('$prog_name')}));
    $dir_sub  = eval(qq(sub {shift->dir('$prog_dir')}));
  }
  else {
    # runtime error
    $dir_sub  = sub {croak("$prog not found => no program_dir known")};
    $name_sub = sub {croak("$prog not found => no program_name known")};
  }
}
*program_name = $name_sub;
*program_dir  = $dir_sub;
} # program_name/program_dir
########################################################################

=head1 Class Methods

=head2 THIS_FILE

A nicer way to say __FILE__.

  my $file = File::Fu->THIS_FILE;

=cut

sub THIS_FILE {
  my $package = shift;
  my $name = (caller)[1];
  return $package->file($name);
} # end subroutine THIS_FILE definition
########################################################################

=head2 cwd

The current working directory.

  my $dir = File::Fu->cwd;

=cut

sub cwd {
  my $package = shift;

  defined(my $ans = Cwd::cwd()) or croak("cwd() failed");
  return $package->dir($ans);
} # end subroutine cwd definition
########################################################################

=head2 which

Returns File::Fu::File objects of ordered candidates for $name found in
the path.

  my @prog = File::Fu->which($name) or die "cannot find $name";

If called in scalar context, returns a single File::Fu::File object or throws an error if no candidates were found.

  my $prog = File::Fu->which($name);

=cut

sub which {
  my $package = shift;
  croak("must have an argument") unless(@_);
  my ($what) = @_;

  require File::Which;
  if(wantarray) {
    return map({$package->file($_)} File::Which::which($what));
  }
  else {
    my $found = scalar(File::Which::which($what)) or
      croak("cannot locate '$what' in PATH");
    return $package->file($found);
  }
} # which ##############################################################

=head1 Temporary Directories and Files

These class methods call the corresponding File::Fu::Dir methods on the
value of tmp().  That is, you get a temporary file/dir in the '/tmp/'
directory.

=head2 temp_dir

  my $dir = File::Fu->temp_dir;

=cut



( run in 0.752 second using v1.01-cache-2.11-cpan-5511b514fd6 )