in reply to Funny code

For anyone who's wondering: it prints the basename of the script it runs in, eg for /home/ap/t.pl it produces t.pl. The $^O stuff is to distinguish DOS vs Unix path separators (backslash vs slash).

It's not obfuscated at all, just overly dense, and not even really portable (slash works as a path separator on DOS as well; and it will fail on some systems like old MacOS and likely VMS). Here's how it should be done, readably, safely, and portably:

use File::Spec::Functions qw( rel2abs splitpath ); print +( splitpath rel2abs $0 )[2];

Or, better yet, if you don't mind loading another module:

use File::Spec::Functions qw( rel2abs ); use File::Basename; print basename rel2abs $0;

But placing too much trust in $0 in the first place is a mistake anyway…

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^2: Funny code (not amused)
by ysth (Canon) on Aug 18, 2004 at 22:27 UTC
    The $^O stuff is to distinguish DOS vs Unix path separators (backslash vs slash).
    That may be it's purpose, but in fact it is having no effect at all. First the code tries removing everything through the last backslash; if there was no backslash it tries removing everything through the last slash. I still can't tell if the $^O part is intentional obfu or just a typo.

      For the third time, it is a typo, not an attempt at obfuscation. Please don't make me tell you again.

      - tye