[33053] in Perl-Users-Digest
Perl-Users Digest, Issue: 4329 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Dec 16 05:17:21 2014
Date: Tue, 16 Dec 2014 02:17:06 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 16 Dec 2014 Volume: 11 Number: 4329
Today's topics:
"->" operator optional in some circumstances? <see.my.sig@for.my.address>
Re: "->" operator optional in some circumstances? <news@lawshouse.org>
Re: "->" operator optional in some circumstances? <rweikusat@mobileactivedefense.com>
Re: Both substitute and filter <m@rtij.nl.invlalid>
Re: Both substitute and filter <see.my.sig@for.my.address>
Re: Both substitute and filter <bauhaus@futureapps.invalid>
Re: Both substitute and filter <rweikusat@mobileactivedefense.com>
Re: Both substitute and filter <rweikusat@mobileactivedefense.com>
Re: Both substitute and filter <bauhaus@futureapps.invalid>
Re: Both substitute and filter <bauhaus@futureapps.invalid>
Re: Both substitute and filter <rweikusat@mobileactivedefense.com>
Re: Both substitute and filter <rweikusat@mobileactivedefense.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 15 Dec 2014 02:38:42 -0800
From: Robbie Hatley <see.my.sig@for.my.address>
Subject: "->" operator optional in some circumstances?
Message-Id: <raednY2nYrmsJxPJnZ2dnUVZ57ydnZ2d@giganews.com>
I'm confused. On the one hand, what I've read about Perl intimates
that "Perl does no implicit dereferencing"; but on the other hand,
some code lines which look to *me* as if they're illegal actually
compile and run without error.
Say that variable $asdf is a reference to a hash. Then in the
following line of code, the "->" must be in there or it won't
even compile:
my $zxcv = $asdf->{ghjk}; # works
my $zxcv = $asdf{ghjk}; # error
HOWEVER, given an array of such references to hashes, it seems
that you can legally grab one value of one of the hashes,
with or without the "->":
my $zxcv = $yuio[$i]->{ghjk}; # works
my $zxcv = $yuio[$i]{ghjk}; # ALSO works
What the heck??? That last line shouldn't even compile, as
far as I can see.
For a working example of what I'm talking about, consider
the following test program, especially the lines commented
"-> appears to be optional here"
#!/usr/bin/perl
##########################################################
# file-test.perl #
# Test of file- and directory- related Perl techniques. #
##########################################################
use v5.14;
use strict;
use warnings;
use Cwd;
my $CurDir;
my @CurDirFiles;
# Get and open current working directory:
$CurDir = getcwd();
say ("current directory is ", $CurDir);
opendir(my $dot, ".") or die "Can\'t open directory. $!";
GETINFO: while (my $filename=readdir($dot))
{
# Is current file a "regular" file?
my $regular = (-f $filename) ? 1 : 0 ;
# Get info:
my ($inode, $mode, $nlinks, $size, $mtime) = (stat($filename))[1,2,3,7,9];
# Push file info record for this file onto array:
push @CurDirFiles,
{
"Name" => $filename,
"Inode" => $inode,
"Mode" => $mode,
"Regular" => $regular,
"NLinks" => $nlinks,
"Size" => $size,
"Time" => $mtime
};
};
closedir($dot);
my $count= scalar @CurDirFiles;
say "There are $count files in this directory.";
PRINTINFO: foreach my $fileinfo (@CurDirFiles)
{
printf
(
"%-40s %18s %10s %1s %2d %10d %10d\n",
$fileinfo->{Name}, # won't compile without ->
$fileinfo->{Inode}, # won't compile without ->
$fileinfo->{Mode}, # won't compile without ->
$fileinfo->{Regular}, # won't compile without ->
$fileinfo->{NLinks}, # won't compile without ->
$fileinfo->{Size}, # won't compile without ->
$fileinfo->{Time}, # won't compile without ->
);
}
for (my $i = 0 ; $i < ($count - 1) ; ++$i)
{
my $name1 = $CurDirFiles[$i]->{Name}; # -> appears to be optional here
my $inode1 = $CurDirFiles[$i]{Inode}; # -> appears to be optional here
for (my $j = $i + 1 ; $j < $count ; ++$j)
{
my $name2 = $CurDirFiles[$j]{Name}; # -> appears to be optional here
my $inode2 = $CurDirFiles[$j]->{Inode}; # -> appears to be optional here
# If these files have same inode, notify user:
if ($inode1 == $inode2)
{
print("\nThese two files share inode $inode1:\n$name1\n$name2\n");
}
}
}
foreach my $fileinfo (@CurDirFiles)
{
my $name = $fileinfo->{Name}; # won't compile without ->
my $nlinks = $fileinfo->{NLinks}; # won't compile without ->
# If current file has more than 1 nlink, alert user:
if ($nlinks >1)
{
print("\nThis file has $nlinks nlinks:\n$name\n");
}
}
exit 0;
--
Cheers,
Robbie Hatley
Midway City, CA, USA
perl -le 'print "\154o\156e\167o\154f\100w\145ll\56c\157m"'
http://www.well.com/user/lonewolf/
------------------------------
Date: Mon, 15 Dec 2014 11:28:03 +0000
From: Henry Law <news@lawshouse.org>
Subject: Re: "->" operator optional in some circumstances?
Message-Id: <e_SdnXtx9YNYWBPJnZ2dnUVZ8hudnZ2d@giganews.com>
On 15/12/14 10:38, Robbie Hatley wrote:
> Say that variable $asdf is a reference to a hash. Then in the
> following line of code, the "->" must be in there or it won't
> even compile:
>
> my $zxcv = $asdf->{ghjk}; # works
> my $zxcv = $asdf{ghjk}; # error
Quite so. Any reference, such as $asdf here, is itself a /scalar/ and
putting braces after a scalar is like putting a fish onto a bicycle.
> HOWEVER, given an array of such references to hashes, it seems
> that you can legally grab one value of one of the hashes,
> with or without the "->"
Also quite so; it's documented in "perlreftut"; here's an online
reference to save you having to find it on your machine:
http://perldoc.perl.org/perlreftut.html
If you ask yourself what /else/ $foo->{bar}{quux} might mean other than
$foo->{bar}->{quux} I think you'll see why this is both useful and very
sensible.
--
Henry Law Manchester, England
------------------------------
Date: Mon, 15 Dec 2014 15:35:46 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: "->" operator optional in some circumstances?
Message-Id: <87ppbldiy5.fsf@doppelsaurus.mobileactivedefense.com>
Robbie Hatley <see.my.sig@for.my.address> writes:
[...]
> Say that variable $asdf is a reference to a hash. Then in the
> following line of code, the "->" must be in there or it won't
> even compile:
>
> my $zxcv = $asdf->{ghjk}; # works
> my $zxcv = $asdf{ghjk}; # error
>
> HOWEVER, given an array of such references to hashes, it seems
> that you can legally grab one value of one of the hashes,
> with or without the "->":
>
> my $zxcv = $yuio[$i]->{ghjk}; # works
> my $zxcv = $yuio[$i]{ghjk}; # ALSO works
>
> What the heck??? That last line shouldn't even compile, as
> far as I can see.
Quoting 'perldoc perlref'
One more thing here. The arrow is optional between brackets
subscripts,
------------------------------
Date: Mon, 15 Dec 2014 09:06:00 +0100
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: Both substitute and filter
Message-Id: <8ch2mb-mbb.ln1@news.rtij.nl>
On Mon, 15 Dec 2014 06:03:42 +0000, Tim McDaniel wrote:
> In article <8fltlb-ndq.ln1@news.rtij.nl>,
> Martijn Lievaart <m@rtij.nl.invlalid> wrote:
>>I might use something like (untested)
>>
>>@results = map s/^TARGET//, grep /^TARGET, @sources;
>
> I really don't like to use map or grep without curly braces.
>
I use curlies usually, but here I find this more readable.
>
>>or even
>>
>>@results = map substr($_,6), grep /^TARGET, @sources;
>
> (Why do you keep forgetting to close the regexp?)
Copy paste error.
>
> I also don't like substr in Perl. (The current code uses it, in fact,
> and I'd like to lobby against it.)
Why not. substr is a lot faster than using regexps.
mmmmm.
@sources = ("hi", "there", "TARGET23", "hello", "TARGETblarg", "world");
@results = ()
for (@sources) {
push @results, substr($_, 6) if (substr($_, 0, 6) eq 'TARGET');
}
Could be by far the fastest of the bunch, even if it is the least
'Perlish'.
>
>>I would write your solution 3) as
>>
>>@results = map {/^TARGET(.*)/ ? $1 : ()} @sources;
>
> As I noted later, if the pattern matches, it'll return $1, and if it
> doesn't match, it returns (), so the conditional is unneeded and it can
> be just
>
>>@results = map { /^TARGET(.*)/ } @sources;
>
> Unless you think that's too obscure and the ?$1:() is needed for
> clarity.
Nice trick! I think it is immediately clear even for those who don't know
the trick.
M4
------------------------------
Date: Mon, 15 Dec 2014 01:47:05 -0800
From: Robbie Hatley <see.my.sig@for.my.address>
Subject: Re: Both substitute and filter
Message-Id: <pIOdnVgoYJ6EMxPJnZ2dnUVZ572dnZ2d@giganews.com>
On 12/12/2014 3:49 PM, Tim McDaniel wrote:
> @sources = ("hi", "there", "TARGET23", "hello", "TARGETblarg", "world");
> @results = ()
> for each member of @sources
> if s/^TARGET// matched
> push onto @results the result of the s///
> else
> do nothing
How about this?
#!/usr/bin/perl
use v5.14;
use strict;
use warnings;
@::sources = ("hi", "there", "TARGET23", "hello", "TARGETblarg", "world");
@::results = ();
map {my $matched = ($_ =~ s/^TARGET//); push(@::results, $_) if $matched;} @::sources;
say join(", ", @::results);
prints:
23, blarg
--
Cheers,
Robbie Hatley
Midway City, CA, USA
perl -le 'print "\154o\156e\167o\154f\100w\145ll\56c\157m"'
http://www.well.com/user/lonewolf/
------------------------------
Date: Mon, 15 Dec 2014 11:08:35 +0100
From: Georg Bauhaus <bauhaus@futureapps.invalid>
Subject: Re: Both substitute and filter
Message-Id: <m6mbug$7s0$1@dont-email.me>
On 15.12.14 09:06, Martijn Lievaart wrote:
> @sources = ("hi", "there", "TARGET23", "hello", "TARGETblarg", "world");
> @results = ()
> for (@sources) {
> push @results, substr($_, 6) if (substr($_, 0, 6) eq 'TARGET');
> }
>
> Could be by far the fastest of the bunch, even if it is the least
> 'Perlish'.
It seems so, and using “index($_, 'TARGET') == 0” only adds a minor improvement
on top. So, in an inner loop, a little comment could state enough justification
for not using stylish REs.
------------------------------
Date: Mon, 15 Dec 2014 15:11:28 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Both substitute and filter
Message-Id: <87y4q9dk2n.fsf@doppelsaurus.mobileactivedefense.com>
Steve May <stevem_@nogood.com> writes:
> On 12/14/2014 04:49 AM, Rainer Weikusat wrote:
>> Steve May <stevem_@nogood.com> writes:
[...]
>>> my @results = ();
>>
>> This is pointless: You're assigning an empty list to an empty array
>> which results in an empty array.
>
> Pointless from a strictly required standpoint, mostly yes.
No, 'totally yes'.
my @result;
creates a lexically scoped, empty array. As stated by 'perldoc -f
delete',
assigning the empty list [...] is the customary way to empty out
an aggregate
but since the aggregate is emtpy, "emptying it" doesn't make it any
emptier.
> For self documentation standpoint, ease of reading, and some specific
> cases I'm not so sure.
If you want to document something in code, say
my @array; # don't forget that it's emtpy!
use a comment. That's the construct for adding inline
documentation. Useless code is just useless code.
------------------------------
Date: Mon, 15 Dec 2014 15:33:18 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Both substitute and filter
Message-Id: <87tx0xdj29.fsf@doppelsaurus.mobileactivedefense.com>
Martijn Lievaart <m@rtij.nl.invlalid> writes:
> On Mon, 15 Dec 2014 06:03:42 +0000, Tim McDaniel wrote:
[...]
>> I also don't like substr in Perl. (The current code uses it, in fact,
>> and I'd like to lobby against it.)
>
> Why not. substr is a lot faster than using regexps.
>
> mmmmm.
>
> @sources = ("hi", "there", "TARGET23", "hello", "TARGETblarg", "world");
> @results = ()
> for (@sources) {
> push @results, substr($_, 6) if (substr($_, 0, 6) eq 'TARGET');
> }
>
> Could be by far the fastest of the bunch, even if it is the least
> 'Perlish'.
It is (the fastest). It's, however, code which is hard to maintain
because the length of the prefix appears in three different places (the
substrs and the prefix itself), all of which have to be changed in sync
for the algorithm to work as intended for a different prefix. And the
prefix could, of course, as well be
supercallifragilistischexplialligetisch
------------------------------
Date: Mon, 15 Dec 2014 17:50:29 +0100
From: Georg Bauhaus <bauhaus@futureapps.invalid>
Subject: Re: Both substitute and filter
Message-Id: <m6n3g2$86o$1@dont-email.me>
On 15.12.14 16:33, Rainer Weikusat wrote:
> It is (the fastest). It's, however, code which is hard to maintain
> because the length of the prefix appears in three different places (the
> substrs and the prefix itself), all of which have to be changed in sync
> for the algorithm to work as intended for a different prefix.
By introducing both 'TARGET' and its length as compile time constants,
the algorithm can even be generalized. The above concerns about changes
will be gone, and following the advice about comments, speed and clarity
need not surrender to style whenever performance matters.
------------------------------
Date: Mon, 15 Dec 2014 18:03:01 +0100
From: Georg Bauhaus <bauhaus@futureapps.invalid>
Subject: Re: Both substitute and filter
Message-Id: <m6n47h$bcl$1@dont-email.me>
On 15.12.14 16:11, Rainer Weikusat wrote:
> Steve May <stevem_@nogood.com> writes:
>> For self documentation standpoint, ease of reading, and some specific
>> cases I'm not so sure.
>
> If you want to document something in code, say
>
> my @array; # don't forget that it's emtpy!
>
> use a comment. That's the construct for adding inline
> documentation. Useless code is just useless code.
The balance could be between knowledge of implicit features of Perl
and boring verbosity (even when avoiding redundancy). Consider knowledge
about when an initial assignment to a variable is necessary according
as it was introduced with our, local, or my, or none of these, etc.,
depending possibly on the calling/eval environment (cf. anonymous subs, do).
Like there can be an overdose of idiomatic style, namely when the author
writes for non-Perlists, there can also be an overdose of non-Perl style.
Maybe
my @array; # = ();
alone provides too little context to be deciding a debate about redundancy.
------------------------------
Date: Mon, 15 Dec 2014 17:15:25 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Both substitute and filter
Message-Id: <87bnn4eswi.fsf@doppelsaurus.mobileactivedefense.com>
Georg Bauhaus <bauhaus@futureapps.invalid> writes:
> On 15.12.14 16:33, Rainer Weikusat wrote:
>> It is (the fastest). It's, however, code which is hard to maintain
>> because the length of the prefix appears in three different places (the
>> substrs and the prefix itself), all of which have to be changed in sync
>> for the algorithm to work as intended for a different prefix.
>
> By introducing both 'TARGET' and its length as compile time constants,
> the algorithm can even be generalized. The above concerns about changes
> will be gone, and following the advice about comments, speed and clarity
> need not surrender to style whenever performance matters.
With 'speed and clarity' defined as "looks like something I'm used to
from other programming languages" and 'style' defined as "feature said
other programming language didn't have and I object to because of this".
But "since I'm in Rome, the Romans better start behaving the like the
inhabitants of Klein-Hintertupfingen-Seidelbach-Windenhausen because
that's where I came from" isn't very sensible: Regexes are a central
feature in Perl, hence, using them liberally makes good Perl and 'speed
hacks' ought to be restricted to places where they're demonstrably
necessary.
------------------------------
Date: Mon, 15 Dec 2014 17:45:18 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Both substitute and filter
Message-Id: <87388gerip.fsf@doppelsaurus.mobileactivedefense.com>
Georg Bauhaus <bauhaus@futureapps.invalid> writes:
> On 15.12.14 16:11, Rainer Weikusat wrote:
>> Steve May <stevem_@nogood.com> writes:
>
>>> For self documentation standpoint, ease of reading, and some specific
>>> cases I'm not so sure.
>>
>> If you want to document something in code, say
>>
>> my @array; # don't forget that it's emtpy!
>>
>> use a comment. That's the construct for adding inline
>> documentation. Useless code is just useless code.
>
> The balance could be between knowledge of implicit features of Perl
> and boring verbosity (even when avoiding redundancy). Consider knowledge
> about when an initial assignment to a variable is necessary according
> as it was introduced with “our”, “local”, or “my”, or none of these, etc.,
> depending possibly on the calling/eval environment (cf. anonymous
> subs, do).
my @array; (or my %hash;)
creates a container object. Because no information has been (or could
have been) provided regarding its contents, it must obviously be an empty
object. Assigning a type-less empty list to a container object (surely a specific
Perl feature(!)) clears the current contents of the container. Since
there are none, the whole construct is just an exercise in obfuscation
which will - at best - confuse people who aren't very familiar with
Perl ("Surely, if it's in the code, it must be good for something!?!").
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 4329
***************************************