Click here to get back home

every combination of Y/N in 5 positions

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
every combination of Y/N in 5 positions joemacbusiness 03-31-2008
Posted by John W. Krahn on April 1, 2008, 10:45 pm
Please log in for more thread options
David Harmon wrote:
> On Mon, 31 Mar 2008 23:23:25 +0200 in comp.lang.perl.misc, Gunnar
>> I do, I hope. :)
>>
>> foreach my $num ( 0 .. 0b11111 ) {
>> local *_ = \ sprintf '%05b', $num;
>
> What is *_ ? It looks like one of those magic perl variables, but
> I don't find any documentation on it.

It is a typeglob. It means that you want all of the _ variables to have
a local value. See the "Typeglobs and Filehandles" section of perldata.pod.

perldoc perldata


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Posted by Tad J McClellan on April 1, 2008, 11:34 pm
Please log in for more thread options
> On Mon, 31 Mar 2008 23:23:25 +0200 in comp.lang.perl.misc, Gunnar
>>I do, I hope. :)
>>
>> foreach my $num ( 0 .. 0b11111 ) {
>> local *_ = \ sprintf '%05b', $num;
>
> What is *_ ?


It is a typeglob of the variables named underscore.


> It looks like one of those magic perl variables,
^^^
^^^

It is many of those magic perl variables.

It is $_ and @_ and _ and ...


> but
> I don't find any documentation on it.


See the "Typeglobs and Filehandles" section in perldata.pod.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"

Posted by Ben Morrow on April 2, 2008, 12:58 am
Please log in for more thread options

Quoth "Newsgroup only please, address is no longer replyable."
> On Mon, 31 Mar 2008 23:23:25 +0200 in comp.lang.perl.misc, Gunnar
> >I do, I hope. :)
> >
> > foreach my $num ( 0 .. 0b11111 ) {
> > local *_ = \ sprintf '%05b', $num;
>
> What is *_ ? It looks like one of those magic perl variables, but
> I don't find any documentation on it.

What noone has said yet is why Gunnar used it. Due to a rather nasty bug
in perl, under certain rather obscure circumstances[0] $_ doesn't
localise properly, so if you need to do so it is safer to localise the
whole of *_. Unfortunately, besides being ugly, this means you lose your
sub arguments (and the magic stat filehandle, of course, but that's
likely less important); personally I would always rather use a for loop
over one element

for (sprintf '%05b', $num) {

or, with 5.10, either 'given' (like for, but gives scalar context to its
argument) or 'my $_'. None of these suffer from the bug.

Ben

[0] If $_ is an alias to an element of a tied hash or array, the value
of that element will be localised along with $_. A simple example is

use Tie::Hash;

tie my %h, 'Tie::StdHash';
$h = 1;

for ($h) {
local $_ = 2;
print $h; # 2, but should be 1
}
print $h; # back to 1 again

This is only really important if you call external code in the scope of
the 'local': if that code reads the hash, it will be surprised to find
the values have changed.


Posted by Abigail on April 2, 2008, 8:33 am
Please log in for more thread options
_
David Harmon (source@netcom.com) wrote on VCCCXXVIII September MCMXCIII
[] On Mon, 31 Mar 2008 23:23:25 +0200 in comp.lang.perl.misc, Gunnar
[] >I do, I hope. :)
[] >
[] > foreach my $num ( 0 .. 0b11111 ) {
[] > local *_ = \ sprintf '%05b', $num;
[]
[] What is *_ ? It looks like one of those magic perl variables, but
[] I don't find any documentation on it.


It's a silly attempt to obfuscate the code.

It's much better written as:

foreach (0 .. 0b11111) {
my $_ = sprintf '%05b' => $_;
tr _01_NY_;
say "$::_:$_";
}


No silly typeglobs needed.


Abigail
--
map=chr}map$=+$]..3*$=/2;
print "$J$u$s$t $a$n$o$t$h$e$r $P$e$r$l $H$a$c$k$e$r\n";

Posted by Frank Seitz on April 2, 2008, 8:49 am
Please log in for more thread options
Abigail wrote:
>
> It's a silly attempt to obfuscate the code.
>
> It's much better written as:
>
> foreach (0 .. 0b11111) {
> my $_ = sprintf '%05b' => $_;
> tr _01_NY_;
> say "$::_:$_";
> }

Nice.

> No silly typeglobs needed.

You forgot

use 5.10.0;

Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

Similar ThreadsPosted
Looking for combination.pm June 29, 2005, 10:56 pm
Perl + Objects = Winning combination April 9, 2007, 7:55 pm
Perl Developer Positions in California March 11, 2005, 12:53 pm
htaccess rewriterule in combination with POST form January 28, 2005, 12:38 am
How can I add tokens at arbitrary positions on a line in a file? August 13, 2005, 6:36 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap