forked from szabgab/perlmaven.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththe-default-variable-of-perl.tt
176 lines (129 loc) · 4.33 KB
/
the-default-variable-of-perl.tt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
=title $_ the default variable of Perl
=timestamp 2012-08-14T20:41:51
=indexes Perl, $_, scalar, default, variable, topic
=status show
=author szabgab
=index 1
=archive 1
=feed 1
=comments 1
=social 1
=abstract start
There is a strange scalar variable called <hl>$_</hl> in Perl, which is the
<hl>default variable</hl>, or in other words the <b>topic</b>.
In Perl, several functions and operators use this variable as a default,
in case no parameter is explicitly used. In general, I'd say you should <b>NOT</b>
see <hl>$_</hl> in real code. I think the whole point of <hl>$_</hl> is that you don't
have to write it explicitly.
Well, except when you do.
=abstract end
Having a default variable is a very powerful idea, but using it
incorrectly can reduce the readability of your code.
Check out this script:
<code lang="perl">
use strict;
use warnings;
use v5.10;
while (<STDIN>) {
chomp;
if (/MATCH/) {
say;
}
}
</code>
This is exactly the same as:
<code lang="perl">
use strict;
use warnings;
use v5.10;
while ($_ = <STDIN>) {
chomp $_;
if ($_ =~ /MATCH/) {
say $_;
}
}
</code>
I would never write the second one, and I'd probably write the first one only
in a very small script, or in a very tight part of the code.
Maybe not even there.
As you can see, in a <hl>while</hl> loop, when you read from a file handle,
even if that's the standard input, if you don't assign it explicitly
to any variable, the line that was read will be assigned to <hl>$_</hl>.
<hl>chomp()</hl> defaults to work on this variable, if no parameter was given.
Regular expression matching can be written without an explicit string,
and even without the <hl>=~</hl> operator. If written that way, it will work on the
content of <hl>$_</hl>.
Finally <hl>say()</hl>, just as <hl>print()</hl>, would print the content
of <hl>$_</hl>, if no other parameter was given.
<h2>split</h2>
The second parameter of <hl>split</hl> is the string to be cut in pieces.
If no second parameter is given, split will cut up the content of <hl>$_</hl>.
<code lang="perl">
my @fields = split /:/;
</code>
<h2>foreach</h2>
If we don't supply the name of the iterator variable to <hl>foreach</hl>,
it will use <hl>$_</hl>.
<code lang="perl">
use strict;
use warnings;
use v5.10;
my @names = qw(Foo Bar Baz);
foreach (@names) { # puts values in $_
say;
}
</code>
<h2>Assignment in condition</h2>
There are some cases when we implicitly use <hl>$_</hl> by mistake.
Some experts might use this kind of code deliberately,
but when this is written by a newbie, or a mere mortal, it is just a bug.
<code lang="perl">
if ($line = /regex/) {
}
</code>
You see, instead of the regex operator: <hl>=~</hl> we used here the plain assignment operator: <hl>=</hl>.
This is, in fact the same as
<code lang="perl">
if ($line = $_ =~ /regex/) {
}
</code>
It takes the content of <hl>$_</hl>, executes the pattern matching on it,
and assigns the result to <hl>$line</hl>. Then checks if the content of $line is true or false.
<h2>Explicit $_</h2>
I mentioned earlier I recommend <b>not</b> using <hl>$_</hl> explicitly. Sometimes I see people writing code like this:
<code lang="perl">
while (<$fh>) {
chomp;
my $prefix = substr $_, 0, 7;
}
</code>
I think, once you use a statement in perl that forces you to explicitly write out <hl>$_</hl>,
such as <hl>substr</hl> in our case, you should go all the way and use a more meaningful name.
Even if that means more typing:
<code lang="perl">
while (my $line = <$fh>) {
chomp $line;
my $prefix = substr $line, 0, 7;
}
</code>
Another bad example I often see:
<code lang="perl">
while (<$fh>) {
my $line = $_;
...
}
</code>
This probably happens to people who do not understand the interaction between
the <hl>while</hl> statement, the read operator on the file handle and <hl>$_</hl>.
This could be written in a more simple way directly assigning to the <hl>$line</hl>
variable.
<code lang="perl">
while (my $line = <$fh>) {
...
}
</code>
<h2>Exceptions</h2>
There are several cases where you can't really avoid, and you have to use <hl>$_</hl>
explicitly. These are the <a href="http://szabgab.com/filtering-values-with-perl-grep.html">grep</a>
and <a href="http://szabgab.com/transforming-a-perl-array-using-map.html">map</a> function, and the
other similar ones, such as <a href="http://szabgab.com/filtering-values-with-perl-grep.html">any</a>.