[32850] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4116 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jan 15 09:09:31 2014

Date: Wed, 15 Jan 2014 06:09:03 -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           Wed, 15 Jan 2014     Volume: 11 Number: 4116

Today's topics:
    Re: Logical or assignment and array <rweikusat@mobileactivedefense.com>
    Re: Logical or assignment and array <rweikusat@mobileactivedefense.com>
    Re: Logical or assignment and array (Tim McDaniel)
    Re: Logical or assignment and array (Tim McDaniel)
    Re: Logical or assignment and array <rweikusat@mobileactivedefense.com>
    Re: purge line <rweikusat@mobileactivedefense.com>
    Re: purge line <gravitalsun@foo.com>
    Re: purge line <rweikusat@mobileactivedefense.com>
    Re: purge line <gravitalsun@foo.com>
    Re: purge line <rweikusat@mobileactivedefense.com>
    Re: purge line <gravitalsun@hotmail.foo>
    Re: purge line <rweikusat@mobileactivedefense.com>
    Re: purge line <ben@morrow.me.uk>
    Re: purge line <gravitalsun@hotmail.foo>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 14 Jan 2014 14:40:39 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Logical or assignment and array
Message-Id: <8761pm4oew.fsf@sable.mobileactivedefense.com>

Ben Morrow <ben@morrow.me.uk> writes:
> Quoth Adrien BARREAU <adrien.barreau@live.fr>:
>> Here is a piece of code that troubles me:
>> 
>> =====
>> $ perl -e 'my @a; @a = @a || 1..3;'
>> $ perl -e 'my @a; @a ||= 1..3;'
>> Can't modify private array in logical or assignment (||=) at -e line 1, 
>> near "3;"
>> Execution of -e aborted due to compilation errors.
>> =====
>> 
>> It thought the "a operator= b" notation really was "a = a operator b", 
>> but it seems it is wrong.
>> 
>> Does anyone have some information about this?
>
> In the statement
>
>     @a = @a || 1..3;
>
> the first '@a' is in list context and the second is in scalar context.

I doubt that this expression means what the OP thinks it means, cf

perl -e '@b = (4,5,6); @a = @b || 1 .. 3; print @a;'

This prints 3 because evaluating @b in scalar context returns the size
of the array.

> This means they can't be combined into a single argument to the ||=
> operator. 
>
> I think this is because if the LHS of the assignment were a list it
> would be impossible to evaluate that list in both contexts without
> evaluating it twice (which ||= promises not to do).

The @a in @a ||= parses as OP_RV2AV and the Perl_mod routine in op.c
(5.10.1) which is used to

	Propagate lvalue ("modifiable") context to an op and its
	children.  'type' represents the context type, roughly based on
	the type of op that would do the modifying, although local() is
	represented by OP_NULL.  It's responsible for detecting things
	that can't be modified,

type is OP_ORASSIGN which is classifed as 'scalar mod type' by the
function of the same name and Perl_mod rejects the assignment because of
that.

Logically, one could regard this as '@a evaluated in scalar context', as
required by the || part of ||=, yields the size of a and since this is
an integer constant, it can't be assigned to.


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

Date: Tue, 14 Jan 2014 14:49:08 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Logical or assignment and array
Message-Id: <87sisq39gb.fsf@sable.mobileactivedefense.com>

Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:

[...]

> The @a in @a ||= parses as OP_RV2AV and the Perl_mod routine in op.c
> (5.10.1) which is used to
>
> 	Propagate lvalue ("modifiable") context to an op and its
> 	children.  'type' represents the context type, roughly based on
> 	the type of op that would do the modifying, although local() is
> 	represented by OP_NULL.  It's responsible for detecting things
> 	that can't be modified,
>
> type is OP_ORASSIGN which is classifed as 'scalar mod type' by the
> function of the same name and Perl_mod rejects the assignment because of
> that.

Remotely Enlish variant: "The Perl_mod routine which is used to [...]
rejects the assignment because the type is [...] which is [...]"


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

Date: Tue, 14 Jan 2014 21:39:14 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: Logical or assignment and array
Message-Id: <lb4aq2$83l$1@reader1.panix.com>

In article <epnfqa-i4q1.ln1@anubis.morrow.me.uk>,
Ben Morrow  <ben@morrow.me.uk> wrote:
>
>Quoth Adrien BARREAU <adrien.barreau@live.fr>:
>>
>> Here is a piece of code that troubles me:
>> 
>> =====
>> $ perl -e 'my @a; @a = @a || 1..3;'
>> $ perl -e 'my @a; @a ||= 1..3;'
>> Can't modify private array in logical or assignment (||=) at -e line 1, 
>> near "3;"
>> Execution of -e aborted due to compilation errors.
>> =====
>> 
>> It thought the "a operator= b" notation really was "a = a operator b", 
>> but it seems it is wrong.
>> 
>> Does anyone have some information about this?
>
>In the statement
>
>    @a = @a || 1..3;
>
>the first '@a' is in list context and the second is in scalar
>context.

The first time, I misread what you actually wrote.  The second
occurrence of @a in the whole line (the first operand of ||) is
evaluated in a scalar context.  1..3 (the second operand of ||) as
stated in "man perlop" should be evaluated in a list context:

   C-style Logical Or

          Binary "||" performs a short-circuit logical OR operation.
          That is, if the left operand is true, the right operand is
          not even evaluated.  Scalar or list context propagates down
          to the right operand if it is evaluated.
                 
-- 
Tim McDaniel, tmcd@panix.com


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

Date: Tue, 14 Jan 2014 21:51:18 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: Logical or assignment and array
Message-Id: <lb4bgm$f72$1@reader1.panix.com>

In article <8761pm4oew.fsf@sable.mobileactivedefense.com>,
Rainer Weikusat  <rweikusat@mobileactivedefense.com> wrote:
>Ben Morrow <ben@morrow.me.uk> writes:
>> Quoth Adrien BARREAU <adrien.barreau@live.fr>:
>>> Here is a piece of code that troubles me:
>>> 
>>> =====
>>> $ perl -e 'my @a; @a = @a || 1..3;'
>>> $ perl -e 'my @a; @a ||= 1..3;'
>>> Can't modify private array in logical or assignment (||=) at -e line 1, 
>>> near "3;"
>>> Execution of -e aborted due to compilation errors.
>>> =====
>>> 
>>> It thought the "a operator= b" notation really was "a = a operator b", 
>>> but it seems it is wrong.
>>> 
>>> Does anyone have some information about this?
>>
>> In the statement
>>
>>     @a = @a || 1..3;
>>
>> the first '@a' is in list context and the second is in scalar context.
>
>I doubt that this expression means what the OP thinks it means, cf
>
>perl -e '@b = (4,5,6); @a = @b || 1 .. 3; print @a;'
>
>This prints 3 because evaluating @b in scalar context returns the size
>of the array.

that is, trying to do this

    @a = @a || 1..3;

to mean "if @a has no elements, supply defaults", only works if @a has
no elements.

I've certainly used the scalar version,

    $arg ||= 'default';

A pity that it doesn't work for arrays.  Is there a nice concise way
to do it for arrays?

    @a = 1..3 if !@a;

    @a = @a ? @a : 1..3;

Neither of those is quite as nice.

-- 
Tim McDaniel, tmcd@panix.com


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

Date: Tue, 14 Jan 2014 22:09:56 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Logical or assignment and array
Message-Id: <87y52i1ah7.fsf@sable.mobileactivedefense.com>

tmcd@panix.com (Tim McDaniel) writes:
> Rainer Weikusat  <rweikusat@mobileactivedefense.com> wrote:

[...]

>>perl -e '@b = (4,5,6); @a = @b || 1 .. 3; print @a;'
>>
>>This prints 3 because evaluating @b in scalar context returns the size
>>of the array.
>
> that is, trying to do this
>
>     @a = @a || 1..3;
>
> to mean "if @a has no elements, supply defaults", only works if @a has
> no elements.
>
> I've certainly used the scalar version,
>
>     $arg ||= 'default';
>
> A pity that it doesn't work for arrays.  Is there a nice concise way
> to do it for arrays?
>
>     @a = 1..3 if !@a;
>
>     @a = @a ? @a : 1..3;

@a or @a = 1 .. 3

or

!@a and @a = 1 .. 3

would be two other options.


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

Date: Tue, 14 Jan 2014 14:46:04 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: purge line
Message-Id: <871u0a4o5v.fsf@sable.mobileactivedefense.com>

George Mpouras <gravitalsun@foo.com> writes:
> Στις 14/1/2014 13:32, ο/η Peter Makholm έγραψε:
>> George Mpouras <gravitalsun@foo.com> writes:
>>
>>>> If you need to truncate the file you have to do it explicitely. The
>>>> function you are looking for is called 'truncate'.
>>>
>>> not truncate , but read/update
>>
>> What happens if you add a 'truncate FILE, 0;' right before printing the
>> second number?

[...]

> i tried , do not work seem to work at multiple iterations

--------
open($fh, '>', '/tmp/out') or die("$!");

while ($data = <STDIN>) {
    seek($fh, 0, 0);
    truncate($fh, 0);
    print $fh ($data);
    
    system('cat', '/tmp/out');
}
--------

works for me (in the sense that the output of cat is the last line
written to the file, even if it is shorter than the second-to-last line.

    



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

Date: Tue, 14 Jan 2014 17:15:25 +0200
From: George Mpouras <gravitalsun@foo.com>
Subject: Re: purge line
Message-Id: <lb3k96$bc3$1@news.ntua.gr>

Στις 14/1/2014 16:46, ο/η Rainer Weikusat έγραψε:
> open($fh, '>', '/tmp/out') or die("$!");
>
> while ($data = <STDIN>) {
>      seek($fh, 0, 0);
>      truncate($fh, 0);
>      print $fh ($data);
>
>      system('cat', '/tmp/out');
> }



this works but for reading the counter you need the shell command

	system('cat', '/tmp/out');







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

Date: Tue, 14 Jan 2014 15:17:46 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: purge line
Message-Id: <87ob3e384l.fsf@sable.mobileactivedefense.com>

George Mpouras <gravitalsun@foo.com> writes:
> Στις 14/1/2014 16:46, ο/η Rainer Weikusat έγραψε:
>> open($fh, '>', '/tmp/out') or die("$!");
>>
>> while ($data = <STDIN>) {
>>      seek($fh, 0, 0);
>>      truncate($fh, 0);
>>      print $fh ($data);
>>
>>      system('cat', '/tmp/out');
>> }
>
>
>
> this works but for reading the counter you need the shell command
>
> 	system('cat', '/tmp/out');

Not really. That's just what I used because it was the easiest way to
display content of the file.



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

Date: Tue, 14 Jan 2014 17:50:47 +0200
From: George Mpouras <gravitalsun@foo.com>
Subject: Re: purge line
Message-Id: <lb3mbg$gdj$1@news.ntua.gr>

Στις 14/1/2014 17:17, ο/η Rainer Weikusat έγραψε:
> George Mpouras <gravitalsun@foo.com> writes:
>> Στις 14/1/2014 16:46, ο/η Rainer Weikusat έγραψε:
>>> open($fh, '>', '/tmp/out') or die("$!");
>>>
>>> while ($data = <STDIN>) {
>>>       seek($fh, 0, 0);
>>>       truncate($fh, 0);
>>>       print $fh ($data);
>>>
>>>       system('cat', '/tmp/out');
>>> }
>>
>>
>>
>> this works but for reading the counter you need the shell command
>>
>> 	system('cat', '/tmp/out');
>
> Not really. That's just what I used because it was the easiest way to
> display content of the file.
>


really


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

Date: Tue, 14 Jan 2014 16:02:09 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: purge line
Message-Id: <87k3e2362m.fsf@sable.mobileactivedefense.com>

George Mpouras <gravitalsun@foo.com> writes:
> Στις 14/1/2014 17:17, ο/η Rainer Weikusat έγραψε:
>> George Mpouras <gravitalsun@foo.com> writes:
>>> Στις 14/1/2014 16:46, ο/η Rainer Weikusat έγραψε:
>>>> open($fh, '>', '/tmp/out') or die("$!");
>>>>
>>>> while ($data = <STDIN>) {
>>>>       seek($fh, 0, 0);
>>>>       truncate($fh, 0);
>>>>       print $fh ($data);
>>>>
>>>>       system('cat', '/tmp/out');
>>>> }
>>>
>>>
>>>
>>> this works but for reading the counter you need the shell command
>>>
>>> 	system('cat', '/tmp/out');
>>
>> Not really. That's just what I used because it was the easiest way to
>> display content of the file.
>>
> really

------
open($fh, '+>', '/tmp/out') or die("$!");

while ($data = <STDIN>) {
    seek($fh, 0, 0);
    truncate($fh, 0);
    print $fh ($data);

    print("content of out\n");
    seek($fh, 0, 0);
    printf("\t%s", $_) for <$fh>;
}
------



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

Date: Tue, 14 Jan 2014 19:29:12 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: purge line
Message-Id: <lb3s5o$v3i$1@news.ntua.gr>

Στις 14/1/2014 18:02, ο/η Rainer Weikusat έγραψε:
> open($fh, '+>', '/tmp/out') or die("$!");
>
> while ($data = <STDIN>) {
>      seek($fh, 0, 0);
>      truncate($fh, 0);
>      print $fh ($data);
>
>      print("content of out\n");
>      seek($fh, 0, 0);
>      printf("\t%s", $_) for <$fh>;
> }
> ------

$fh can not read at start the stored value ...


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

Date: Tue, 14 Jan 2014 17:46:12 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: purge line
Message-Id: <87fvoq3197.fsf@sable.mobileactivedefense.com>

George Mpouras <gravitalsun@hotmail.foo> writes:
> Στις 14/1/2014 18:02, ο/η Rainer Weikusat έγραψε:
>> open($fh, '+>', '/tmp/out') or die("$!");
>>
>> while ($data = <STDIN>) {
>>      seek($fh, 0, 0);
>>      truncate($fh, 0);
>>      print $fh ($data);
>>
>>      print("content of out\n");
>>      seek($fh, 0, 0);
>>      printf("\t%s", $_) for <$fh>;
>> }
>> ------
>
> $fh can not read at start the stored value ...

There is no 'stored value at start' in this example because creating the
file with +> truncates it to zero.


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

Date: Tue, 14 Jan 2014 17:41:20 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: purge line
Message-Id: <0f9gqa-u9t1.ln1@anubis.morrow.me.uk>


Quoth Rainer Weikusat <rweikusat@mobileactivedefense.com>:
> George Mpouras <gravitalsun@foo.com> writes:
> >
> > this works but for reading the counter you need the shell command

Do we need to mention perldoc -q locking at this point? 

(Though it seems George hasn't even got as far as considering
locking...)

Ben



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

Date: Tue, 14 Jan 2014 20:39:22 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: purge line
Message-Id: <lb409a$19d4$1@news.ntua.gr>

Στις 14/1/2014 19:46, ο/η Rainer Weikusat έγραψε:
> George Mpouras <gravitalsun@hotmail.foo> writes:
>> Στις 14/1/2014 18:02, ο/η Rainer Weikusat έγραψε:
>>> open($fh, '+>', '/tmp/out') or die("$!");
>>>
>>> while ($data = <STDIN>) {
>>>       seek($fh, 0, 0);
>>>       truncate($fh, 0);
>>>       print $fh ($data);
>>>
>>>       print("content of out\n");
>>>       seek($fh, 0, 0);
>>>       printf("\t%s", $_) for <$fh>;
>>> }
>>> ------
>>
>> $fh can not read at start the stored value ...
>
> There is no 'stored value at start' in this example because creating the
> file with +> truncates it to zero.
>

I know, that is why I end up at the solution without the truncate


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

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


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