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 | |
by tye (Sage) on Aug 19, 2004 at 01:33 UTC |