[32354] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3621 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Feb 24 06:09:24 2012

Date: Fri, 24 Feb 2012 03:09:09 -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           Fri, 24 Feb 2012     Volume: 11 Number: 3621

Today's topics:
    Re: a question about efficiency <kiuhnm03.4t.yahoo.it>
    Re: a question about efficiency <rweikusat@mssgmbh.com>
    Re: a question about efficiency <kiuhnm03.4t.yahoo.it>
    Re: a question about efficiency <kiuhnm03.4t.yahoo.it>
    Re: a question about efficiency (Greg Bacon)
    Re: Constructing a value beforehand <bhdistortion@gmail.com>
    Re: Constructing a value beforehand (Tim McDaniel)
    Re: Constructing a value beforehand <rweikusat@mssgmbh.com>
    Re: Constructing a value beforehand <ben@morrow.me.uk>
    Re: Constructing a value beforehand <m@rtij.nl.invlalid>
    Re: Constructing a value beforehand <ben@morrow.me.uk>
    Re: Constructing a value beforehand <bhdistortion@gmail.com>
    Re: Constructing a value beforehand <bhdistortion@gmail.com>
    Re: size of an array via a ref to it <rvtol+usenet@xs4all.nl>
    Re: size of an array via a ref to it <ben@morrow.me.uk>
    Re: use file::find to find files modified in last 5 day <justin.1201@purestblue.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Thu, 23 Feb 2012 21:40:53 +0100
From: Kiuhnm <kiuhnm03.4t.yahoo.it>
Subject: Re: a question about efficiency
Message-Id: <4f46a454$0$1388$4fafbaef@reader2.news.tin.it>

On 2/23/2012 17:33, Ted Zlatanov wrote:
> On Wed, 22 Feb 2012 20:51:56 +0100 Kiuhnm<kiuhnm03.4t.yahoo.it>  wrote:
>
> K>  Thanks for the tip. My idea was to read
> K>    Learning Perl,
> K>    Intermediate Perl and
> K>    Mastering Perl,
> K>  but I'll have a look at PP. It seems that the latest edition covers
> K>  Perl 5.14 as well.
>
> I'd put the Perl Cookbook either second or third on that list.  It's a
> very valuable compendium of common problems with good solutions.

Ok!

> Also Unix Power Tools, if you have interest in Unix, is a very good book
> to add to this list.  Perl grew and exists in a Unix ecosystem (Windows
> ports notwithstanding) so learning about the tools and facilities in
> that ecosystem is very useful.

Indeed, I've just found out that I can't use wildcards in command-line 
arguments because Perl expects the shell to expand them, whereas Windows 
expects *the program* to expand them.

Kiuhnm


------------------------------

Date: Thu, 23 Feb 2012 21:15:02 +0000
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: a question about efficiency
Message-Id: <87sji1ntex.fsf@sapphire.mobileactivedefense.com>

gbacon@hiwaay.net (Greg Bacon) writes:
> Rainer Weikusat <rweikusat@mssgmbh.com> wrote:
> : This is, of course, completely wrong :-), because there's always 'an
> : enclosing block' in perl, with 'the current compilation unit' (aka
> : 'file') being the outermost one.
>
> Yes, there's always an outer scope, but that's relevant only when
> the sub needs to close over outer lexicals.

But the fact that there is always a lexical scope implies that
subroutines are never without a lexical environment and that this is
consequently not a distinctive quality in Perl.





------------------------------

Date: Thu, 23 Feb 2012 23:36:23 +0100
From: Kiuhnm <kiuhnm03.4t.yahoo.it>
Subject: Re: a question about efficiency
Message-Id: <4f46bf66$0$1378$4fafbaef@reader2.news.tin.it>

On 2/23/2012 2:08, Ben Morrow wrote:
> Quoth Kiuhnm<kiuhnm03.4t.yahoo.it>:
[...]
>> Just a question. When I run the following code I get different values:
>>     my $x;         # global variable
>
> This isn't a true global, of course, but a file-scoped lexical. True
> package globals (declared with 'our' or 'use vars') are never closed
> over, since they are directly accessible from everywhere.

Now I understand why we write "my" and not something like "var".

>>     say for map { sub { $x } } 1..10000;
>> It seems that Perl uses closures even when there's no real need for
>> them. Or am I missing something?
>
> No, you're not. For efficiency reasons, perl doesn't entirely
> distinguish between this case
>
>      my $x;
>      for (1..2) {
>          sub { $x }
>      }
>
> and this case
>
>      for (1..2) {
>          my $x;
>          sub { $x }
>      }
>
> so it has to create a full closure in both cases. In principle it could
> work out that in the first case $x is in a section of code that will
> only run once, and treat it as a global, but that information isn't
> available at the point where it decides which form of anon sub it needs.

Then we should probably take "sub { $x }" out of the loop (in real-world 
programs, I mean).

> (Incidentally, this issue came up here just the other day in the context
> of 'variable will not stay shared' warnings, which are of course very
> closely related. In that situation perl treats the second case as though
> it were the first, rather than the other way around.)

I'm a little lost here.
Every closure captures the same instance of $x in the first case.
If I write
   say &$_ for map { my $x; sub { ++$x } } (1..10)
and
   my $x;
   say &$_ for map { sub { ++$x } } (1..10)
I expect to get different results.
But I probably miss some context here.

Kiuhnm


------------------------------

Date: Thu, 23 Feb 2012 23:49:06 +0100
From: Kiuhnm <kiuhnm03.4t.yahoo.it>
Subject: Re: a question about efficiency
Message-Id: <4f46c261$0$1378$4fafbaef@reader2.news.tin.it>

On 2/22/2012 4:55, Greg Bacon wrote:
> Kiuhnm<kiuhnm03.4t.yahoo.it>  wrote
>
> : [...]
> : Well, theoretically, a closure doesn't need to "capture" outer
> : variables to be called a closure. But you already know that. I guess
> : it depends on how a community see things.
>
> I'm curious to see examples of this usage. Without closing over the
> lexical environment, what's left is a function pointer.

I wasn't talking about usage. I meant that we might say that every 
function is a closure (but the converse is false) the same way as we 
might say that every number is a matrix. If you know how to multiply two 
matrices you also know how to multiply two numbers.
In Ruby everything is an object. Well, we may design a language where 
everything is a closure. The implementation, to be efficient, would see 
things differently, of course.

Kiuhnm


------------------------------

Date: Thu, 23 Feb 2012 18:12:06 -0600
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: a question about efficiency
Message-Id: <scKdnd1i9rVLSNvSnZ2dnUVZ_uqdnZ2d@posted.hiwaay2>

Rainer Weikusat <rweikusat@mssgmbh.com> wrote:

: Greg Bacon writes:
:
: > Yes, there's always an outer scope, but that's relevant only when
: > the sub needs to close over outer lexicals. [See the implementation
: > of Perl_cv_clone in pad.c. (Important snip restored! -geb)]
: 
: But the fact that there is always a lexical scope implies that
: subroutines are never without a lexical environment and that this is
: consequently not a distinctive quality in Perl.

If a sub has no way of reaching into that lexical environment, then the
distinction is irrelevant. See the implementation of Perl_cv_clone in
pad.c.

Greg
-- 
It would be an absurdity for jurors to be required to accept the judge's
view of the law, against their own opinion, judgment, and conscience.
    -- John Adams


------------------------------

Date: Thu, 23 Feb 2012 11:20:53 -0800 (PST)
From: Xze <bhdistortion@gmail.com>
Subject: Re: Constructing a value beforehand
Message-Id: <fe45c991-2043-4ccf-afb6-a86483c876f4@y10g2000vbn.googlegroups.com>

On Feb 23, 8:18=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth Xze <bhdistort...@gmail.com>:
>
> > Hi everyone,
>
> > I'm trying to implement the 'verbose' mode and got stuck.
> > Here is the requirement:
> > if verbose mode is on then: $just_hash -> {$key} =3D $val1.$val2.$val3
> > if verbose mode is off then: $just_hash -> {$key} =3D =A0$val3
>
> > Now, how do I avoid useless checks 'is verbose on or off' in the loop,
> > having that this is known before entering the loop (please see the
> > code)?
>
> > This is what i currently have
>
> > my %hOpt =3D qw{-v 1 --verbose 1};
> > my $bVerbose =3D 0;
>
> Is this Hungarian notation? Perl variables already have sigils, they
> don't need more prefixes.

The 'b' stands for boolean, a convention used in this particular code
to denote the variable type/value

>
> Also, there's no need for that '=3D 0'. Newly-declared variables are
> initialised to undef, which is a perfecly good false value.
>
> > for (@ARGV) {
> > =A0 =A0 $hOpt{$_} ? $bVerbose =3D 1 : usage ("Unknown option: $_");
> > }
>
> You might want to consider using one of the Getopt modules instead of
> rolling your own.
>

None of the modules will allow using the combination of short and long
forms (e.g. -v and --verbose), AFAIK

> > while(<FH>){
>
> Don't use global bareword filehandles, use filehandles in lexical
> variables. If you'd shown us how you opened the file I could show you
> what I mean; something like
>
> =A0 =A0 open my $FH, "<", "file" or die ...;
>
> > =A0 =A0chomp;
> > =A0 =A0 =A0 my @aflds =3D split/,/;
> > =A0 =A0 =A0 #Verobse on: $just_hash -> {$key} =3D $val1.$val2.$val3;
> > =A0 =A0 =A0 #Verobse off: $just_hash -> {$key} =3D $val3;
> > =A0 =A0 =A0 $bVerbose ? $just_hash -> {$key} =3D ($val1.$val2.$val3) :
> > $just_hash -> {$key} =3D ($val3);
>
> Please post complete code. Where do $val1 &c. come from? Do you just
> mean @aflds[0..3], or are they supposed to come from outside the loop?
>
> > }
>
> > but this code checks the $bVervose at every iteration which is ugly.
>
> It's not bad, actually. If your code really is this simple it may well
> be the best option: all the other choices have overhead, and a simple ?:
> is neither expensive nor unclear.
>
> > I
> > feel that there should be a perl-ish way to accomplish this
> > Is it possible to construct a pattern of the value beforehand, based
> > on $bVerbose?
>
> There are basically two options: duplicate the loop, or use a subref.
> The first is more verbose that what you have:
>
> =A0 =A0 if ($bVerbose) {
> =A0 =A0 =A0 =A0 while (<FH>) {
> =A0 =A0 =A0 =A0 =A0 =A0 ...;
> =A0 =A0 =A0 =A0 =A0 =A0 $just_hash->{$key} =3D $val1.$val2.$val3;
> =A0 =A0 =A0 =A0 }
> =A0 =A0 }
> =A0 =A0 else {
> =A0 =A0 =A0 =A0 while (<FH>) {
> =A0 =A0 =A0 =A0 =A0 =A0 ...;
> =A0 =A0 =A0 =A0 =A0 =A0 $just_hash->{$key} =3D $val3;
> =A0 =A0 =A0 =A0 }
> =A0 =A0 }
>

I'm trying to avoid code duplication as it becomes a headache to
maintain it later, so i'll opt for having only one loop with the
checks inside or without the checks, if a solution can be found

> and probably isn't even faster. The second looks something like this:
>
> =A0 =A0 my $get_val =3D $bVerbose
> =A0 =A0 =A0 =A0 ? sub { $val1.$val2.$val3 }
> =A0 =A0 =A0 =A0 : sub { $val3 };
>
> =A0 =A0 while (<FH>) {
> =A0 =A0 =A0 =A0 ...;
> =A0 =A0 =A0 =A0 $just_hash->{$key} =3D $get_val->();
> =A0 =A0 }
>
> unless $valN actually live inside the loop. In that case you have to
> pass them in to the subref, so you end up with
>
> =A0 =A0 my $get_val =3D $bVerbose
> =A0 =A0 =A0 =A0 ? sub { join "", @_ }
> =A0 =A0 =A0 =A0 : sub { $_[2] };
>
> =A0 =A0 while (<FH>) {
> =A0 =A0 =A0 =A0 ...;
> =A0 =A0 =A0 =A0 $just_hash->{$key} =3D $get_val->($val1, $val2, $val3);
> =A0 =A0 }
>

It's nice, but the logic didn't change. You're still performing the
check of $bVerbose at every iteration.

> Ben

Below is the full code:

sub populate_hash {
   my $just_file =3D shift;
   my $just_hash =3D shift;
   open(FH,$just_file) or die "Error reading file $just_file: $!";
   while(<FH>){
      chomp;
        my @aflds =3D split/,/;
        ($val1, $val2, $val3) =3D @aflds[5,6,9];
        #Verobse on: $just_hash -> {$key} =3D $val1.$val2.$val3;
        #Verobse off: $just_hash -> {$key} =3D $val3;
        #$bVerbose ? $just_hash -> {$key} =3D ($val1.$val2.$val3) :
$just_hash -> {$key} =3D ($val3);
        $just_hash -> {$key} =3D ($val1.$val2) x!! $bVerbose . $val3;
   }
   close (FH);
}

The ternary operator could be replaced by the following, which is what
i did:
$just_hash -> {$key} =3D ($val1.$val2) x!! $bVerbose . $val3;

In fact, i don't have any big issue with the code, it works fine.
It's just my personal curiosity..

May be it is possible to eval in a tricky way like this:

# define the pattern before loop
my $val =3D ($val1.$val2) x!! $bVerbose . $val3;
# and later in the loop:
 ...
$just_hash -> {$key} =3D eval $val; #?? <make the assignment happen
again, but with the desired pattern>


Thanks a lot to everyone for spending your time!!
Xze


------------------------------

Date: Thu, 23 Feb 2012 20:01:15 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: Constructing a value beforehand
Message-Id: <ji65ub$6sp$1@reader1.panix.com>

In article <fe45c991-2043-4ccf-afb6-a86483c876f4@y10g2000vbn.googlegroups.com>,
Xze  <bhdistortion@gmail.com> wrote:
>On Feb 23, 8:18 pm, Ben Morrow <b...@morrow.me.uk> wrote:
>> Quoth Xze <bhdistort...@gmail.com>:
>> > my $bVerbose = 0;
 ...
>> Also, there's no need for that '= 0'. Newly-declared variables are
>> initialised to undef, which is a perfecly good false value.

use of undef in a boolean context doesn't provoke any warnings.
However, personally I dislike implicit initialization and avoid undef
(except when I need a value distinguishable from all valid values).
So I tend to initialize to an appropriate value, like 0.

>None of the modules will allow using the combination of short and long
>forms (e.g. -v and --verbose), AFAIK

http://perldoc.perl.org/Getopt/Long.html

It allows alternatives, like
    GetOptions ('length|height=f' => \$length);
and by default it allows automatic abbreviation to a unique prefix,
which may be one letter.  I haven't tried it, but I'd try
noauto_abbrev to get rid of that automatic shortening, and list
explicit one-character synonyms as needed.

>The ternary operator could be replaced by the following, which is what
>i did:
>$just_hash -> {$key} = ($val1.$val2) x!! $bVerbose . $val3;

*blink*   *blink*
Oooookay, that works, but I'd suggest ($verbose && ($val1.$val2)) as
someone else suggested.

>May be it is possible to eval in a tricky way like this:
>
># define the pattern before loop
>my $val = ($val1.$val2) x!! $bVerbose . $val3;
># and later in the loop:
>...
>$just_hash -> {$key} = eval $val; #?? <make the assignment happen
>again, but with the desired pattern>

You'd need to have
    my $val = 'some code here';
to set it up for that eval.  It is ridiculously inefficient to call
the Perl parser on each iteration.

If you want to generate code on the fly, I think it's standard to have
the code be a sub, like
    my $code = 'sub { the code you want }';
    my $code_ref = eval $code;
    loop {
        ...
        ... $code_ref->(args)
That way the overhead for the eval is paid only once.
But there's no need for an eval in the situation you posit.

-- 
Tim McDaniel, tmcd@panix.com


------------------------------

Date: Thu, 23 Feb 2012 20:29:19 +0000
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Constructing a value beforehand
Message-Id: <87wr7dnvj4.fsf@sapphire.mobileactivedefense.com>

Xze <bhdistortion@gmail.com> writes:

[...]

>>     my $get_val = $bVerbose
>>         ? sub { $val1.$val2.$val3 }
>>         : sub { $val3 };
>>
>>     while (<FH>) {
>>         ...;
>>         $just_hash->{$key} = $get_val->();
>>     }
>>
>> unless $valN actually live inside the loop. In that case you have to
>> pass them in to the subref, so you end up with
>>
>>     my $get_val = $bVerbose
>>         ? sub { join "", @_ }
>>         : sub { $_[2] };
>>
>>     while (<FH>) {
>>         ...;
>>         $just_hash->{$key} = $get_val->($val1, $val2, $val3);
>>     }
>>
>
> It's nice, but the logic didn't change. You're still performing the
> check of $bVerbose at every iteration.

This is wrong: The value of $get_val will be a code reference to a
subroutine which performs either the verbose or the non-verbose
operation. Which one it will be is determined at the time of the
assignment. Later on, the code in the loop-body just invokes whatever
subroutine reference was assigned to get_val.

This is actually a generally useful technique for eliminating
comparisons whose outcome is already known at some 'point of call'
(I refuse to use the term 'pattern' for that):
Instead of using switching statement a la

	if (...) do_w();
        elsif (...) do_x();
        elsif (...) do_y();
        else  do_z();

a variable $operation is introduced and the code in the loop just does
$operation->(). The code dealing with the state changes sets the
value of $operation to something representing the code which is to be
executed in the current state, as opposed to setting some state
variable and selecting one of several code alternatives based on the
current value of that whenever 'the operation associated with the
current state' needs to be performed.


------------------------------

Date: Thu, 23 Feb 2012 22:14:56 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Constructing a value beforehand
Message-Id: <0cqi19-9ek1.ln1@anubis.morrow.me.uk>


Quoth Xze <bhdistortion@gmail.com>:
> On Feb 23, 8:18 pm, Ben Morrow <b...@morrow.me.uk> wrote:
> > Quoth Xze <bhdistort...@gmail.com>:
> >
> > > my $bVerbose = 0;
> >
> > Is this Hungarian notation? Perl variables already have sigils, they
> > don't need more prefixes.
> 
> The 'b' stands for boolean, a convention used in this particular code
> to denote the variable type/value

Yes. That's called 'Hungarian notation'. Particularly in the case of 'h'
for hash and 'a' for array, it is completely redundant with the @ or %
already on the variable. I realise Perl doesn't have a dedicated sigil
for 'boolean', but it's still not useful

> > > for (@ARGV) {
> > >     $hOpt{$_} ? $bVerbose = 1 : usage ("Unknown option: $_");
> > > }
> >
> > You might want to consider using one of the Getopt modules instead of
> > rolling your own.
> >
> 
> None of the modules will allow using the combination of short and long
> forms (e.g. -v and --verbose), AFAIK

Getopt::Long will, among others.

> I'm trying to avoid code duplication as it becomes a headache to
> maintain it later, so i'll opt for having only one loop with the
> checks inside or without the checks, if a solution can be found

Yup, that's sensible.

> >     my $get_val = $bVerbose
> >         ? sub { join "", @_ }
> >         : sub { $_[2] };
> >
> >     while (<FH>) {
> >         ...;
> >         $just_hash->{$key} = $get_val->($val1, $val2, $val3);
> >     }
> >
> 
> It's nice, but the logic didn't change. You're still performing the
> check of $bVerbose at every iteration.

No I'm not. $bVerbose is checked once, outside the loop, and then
whichever subref we picked is called inside. Maybe it would be clearer
if I wrote it like this?

    my $get_val;
    if ($bVerbose) {
        $get_val = sub { join "", @_ };
    }
    else {
        $get_val = sub { $_[2] };
    }

    while (...) {
        ... = $get_val->(...);
    }

> The ternary operator could be replaced by the following, which is what
> i did:
> $just_hash -> {$key} = ($val1.$val2) x!! $bVerbose . $val3;

Oh, yuck. That's just *nasty*. In any case, it's more work than the
ternary.

> In fact, i don't have any big issue with the code, it works fine.
> It's just my personal curiosity..
> 
> May be it is possible to eval in a tricky way like this:
> 
> # define the pattern before loop
> my $val = ($val1.$val2) x!! $bVerbose . $val3;
> # and later in the loop:
> ...
> $just_hash -> {$key} = eval $val; #?? <make the assignment happen
> again, but with the desired pattern>

This is pretty-much exactly what I was doing with the anon subs above,
but without needing to resort to 'eval'. I could have written it like
this:

    my $get_val = $bVerbose
        ? '$val1.$val2.$val3'   # note single quotes!
        : '$val3';

    while (...) {
        ... = eval $get_val;
    }

which works exactly the same, except that the anon subs are a whole lot
cleaner and safer.

Ben



------------------------------

Date: Thu, 23 Feb 2012 23:13:59 +0100
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: Constructing a value beforehand
Message-Id: <7aqi19-52v.ln1@news.rtij.nl>

On Thu, 23 Feb 2012 18:42:15 +0000, Ben Morrow wrote:

> (I have always quite wanted an 'else' which would turn 'and' and 'or'
> into a ternary construction...)

If you like'm ugly and unreadable:

(this() and (that(),1) or so();

:-)
M4


------------------------------

Date: Fri, 24 Feb 2012 03:44:13 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Constructing a value beforehand
Message-Id: <dldj19-ecr1.ln1@anubis.morrow.me.uk>


Quoth Martijn Lievaart <m@rtij.nl.invlalid>:
> On Thu, 23 Feb 2012 18:42:15 +0000, Ben Morrow wrote:
> 
> > (I have always quite wanted an 'else' which would turn 'and' and 'or'
> > into a ternary construction...)
> 
> If you like'm ugly and unreadable:
> 
> (this() and (that(),1) or so();

The parens aren't actually necessary: 'and' binds tighter than 'or'. I
use this construct in shell on occasion (it even reads half-decently),
but I avoid it in Perl because of the edge case where that() returns
false. (I realise you've handled it there, but I'm certainly not going
to write *that* in practice :) ).

Ben



------------------------------

Date: Fri, 24 Feb 2012 01:16:31 -0800 (PST)
From: Xze <bhdistortion@gmail.com>
Subject: Re: Constructing a value beforehand
Message-Id: <226995e0-2f7f-41d3-88a3-013ac1753ac3@y10g2000vbn.googlegroups.com>

On Feb 24, 12:14=A0am, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth Xze <bhdistort...@gmail.com>:
>
> > On Feb 23, 8:18=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> > > Quoth Xze <bhdistort...@gmail.com>:
>
> > > > my $bVerbose =3D 0;
>
> > > Is this Hungarian notation? Perl variables already have sigils, they
> > > don't need more prefixes.
>
> > The 'b' stands for boolean, a convention used in this particular code
> > to denote the variable type/value
>
> Yes. That's called 'Hungarian notation'. Particularly in the case of 'h'
> for hash and 'a' for array, it is completely redundant with the @ or %
> already on the variable. I realise Perl doesn't have a dedicated sigil
> for 'boolean', but it's still not useful
>
> > > > for (@ARGV) {
> > > > =A0 =A0 $hOpt{$_} ? $bVerbose =3D 1 : usage ("Unknown option: $_");
> > > > }
>
> > > You might want to consider using one of the Getopt modules instead of
> > > rolling your own.
>
> > None of the modules will allow using the combination of short and long
> > forms (e.g. -v and --verbose), AFAIK
>
> Getopt::Long will, among others.
>
> > I'm trying to avoid code duplication as it becomes a headache to
> > maintain it later, so i'll opt for having only one loop with the
> > checks inside or without the checks, if a solution can be found
>
> Yup, that's sensible.
>
> > > =A0 =A0 my $get_val =3D $bVerbose
> > > =A0 =A0 =A0 =A0 ? sub { join "", @_ }
> > > =A0 =A0 =A0 =A0 : sub { $_[2] };
>
> > > =A0 =A0 while (<FH>) {
> > > =A0 =A0 =A0 =A0 ...;
> > > =A0 =A0 =A0 =A0 $just_hash->{$key} =3D $get_val->($val1, $val2, $val3=
);
> > > =A0 =A0 }
>
> > It's nice, but the logic didn't change. You're still performing the
> > check of $bVerbose at every iteration.
>
> No I'm not. $bVerbose is checked once, outside the loop, and then
> whichever subref we picked is called inside. Maybe it would be clearer
> if I wrote it like this?
>
> =A0 =A0 my $get_val;
> =A0 =A0 if ($bVerbose) {
> =A0 =A0 =A0 =A0 $get_val =3D sub { join "", @_ };
> =A0 =A0 }
> =A0 =A0 else {
> =A0 =A0 =A0 =A0 $get_val =3D sub { $_[2] };
> =A0 =A0 }
>
> =A0 =A0 while (...) {
> =A0 =A0 =A0 =A0 ... =3D $get_val->(...);
> =A0 =A0 }
>
> > The ternary operator could be replaced by the following, which is what
> > i did:
> > $just_hash -> {$key} =3D ($val1.$val2) x!! $bVerbose . $val3;
>
> Oh, yuck. That's just *nasty*. In any case, it's more work than the
> ternary.
>
> > In fact, i don't have any big issue with the code, it works fine.
> > It's just my personal curiosity..
>
> > May be it is possible to eval in a tricky way like this:
>
> > # define the pattern before loop
> > my $val =3D ($val1.$val2) x!! $bVerbose . $val3;
> > # and later in the loop:
> > ...
> > $just_hash -> {$key} =3D eval $val; #?? <make the assignment happen
> > again, but with the desired pattern>
>
> This is pretty-much exactly what I was doing with the anon subs above,
> but without needing to resort to 'eval'. I could have written it like
> this:
>
> =A0 =A0 my $get_val =3D $bVerbose
> =A0 =A0 =A0 =A0 ? '$val1.$val2.$val3' =A0 # note single quotes!
> =A0 =A0 =A0 =A0 : '$val3';
>
> =A0 =A0 while (...) {
> =A0 =A0 =A0 =A0 ... =3D eval $get_val;
> =A0 =A0 }
>
> which works exactly the same, except that the anon subs are a whole lot
> cleaner and safer.
>
> Ben

Thanks for the detailed explanations!

Indeed the following snippet:
my $get_val =3D $bVerbose
    ? sub { join "", @_ }
    : sub { $_[2] };

does what i was looking for, somehow i missed that in your first post.
But it is not as fast as ternary operator inside the loop :)

Thanks everyone for spending your time!
Cheers,
Xze



------------------------------

Date: Fri, 24 Feb 2012 01:19:29 -0800 (PST)
From: Xze <bhdistortion@gmail.com>
Subject: Re: Constructing a value beforehand
Message-Id: <08d37178-b55c-4812-827e-8e6eb248fa65@gr6g2000vbb.googlegroups.com>

On Feb 24, 12:14=A0am, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth Xze <bhdistort...@gmail.com>:
>
> > On Feb 23, 8:18=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> > > Quoth Xze <bhdistort...@gmail.com>:
>
> > > > my $bVerbose =3D 0;
>
> > > Is this Hungarian notation? Perl variables already have sigils, they
> > > don't need more prefixes.
>
> > The 'b' stands for boolean, a convention used in this particular code
> > to denote the variable type/value
>
> Yes. That's called 'Hungarian notation'. Particularly in the case of 'h'
> for hash and 'a' for array, it is completely redundant with the @ or %
> already on the variable. I realise Perl doesn't have a dedicated sigil
> for 'boolean', but it's still not useful
>
> > > > for (@ARGV) {
> > > > =A0 =A0 $hOpt{$_} ? $bVerbose =3D 1 : usage ("Unknown option: $_");
> > > > }
>
> > > You might want to consider using one of the Getopt modules instead of
> > > rolling your own.
>
> > None of the modules will allow using the combination of short and long
> > forms (e.g. -v and --verbose), AFAIK
>
> Getopt::Long will, among others.
>
> > I'm trying to avoid code duplication as it becomes a headache to
> > maintain it later, so i'll opt for having only one loop with the
> > checks inside or without the checks, if a solution can be found
>
> Yup, that's sensible.
>
> > > =A0 =A0 my $get_val =3D $bVerbose
> > > =A0 =A0 =A0 =A0 ? sub { join "", @_ }
> > > =A0 =A0 =A0 =A0 : sub { $_[2] };
>
> > > =A0 =A0 while (<FH>) {
> > > =A0 =A0 =A0 =A0 ...;
> > > =A0 =A0 =A0 =A0 $just_hash->{$key} =3D $get_val->($val1, $val2, $val3=
);
> > > =A0 =A0 }
>
> > It's nice, but the logic didn't change. You're still performing the
> > check of $bVerbose at every iteration.
>
> No I'm not. $bVerbose is checked once, outside the loop, and then
> whichever subref we picked is called inside. Maybe it would be clearer
> if I wrote it like this?
>
> =A0 =A0 my $get_val;
> =A0 =A0 if ($bVerbose) {
> =A0 =A0 =A0 =A0 $get_val =3D sub { join "", @_ };
> =A0 =A0 }
> =A0 =A0 else {
> =A0 =A0 =A0 =A0 $get_val =3D sub { $_[2] };
> =A0 =A0 }
>
> =A0 =A0 while (...) {
> =A0 =A0 =A0 =A0 ... =3D $get_val->(...);
> =A0 =A0 }
>
> > The ternary operator could be replaced by the following, which is what
> > i did:
> > $just_hash -> {$key} =3D ($val1.$val2) x!! $bVerbose . $val3;
>
> Oh, yuck. That's just *nasty*. In any case, it's more work than the
> ternary.
>
> > In fact, i don't have any big issue with the code, it works fine.
> > It's just my personal curiosity..
>
> > May be it is possible to eval in a tricky way like this:
>
> > # define the pattern before loop
> > my $val =3D ($val1.$val2) x!! $bVerbose . $val3;
> > # and later in the loop:
> > ...
> > $just_hash -> {$key} =3D eval $val; #?? <make the assignment happen
> > again, but with the desired pattern>
>
> This is pretty-much exactly what I was doing with the anon subs above,
> but without needing to resort to 'eval'. I could have written it like
> this:
>
> =A0 =A0 my $get_val =3D $bVerbose
> =A0 =A0 =A0 =A0 ? '$val1.$val2.$val3' =A0 # note single quotes!
> =A0 =A0 =A0 =A0 : '$val3';
>
> =A0 =A0 while (...) {
> =A0 =A0 =A0 =A0 ... =3D eval $get_val;
> =A0 =A0 }
>
> which works exactly the same, except that the anon subs are a whole lot
> cleaner and safer.
>
> Ben

Thanks for detailed explanations!
Indeed, the following snippet:

my $get_val =3D $bVerbose
    ? sub { join "", @_ }
    : sub { $_[2] };

does what i was looking for, but ternary operator turned to be faster
anyway :)

Thank you for spending your time
Cheers,
Xze


------------------------------

Date: Thu, 23 Feb 2012 21:35:16 +0100
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: size of an array via a ref to it
Message-Id: <4f46a304$0$6923$e4fe514c@news2.news.xs4all.nl>

On 2012-02-23 14:23, John W. Krahn wrote:

> s/^\s+//, s/\s+$// for @arr2;

I tend to write that as:

   s/\s+\z//, s/\A\s+// for @arr2;

but I don't think it matters much.

-- 
Ruud


------------------------------

Date: Thu, 23 Feb 2012 22:20:17 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: size of an array via a ref to it
Message-Id: <1mqi19-9ek1.ln1@anubis.morrow.me.uk>


Quoth "Dr.Ruud" <rvtol+usenet@xs4all.nl>:
> On 2012-02-23 14:23, John W. Krahn wrote:
> 
> > s/^\s+//, s/\s+$// for @arr2;
> 
> I tend to write that as:
> 
>    s/\s+\z//, s/\A\s+// for @arr2;
> 
> but I don't think it matters much.

In this specific case, since /\s/ matches "\n", it makes no difference.
In general /$/ should in principle be avoided unless you know your
string doesn't end in a newline, though I must admit I rarely bother.

/\A/ is identical to /^/ if you're not using /m.

Ben



------------------------------

Date: Fri, 24 Feb 2012 10:51:29 +0000
From: Justin C <justin.1201@purestblue.com>
Subject: Re: use file::find to find files modified in last 5 days
Message-Id: <hm6k19-07v.ln1@zem.masonsmusic.co.uk>

On 2012-02-22, Jim Gibson <jimsgibson@gmail.com> wrote:
> In article <ji35u3$os4$3@reader1.panix.com>, Tim McDaniel
> <tmcd@panix.com> wrote:
>
>> In article <s7ef19-lks.ln1@zem.masonsmusic.co.uk>,
>> Justin C  <justin.1201@purestblue.com> wrote:
>> >I just don't know how people remember all of those.
>> 
>> There's a lot I don't remember about Perl.  I try to remember that
>> there's a way to do such-and-so and a vague idea of where to look it
>> up.  For "-M", I tried "man perlop", but it's not there -- it's in
>> "man perlfunc", which makes more sense come to think of it.
>
> 'perldoc -f -x' lists all of the file test operators. That is what I
> can remember.

That's useful, I'll try and remember that.

   Justin.

-- 
Justin C, by the sea.


------------------------------

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 3621
***************************************


home help back first fref pref prev next nref lref last post