I think he ment to say local($_), the local() means to keep whatever variables in the brackets local to the function in perl. The $_ variable is a special variable that will give you the last result of a function or calling function.
There are more punctuation variables than just $_, you know. The $/ variable is the input record separator. It defaults to a newline "\n", but if you change it, you can change the meaning of a "line".
For example, if you want to read the sections of a fortune file, they're formatted like this:
Funny quote here...
%%
Here's another quote that spans more than two
Lines, so it'd be annoying to use \n as
the record separator, no?
%%
Etc.
So that's where $/ becomes handy. Even better, when you chomp(), you're not just removing an ending newline from a string, you're removing an ending $/ from the string. And the variable has two special values, the empty string "", and undef. Check perlvar for more info.
randfortune
#!/usr/bin/perl
$/ = "%%\n"; open FORTUNE, shift or die "usage: randfortune FILE\n"; while (<FORTUNE>) { if (rand($.) < 1) { $line = $_ } } close FORTUNE; chomp $line; print $line;
'
That nifty little program reads a fortune-style file and gives you a random entry. It uses the $. variable too, which is the line number of the last line read from the last filehandle you read from. It, too, is in perlvar. And the program uses a nice, fair algorithm
for choosing a random line.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.
omni posted this at 07:15 — 19th October 2000.
They have: 32 posts
Joined: Oct 2000
I think he ment to say local($_), the local() means to keep whatever variables in the brackets local to the function in perl. The $_ variable is a special variable that will give you the last result of a function or calling function.
Greg
Need help? Ask an expert.
infoDNS - Domain Name Registration and Information Services
infoForums - Domain Name Registration Forums and Information Services
japhy posted this at 11:49 — 19th October 2000.
They have: 161 posts
Joined: Dec 1999
There are more punctuation variables than just $_, you know. The $/ variable is the input record separator. It defaults to a newline "\n", but if you change it, you can change the meaning of a "line".
For example, if you want to read the sections of a fortune file, they're formatted like this:
Funny quote here...
%%
Here's another quote that spans more than two
Lines, so it'd be annoying to use \n as
the record separator, no?
%%
Etc.
So that's where $/ becomes handy. Even better, when you chomp(), you're not just removing an ending newline from a string, you're removing an ending $/ from the string. And the variable has two special values, the empty string "", and undef. Check perlvar for more info.
randfortune
#!/usr/bin/perl
$/ = "%%\n";
open FORTUNE, shift or die "usage: randfortune FILE\n";
while (<FORTUNE>) {
if (rand($.) < 1) { $line = $_ }
}
close FORTUNE;
chomp $line;
print $line;
That nifty little program reads a fortune-style file and gives you a random entry. It uses the $. variable too, which is the line number of the last line read from the last filehandle you read from. It, too, is in perlvar. And the program uses a nice, fair algorithm
for choosing a random line.
Orpheus posted this at 20:46 — 19th October 2000.
They have: 568 posts
Joined: Nov 1999
Nope, I mean $\ not $_
japhy posted this at 21:25 — 19th October 2000.
They have: 161 posts
Joined: Dec 1999
You have it a bit backwards, then, Orpheus... $\ is
the output record separator. That is:
print 1; print 2; print 3; # prints "123"
$\ = "#";
print 1; print 2; print 3; # prints "1#2#3#"
$\ = " ";
print 1; print 2; print 3; # prints "1 2 3 "
$\ = ",";
print 1; print 2; print 3; # prints "1,2,3,"
I think you meant the $/ variable...
Orpheus posted this at 05:08 — 21st October 2000.
They have: 568 posts
Joined: Nov 1999
We all make typos when we're out of coffee.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.