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

Hi, monks !

I use code bellow to test , but it not works:

perl -e '-e "~/.ssh/known_hosts" and print "OK" '
but replace the tilde symbol with $ENV{HOME} , it works and printout "OK":
perl -e '-e "$ENV{HOME}/.ssh/known_hosts" and print "OK"'
so, does the tilde symbol (~) not works in perl ? and what situation it works ? very thanks your wisdom :)

Replies are listed 'Best First'.
Re: Why tilde symbol not works in perl file testing
by toolic (Bishop) on Aug 23, 2013 at 01:50 UTC
    As you have shown, the tilde does not work in perl as it does in the shell. If you want tilde behavior, it looks like File::Glob might work: GLOB_TILDE.
Re: Why tilde symbol now works in perl file testing
by Athanasius (Archbishop) on Aug 23, 2013 at 02:13 UTC
Re: Why tilde symbol now works in perl file testing
by MidLifeXis (Monsignor) on Aug 23, 2013 at 12:56 UTC

    Tilde referring to the home directory is a shell construct, and as such, is expanded by the shell (or other programs that explicitly handle the tilde). See bash in the section Tilde Expansion for how it works in bash.

    Each program handles its domain specific language independently of other programs. Even if the way different programs handle things is similar, you need to go to the language specific for that program. Perl has a different way of handling it from the shell.

    --MidLifeXis