A special variable in Perl which simply means the default -- used whenever an operator or function hasn't been given any other variable to work with, and used extensively by Perl hackers. In particular it can be used in a while loop to represent each line of an input file.

In Perl 6, $_ is known as the topic. It is lexically scoped (unlike in Perl 5), and is automatically assigned by topicalizers.

Some of the various topicalizers are for, given, CATCH (which is a special form of given), sub and -> (pointy sub), and bare closures.

What follows is an example of topicalizing blocks (in a simple ELIZA program):

    for <> -> $line {    # Both $line and $_ are <> 
        given $line {    # $line aliased to $_ (as before)
            when :iw/i love (\S+)/ {
                print "Do you have further plans for $1?\n"
            }
            when :i/sex/ {
                print "Tell me more about your sex life.\n"
            }
            when :i/eliza/ {
                print "You talkin' bout me?\n"
            }
        }
    }

The for and given alias to $_, and the pointy sub (-> in the first line) aliases to its argument.

The funny slashy things with colons are the regular expressions from Perl 6. The rest should be fairly self explanatory.

In DOS (and Windows NT/2000/XP, and possibly OS/2) prompt strings, $_ generates a new line. This can be quite useful.

Often, people use $p$g as their prompt string, which shows their current directory ($p) and then the > prompt character ($g). With modern disks, and especially Microsoft's love with long path names (such as "Documents and Settings", "Temporary Internet Files" and the like), this can end up rather long, and the actual prompt character (and therefore where you type) gets lost.

So, using $p$_$n$g can make life easier. You still get your whole path, but you then get the prompt on a new line.

(Back in the good old DOS days, when everybody loaded an ANSI screen driver, you could do clever tricks that put a "status bar" at the top of the screen with the current path and current date and time in pretty colours. Hum de hum.)

wertperch said I actually miss ANSI.SYS. Hm. Wonder if it's still there in any form. I believe it is there - c:\windows\system32\ansi.sys or c:\winnt\system32\ansi.sys - but it's a bit messy to get it to load into a command prompt.

Log in or register to write something here or to contact authors.