http://www.perlmonks.org?node_id=11135056


in reply to Re^3: Getting the absolute path of a script, without PWD
in thread Getting the shell's version of working directory, without PWD's help

This seems to be a portability issue.

cwd()."\n" eq `pwd -L`

I'd say file a bugreport against Cwd and in the meantime resort to `pwd` or `pwd -L` on systems where it fails.

update

after further investigation, it seems that `pwd` inside Perl doesn't default like pwd in bash ... which is unexpected

lanx@ubuntu14-large:/tmp/sym$ perl -MCwd -E'say `pwd` ' /tmp/dir lanx@ubuntu14-large:/tmp/sym$ pwd /tmp/sym lanx@ubuntu14-large:/tmp/sym$ perl -MCwd -E'say `pwd -L` ' /tmp/sym lanx@ubuntu14-large:/tmp/sym$

so strictly speaking, this is not a bug in Cwd, since cwd() returns the same like `pwd` (which does not always -L )

The question is why is bash's pwd defaulting differently inside Perl?

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^5: Getting the absolute path of a script, without PWD (bug)
by LanX (Saint) on Jul 16, 2021 at 07:50 UTC
    > The question is why is bash's pwd defaulting differently inside Perl?

    From qx

    Similarly to system, if the string contains no shell metacharacters then it will executed directly.

    Not sure what is included into "shell metacharacters" but a simple semicolon seems to suffice to force execution via the shell ...

    lanx@ubuntu14-large:/tmp/sym$ perl -MCwd -E'say `pwd;` ' /tmp/sym lanx@ubuntu14-large:/tmp/sym$ perl -MCwd -E'say `pwd` ' /tmp/dir lanx@ubuntu14-large:/tmp/sym$

    update

    simplified code

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      You're 1 minute faster :-)
        Please, you should really file a bugreport against Cwd.

        Maybe they can't fix cwd() b/c of backwards compatibility but should at least clarify the documentation.

        From my POV it's already silly to call two different routines getcwd() and cwd()

        So you could suggest to add a new pwd() which always does something like `pwd -L`

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Re^5: Getting the absolute path of a script, without PWD (bug?)(update)
by perlancar (Hermit) on Jul 16, 2021 at 07:51 UTC
    after further investigation, it seems that `pwd` inside Perl doesn't default like pwd in bash ... which is unexpected

    The answer is perhaps this in perlop for the qx//:

    if the string contains no shell metacharacters then it will executed directly.

    So although in general qx// uses the shell, if we say `pwd` then perl will execute /bin/pwd, which defaults to -P. If we say `bash -c pwd` then bash will execute its shell builtin version of pwd, which defaults to -L.

      > it seems that `pwd` inside Perl doesn't default like pwd in bash

      Note that qx// doesn't call bash but sh, which might be a different shell (/bin/dash on Ubuntu, for example).

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        Noted, but that's not the cause of the difference here. `pwd` under dash still calls the shell builtin, which defaults to pwd -L.