in reply to Re^4: Regex match on implicit default variable ($_) in a script, not a one liner
in thread Regex match on implicit default variable ($_) in a script, not a one liner
For instance, is "<>" short hand for "<STDIN>"?.
<> is a very common and useful shorthand notation. You'll often encounter this, and I recommend getting familiar with it, it really comes in handy every time you want to process user-supplied input.
The short version is that it Does What You Mean; I/O Operators has the nitty-gritty:
The null filehandle <> is special: it can be used to emulate the behavior of sed and awk, and any other Unix filter program that takes a list of filenames, doing the same to each line of input from all of them. Input from <> comes either from standard input, or from each file listed on the command line. Here's how it works: the first time <> is evaluated, the @ARGV array is checked, and if it is empty, $ARGV[0] is set to "-", which when opened gives you standard input. The @ARGV array is then processed as a list of filenames. The loop
while (<>) { ... # code for each line }is equivalent to the following Perl-like pseudo code:
unshift(@ARGV, '-') unless @ARGV; while ($ARGV = shift) { open(ARGV, $ARGV); while (<ARGV>) { ... # code for each line } }except that it isn't so cumbersome to say, and will actually work. [...]
For the various flags to the perl executable, see perlrun. -n and -p put certain loops around your code, allowing you to write only the code inside the loop (very useful for one-liners on the command line); -l auto-chomps your input, which is useful in much the same situations.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Regex match on implicit default variable ($_) in a script, not a one liner
by Todd Chester (Scribe) on Oct 24, 2015 at 23:07 UTC | |
by AppleFritter (Vicar) on Oct 25, 2015 at 00:09 UTC |