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

here's the code:

#!/usr/local/bin/perl -w

use strict;

my $date_suffix;



# The directory I am running this script from: "/home/limo/Perl/2000/0827/test_dir";

# my $path = system('pwd'); DOESNT WORK
# my $path  "."; DOESNT WORK
# my $path  "./"; DOESNT WORK

if ($path  =~ /(\d\d\d\d.\d\d\d\d)/) {
  
  $date_suffix = $1;
  $date_suffix =~ tr/\//\./;
}

print "$date_suffix\n";


# I want $date_suffix to contain the string: "2000.0827"
# It works when I use "my REAL $path" as stated above
What am I doing wrong here????
  • Comment on Writing current working directory to $variable

Replies are listed 'Best First'.
Re: Writing current working directory to $variable
by ar0n (Priest) on Aug 27, 2000 at 03:23 UTC
    use Cwd; my $dir = getcwd;
    system() only returns the status code. you can use backticks (`) if you want the output:
    my $dir = `pwd`;
    ps. read merlyn's comment below.

    -- ar0n || Just Another Perl Joe

      this returns: Use of uninitialized value at get-filename.date.pl line 21.
        never mind.....typo....my bad! Thanks!
Re: Writing current working directory to $variable
by athomason (Curate) on Aug 27, 2000 at 03:41 UTC
    /(\d\d\d\d.\d\d\d\d)/ will match on "/home/limo/Perl/2000/0827/test_dir", but $1 won't have what you want (the . token matches any single character except \n, but it doesn't replace it with a period). Try this.
    #!/usr/ug/bin/perl -w use strict; use Cwd; my $dir = getcwd; my $date_suffix; $dir =~ m!(\d{4})/(\d{4})! or die "In a bad directory: $dir"; $date_suffix = "$1.$2"; print "$date_suffix\n";
      Works like a charm! Thanks!