ostra has asked for the wisdom of the Perl Monks concerning the following question:

I am coding on a PC and learning about modules. What do you put for the $fullpath variable? See code

perl -w use strict; use warnings; use 5.014; use File::Basename; my $basename=basename($fullpath); my $dirname=dirname($fullpath); print "$basename"; print "\n\n"; print "$dirname";

Replies are listed 'Best First'.
Re: Basename Module
by toolic (Bishop) on Feb 08, 2014 at 14:28 UTC
    From the File::Basename POD, you specify a full path to a file (or directory), such as:
    my $fullpath = 'C:\foo\bar\baz';

    As an aside, you normally do not want to put a scalar by itself in double quotes:

    print $dirname;

      Thank you for the reply. It was putting the scalar in double quotes that was the issue. Thank you very much.

Re: Basename Module
by karlgoethebier (Abbot) on Feb 08, 2014 at 14:35 UTC

    Nomen est omen (AKA "the name is a sign"). Please see also File::Basename.

    Update:

    #!/usr/bin/env perl + use strict; use warnings; use File::Basename; my $fullpath = q(/Users/karl/monks/basename.pl); my $basename = basename($fullpath); my $dirname = dirname($fullpath); print qq(\$fullpath: $fullpath\n); print qq(\$basename: $basename\n); print qq(\$dirname: $dirname\n); __END__ karls-mac-mini:monks karl$ ./basename.pl + $fullpath: /Users/karl/monks/basename.pl $basename: basename.pl $dirname: /Users/karl/monks

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: Basename Module (use Path::Tiny)
by Anonymous Monk on Feb 09, 2014 at 00:10 UTC
    #!/usr/bin/perl -- use strict; use warnings; use Path::Tiny qw/ path cwd /; my $cwd = cwd(); my $thisfile = path( __FILE__ )->realpath; my $thisdir = path( $thisfile )->parent; my $dirname = $thisfile ->dirname; my $basename = $thisfile ->basename; print " cwd $cwd thisfile $thisfile thisdir $thisdir dirname $dirname basename $basename "; __END__ $ perl C:\one\two\file.pl cwd C:/ thisfile C:/one/two/file.pl thisdir C:/one/two dirname /one/two/ basename file.pl