Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange `pwd` output from Perl

Tags:

perl

I have a very short tutorial Perl script:

#!/usr/bin/perl
print "The date is ",`date`;
print "The date is `date`",".\n";
$directory=`pwd`;
print "\nThe current directory is $directory.";

and the output:

The date is Sat Jul  2 17:04:58 PDT 2011
The date is `date`.

The current directory is total 20
-rwxr-xr-x 1 yan yan 433 2011-07-02 15:58 36
-rwxr-xr-x 1 yan yan 313 2011-07-02 16:29 43
-rwxr-xr-x 1 yan yan 116 2011-07-02 16:51 45
-rwxr-xr-x 1 yan yan 149 2011-07-02 16:53 46
-rwxr-xr-x 1 yan yan 145 2011-07-02 17:02 47

But, if I just run pwd, I got:

yan@ubuntu:~/bin/blackperl$ pwd
/home/yan/bin/blackperl

Is there any logical explanation to the mystery here?

like image 413
digit plumber Avatar asked Nov 16 '25 20:11

digit plumber


1 Answers

I can't exlpain the mystery either, but you could try to use the Perl Core module Cwd instead of the pwd command:

use warnings;
use strict;
use Cwd;
my $dir = getcwd();
print "$dir\n";

One advantage is that Cwd is more portable than pwd.

See also: UNIX 'command' equivalents in Perl

like image 193
toolic Avatar answered Nov 19 '25 11:11

toolic