[20038] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2233 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Nov 29 00:05:38 2001

Date: Wed, 28 Nov 2001 21:05:07 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <1007010306-v10-i2233@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Wed, 28 Nov 2001     Volume: 10 Number: 2233

Today's topics:
    Re: chown, but no lchown (Clinton A. Pierce)
    Re: chown, but no lchown <mgjv@tradingpost.com.au>
        How can I open a file for appending and reading simulta <shenxin@sympatico.ca>
    Re: How can I open a file for appending and reading sim (Logan Shaw)
    Re: How can I open a file for appending and reading sim <bart.lateur@pandora.be>
    Re: How to implement "peres unbiasing" in perl. <goldbb2@earthlink.net>
    Re: How to implement "peres unbiasing" in perl. <goldbb2@earthlink.net>
    Re: make subset of the search result <bart.lateur@pandora.be>
    Re: Must be a better way.. <krahnj@acm.org>
    Re: newbie learning perl <koshvader@vorlonempire.org>
        Perl/TK 8.023 Install? <david.mohorn@home.com>
        Running Perl Script under X-Windows Session <david.mohorn@home.com>
        UDP Client/Server Questions (Mark Riehl)
    Re: Using CGI.pm to obtain a list of params - Oops! <uri@stemsystems.com>
        Well Formed HTML - Failed Parameter Returns -- 911! (E. Moore)
    Re: Why isn't grep an l-valued function? <mgjv@tradingpost.com.au>
    Re: Why isn't grep an l-valued function? <bart.lateur@pandora.be>
    Re: Why isn't grep an l-valued function? <joe+usenet@sunstarsys.com>
    Re: Why isn't grep an l-valued function? <mgjv@tradingpost.com.au>
    Re: Why isn't grep an l-valued function? (Logan Shaw)
    Re: Why isn't grep an l-valued function? <bart.lateur@pandora.be>
    Re: Why isn't grep an l-valued function? <mgjv@tradingpost.com.au>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 29 Nov 2001 02:20:25 GMT
From: clintp@geeksalad.org (Clinton A. Pierce)
Subject: Re: chown, but no lchown
Message-Id: <JXgN7.58399$RI2.32294940@news2>

[Posted and mailed]

In article <20011128142418.0b2becb2.brooksj-nospam@ee.pdx.edu>,
	Jason Brooks <brooksj-nospam@ee.pdx.edu> writes:
> Hello,
> 
> I need access to the lchown() system call on about three different unix platforms.  The source code appears to check for the existence of such a system call, but how do I go about using it?

lchown(2) is what happens when chown(perl) gets called on some platforms.

If you need to get a hold of specific system calls though, use XS or
the syscall facility to call it directly.

-- 
    Clinton A. Pierce            Teach Yourself Perl in 24 Hours  *and*
  clintp@geeksalad.org                Perl Developer's Dictionary
"If you rush a Miracle Man,     for details, see http://geeksalad.org     
	you get rotten Miracles." --Miracle Max, The Princess Bride


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

Date: Thu, 29 Nov 2001 02:34:09 GMT
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: chown, but no lchown
Message-Id: <slrna0b7le.rql.mgjv@verbruggen.comdyn.com.au>

On Thu, 29 Nov 2001 02:20:25 GMT,
	Clinton A. Pierce <clintp@geeksalad.org> wrote:
> [Posted and mailed]
> 
> In article <20011128142418.0b2becb2.brooksj-nospam@ee.pdx.edu>,
> 	Jason Brooks <brooksj-nospam@ee.pdx.edu> writes:
>> Hello,
>> 
>> I need access to the lchown() system call on about three different
>> unix platforms.  The source code appears to check for the existence
>> of such a system call, but how do I go about using it?
> 
> lchown(2) is what happens when chown(perl) gets called on some platforms.

Are you sure? Am I missing something?

[lines wrapped for post]

$ pwd
/spare/SRC/perl-5.6.1
$ find . -name \*.h -o -name \*.c | xargs grep -i lchown
 ./doio.c:XXX Should we make lchown() directly available from perl?
 ./doio.c:For now, we'll let Configure test for HAS_LCHOWN, but do
 ./vos/config.alpha.h:/* HAS_LCHOWN:
 ./vos/config.alpha.h: * This symbol, if defined, indicates that the
    lchown routine is
 ./vos/config.alpha.h:/*#define HAS_LCHOWN               /**/
 ./vos/config.ga.h:/* HAS_LCHOWN:
 ./vos/config.ga.h: *    This symbol, if defined, indicates that the
    lchown routine is
 ./vos/config.ga.h:/*#define HAS_LCHOWN          /**/

Inspection of other files leads me to believe that Configure does test
for lchown and appropriate configuration variables get set, but that
nothing really happens anywhere to act on this. There's a note in
doio.c, which has been there for a while, that leads me to
believe that the tests in Configure were put there specifically
for a possible future extension that would make lchown available
in Perl:

/* 
XXX Should we make lchown() directly available from perl?
For now, we'll let Configure test for HAS_LCHOWN, but do
nothing in the core.
    --AD  5/1998
*/

Martien
-- 
                                | 
Martien Verbruggen              | 
Trading Post Australia Pty Ltd  | What's another word for Thesaurus?
                                | 


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

Date: Wed, 28 Nov 2001 22:21:01 -0500
From: "xin" <shenxin@sympatico.ca>
Subject: How can I open a file for appending and reading simultaneously?
Message-Id: <SFhN7.17742$cC5.2120488@news20.bellglobal.com>

I want to read it through for many times but only append at the end.





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

Date: 28 Nov 2001 22:10:29 -0600
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: How can I open a file for appending and reading simultaneously?
Message-Id: <9u4cfl$lmi$1@starbuck.cs.utexas.edu>

In article <SFhN7.17742$cC5.2120488@news20.bellglobal.com>,
xin <shenxin@sympatico.ca> wrote:
>I want to read it through for many times but only append at the end.

First, you'd open it with something like

	open (MYFILE, '+<pathname')

Second, the system implicilty remembers the position within the file,
so if you want to do things at different positions, you'll have to use
tell() to read the position and seek() to set it.

If you want to freely intermix reading an appending, you'll want
to do something like this:

	open (MYFILE, '+<pathname') or die;

	$append_position = tell MYFILE;		# it should start at the end
	$read_position = 0;
	$mode = 'append';

	# switch to mode where you can read
	if ($mode eq 'append')
	{
	    $append_position = tell MYFILE;
	    seek (MYFILE, $read_position, 0) or die;
	}

	# read some stuff

	# switch back to mode where you append
	if ($mode eq 'read')
	{
	    $read_position = tell MYFILE;
	    seek (MYFILE, $append_position, 0) or die;
	}

	# now you can append some stuff

If you don't want to intermix reading and appending, then you don't
need to save your place, so it's a little easier.  You just do

	seek (MYFILE, 0, 0);

before you start reading, and you do

	seek (MYFILE, 0, 2);

before you start appending.

  - Logan
-- 
"In order to be prepared to hope in what does not deceive,
 we must first lose hope in everything that deceives."

                                          Georges Bernanos


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

Date: Thu, 29 Nov 2001 04:21:42 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: How can I open a file for appending and reading simultaneously?
Message-Id: <9odb0usffvlu1nsrfchb8k0d5u1hpjn0gb@4ax.com>

Logan Shaw wrote:

>First, you'd open it with something like
>
>	open (MYFILE, '+<pathname')

This:

	open (MYFILE, '+>>pathname')

works too, IIRC. You'll need to seek back to 0 if you want to read the
file, first.

Oh, and note that some OSes demand that you do a seek before switching
from read to write, even if you're absolutely sure that the file
position pointer is positioned correctly. I have had this bad experience
with ActivePerl (Win32; but I'm not sure it was 5.005_x or 5.6.x).

-- 
	Bart.


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

Date: Wed, 28 Nov 2001 21:10:34 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: How to implement "peres unbiasing" in perl.
Message-Id: <3C05991A.BD264673@earthlink.net>

John J. Trammell wrote:
> 
> On Wed, 28 Nov 2001 02:30:35 -0500, Benjamin Goldberg wrote:
[snip]
> I understand the Peres algorithm, but how your code implements it
> is a bit opaque to me.  How do you represent the two children of
> the j'th flip sequence?  I use a heap structure (in case the 2j+1,
> 2j+2 in the code didn't give it away).

Well, I just took my code for the 'multi-level strategy' and tried to
extend it to the 'advanced multi-level strategy.

Apparently, this was the wrong approach.

New code coming soon.

-- 
Klein bottle for rent - inquire within.


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

Date: Wed, 28 Nov 2001 22:46:04 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: How to implement "peres unbiasing" in perl.
Message-Id: <3C05AF7C.36F72106@earthlink.net>

John J. Trammell wrote:
> 
> On Tue, 27 Nov 2001 19:06:28 -0500, Benjamin Goldberg wrote:
> > I'm trying to implement a really funky algorithm known as "Peres
> > unbiasing."
[snip]

I think I can see how to convert your code from an all-at-once algorithm
to an incremental version.

my (@in, $out);
sub get_bit {
   local $_;
   until( length $out ) {
      $in[0] = (coinflip()?1:0).(coinflip()?1:0);
      for( my ($i,$max)=(0,0); $i <= $max; ++$i ) {
         $_ = $in[$i];
         next unless defined && length == 2;
         if( /^00|^11/ ) {
            length($in[$i*2 + 1] .= chop) == 2 and $max = $i*2 + 1;
            length($in[$i*2 + 2] .= 1   ) == 2 and $max = $i*2 + 2;
         } else {
            $out .= chop;
            length($in[$i*2 + 2] .= 0   ) == 2 and $max = $i*2 + 2;
         }
         delete $in[$i];
      }
   }
   chop $out;
}

[untested]

PS, yes, I do mean delete, not undef.

-- 
Klein bottle for rent - inquire within.


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

Date: Thu, 29 Nov 2001 04:16:43 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: make subset of the search result
Message-Id: <0cdb0ucb6et2d1hbtnqvj70bqk8mr2o8tc@4ax.com>

qiang wrote:

>> As Logan said: starting to search again from scratch might well be
>> faster than refining the search based upon stored results. 

>why ?  in general searching from old temp  is faster than searching from 
>scratch, isn't it .

Not necessarily, because in saving the temp data, and loading it in
afterwards, you've got a lot of extra overhead. Databases are likely
optimised for fast searches, there's no writing to files, not all of the
results need even be returned if only a small part of them would be
used.

But if you save a large list of results, *all* of it must be retrieved,
formatted and saved to a text file. Afterwards, in the next run, you
must read it back and interpret it, before you can even do your search
refinement. A lot of extra work, with doubtful return.

-- 
	Bart.


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

Date: Thu, 29 Nov 2001 04:27:10 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Must be a better way..
Message-Id: <3C05B9EA.151AE851@acm.org>

HoboSong wrote:
> 
> Uri Guttman <uri@stemsystems.com> wrote in message news:<x77ksb8qmp.fsf@home.sysarch.com>...
> > >>>>> "JWK" == John W Krahn <krahnj@acm.org> writes:
> >
> >   JWK> my %lookup = ( qr/SETTLEMENT SECTION/,     \&parse_settlement,
> >   JWK>                qr/DIVIDENDS REPORT/,       \&parse_divs,
> >   JWK>                qr/RISK MANAGEMENT REPORT/, \&parse_risk,
> >   JWK>               );
> >
> >   JWK> while ( <TEXTFILE> ) {
> >   JWK>     for my $regex ( keys %lookup ) {
> >   JWK>         /$regex/ and $lookup{ $regex }->( $_ );
> >   JWK>         last;
> >
> > hmm, will that ever try the 2nd and third regexes if the first fails?
> >
> > i think you meant:
> >
> >               $lookup{ $regex }->( $_ ), last if /$regex/ ;
> >
> >   JWK>         }
> >   JWK>     }
> >
> > uri
> 
>  When I go to http://www.perldoc.com/perl5.6/pod/func/qr.html#top to
> look for documentation on "qr/ /" , I get a blank page, Im assuming it
> is "quote regex"?

Yes.

>  Then, If I am reding your code correctly, it looks like the values to
> the %lookup hash are pointers? to the subroutines...so I ask,

Yes.

>   This would only process lines that match the regex?

Yes.

The example wasn't complete (as pointed out by Uri [thanks Uri :)]).  A
more complete example would be:


my %lookup = ( qr/SETTLEMENT SECTION/,     \&parse_settlement,
               qr/DIVIDENDS REPORT/,       \&parse_divs,
               qr/RISK MANAGEMENT REPORT/, \&parse_risk,
              );

LINE:
while ( <TEXTFILE> ) {
    for my $regex ( keys %lookup ) {
        if ( /$regex/ ) {
            $lookup{ $regex }->( $_ );
            last LINE;
            }
        }
    }



John
-- 
use Perl;
program
fulfillment


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

Date: Wed, 28 Nov 2001 19:30:17 -0800
From: Kosh Vader <koshvader@vorlonempire.org>
Subject: Re: newbie learning perl
Message-Id: <m0ab0u430fjlivsq9albod8kkp1onaote9@4ax.com>

Only necessary if the key name is more than one word:

	$words{'me too'} = boo;
	print "$words{'me too'}\n";

Chris

On Thu, 29 Nov 2001 13:06:03 +1100, "Skeleton Man"
<invalid_email@www.skeleton-man.f2s.com> wrote:

>Shouldn't there be single quotes around the key name ?
>e.g. print "$words{'me'}\n";
>                            ^   ^




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

Date: Thu, 29 Nov 2001 03:53:43 GMT
From: "David Mohorn" <david.mohorn@home.com>
Subject: Perl/TK 8.023 Install?
Message-Id: <bjiN7.25621$V46.5994054@news1.rdc1.va.home.com>

When installing Perl/Tk on AIX 4.3, the "make" appears to run.  However, the
"make test" appears to hang.  This is all I see:

root @haraix1:/usr/local/cpan/Tk800.023 ># make test
        cd pTk && make DEFINE=""
Target "makemakerdflt" is up to date.
        LD_RUN_PATH="/lib" ld -o
blib/arch/auto/Tk/Tk.so  -bhalt:4 -bM:SRE -bI:/usr/opt/perl5/lib/5.00503/aix
/CORE/perl.exp -bE:Tk.e
xp -b noentry -lc Tk.o  chnGlue.o  evtGlue.o  objGlue.o  tixGlue.o  tkGlue.o
tkGlue_f.o  tkWin32Dll.o  pTk/libpTk.a  -L/lib -lX11 -
lnsl -lm
        chmod 755 blib/arch/auto/Tk/Tk.so
        PERL_DL_NONLAZY=1
/usr/bin/perl -Iblib/arch -Iblib/lib -I/usr/opt/perl5/lib/5.00503/aix -I/usr
/opt/perl5/lib/5.00503 -e 'use
 Test::Harness qw(&runtests $verbose); $verbose=0; runtests @ARGV;'
t/Require.t t/Trace.t t/X.t t/autoload.t t/balloon.t t/browseent
ry.t t/create.t t/dash.t t/fbox.t t/fileevent.t t/fileselect.t t/font.t
t/geomgr.t t/list.t t/mega.t t/mwm.t t/optmenu.t t/photo.t t
/progbar.t t/widget.t t/zzHList.t t/zzPhoto.t t/zzScrolled.t t/zzText.t
t/zzTixGrid.t
t/Require...........ok
t/Trace.............


Any ideas?






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

Date: Thu, 29 Nov 2001 03:53:43 GMT
From: "David Mohorn" <david.mohorn@home.com>
Subject: Running Perl Script under X-Windows Session
Message-Id: <bjiN7.25622$V46.5993895@news1.rdc1.va.home.com>

Anyone have any ideas why I get the following message when trying to run my
perl script on AIX 4.3?  This is being invoked from an XWindows session (so
I assume the environment isn't setup entirely...)  The script appears to
work fine and the TK window displays..

haraix1:/home/mohord >perl tktest.pl
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LC_ALL = (unset),
        LC__FASTMSG = "true",
        LANG = "En_US"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C")


david.mohorn@home.com





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

Date: 28 Nov 2001 18:47:02 -0800
From: mark_riehl@hotmail.com (Mark Riehl)
Subject: UDP Client/Server Questions
Message-Id: <e4393277.0111281847.51d64045@posting.google.com>

All - I'm using ActivePerl under Win2k.  I wrote a small client and
server using IO::Socket::INET.

Problem is that I can't seem to shut the server down cleanly if the
client goes down first.  If the server is shut down first, I catch the
Ctrl-C, break out of the while loop, close the socket, and clean up a
log file that I'm writing.

If the client goes down first, I can't seem to catch the Ctrl-C in the
server, the server goes down, and I don't get to close a log file that
I'm writing.

I've registered a sig handler using sigtrap:
use sigtrap qw(handler my_handler normal-signals error-signals);

while ($quit == 0) {
     next unless $sock->recv($msg_in,MAX_MSG_LEN);
     print "$msg_in\n";
}

# Close log file and other things happen here.

sub my_handler {
  ++$quit;
}

Any suggestions?  If this is a Win2k/signal issue, any suggestions on
getting around it?

Thanks for the help,
Mark


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

Date: Thu, 29 Nov 2001 04:54:34 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Using CGI.pm to obtain a list of params - Oops!
Message-Id: <x7k7wa5g6s.fsf@home.sysarch.com>

>>>>> "WAS" == William Alexander Segraves <wsegrave@mindspring.com> writes:

  WAS> "Uri Guttman" <uri@stemsystems.com> wrote in message
  WAS> news:x7y9kq5xhq.fsf@home.sysarch.com...
  WAS> Mea culpa! Out, gremlins! I wrote the above when I should have
  WAS> written:
  >> 
  WAS> s/(G)(\d+)/sprintf($1.($2+1))/ge;
  >> 
  >> that sprintf has no arguments other than the format string so it us
  >> useless. a simple $1 . ($2 + 1) will do.
  >> 

  WAS> Gee, I dunno. I thought the argument was $1.($2+1). Regardless, it works
  WAS> well. I'll certainly try your suggestion, though, after dinner.

well you SHOULD know. sprintf works fine with one argument. but then it
doesn't DO ANYTHING USEFUL. it is a NO-OP without format descriptions
and arguments for them. sprintf is the above case is superfluous.

  >> s/G(\d+)/'G'.($1+1)/ge;

  WAS> Bless you, Uri! Your code works; and is more concise, as
  WAS> well. Thank you, thank you, thank you!

blessing i don't need. heeding my comments above is usually a better
thing. :)

uri


-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
-- Stem is an Open Source Network Development Toolkit and Application Suite -
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: 28 Nov 2001 18:54:26 -0800
From: emoore@cofs.net (E. Moore)
Subject: Well Formed HTML - Failed Parameter Returns -- 911!
Message-Id: <f024c24d.0111281854.12622074@posting.google.com>

The problem:  On some apparently well formed cgi-generated HTMLs (e.g.
syntax is checked => absolutely clean) and for no apparent reason,
when submitted on some users machines, no parameters are returned
except a cookie!  There is no rhyme nor reason.

I thought this was purely something on the users' sides, but I have a
particular instance that is very troublesome:

A simple form with a select (e.g. select the year for next form).

User selects and submits. 

The CGI  reads the  parameters (including a cookie) from this form,
queries the data base, and returns another form for a user to
enter/modify data and submit.

For 3 of the four years, everything is perfect.  On one year, and one
year only, when this second cgi-generated form is submitted, the only
parameter returned is the cookie.  It's the same script, HTML pattern
file, etc., used by all years.  Everything is identical.  When I check
the resulting HTML in a syntax checker, it's 100% perfect!

Has anyone any suggestions or run across this type of problem before? 
I think I have tried everything and still I cannot resolve this
problem.

I would truly appreciate any ideas.  Thanks, Eric.


basic system configuration:
Linux /Perl 5.6.0 /Apache 1.3.9
CGI.pm 2.78
AutoLoader.pm 
Common.pm  --- a package of home-grown common subroutines for the app.


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

Date: Thu, 29 Nov 2001 02:07:31 GMT
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: Why isn't grep an l-valued function?
Message-Id: <slrna0b63g.rql.mgjv@verbruggen.comdyn.com.au>

On Thu, 29 Nov 2001 01:31:47 GMT,
	Bart Lateur <bart.lateur@pandora.be> wrote:
> Martien Verbruggen wrote:
> 
>>>   grep( $_ % 2, @a ) = "a".."z";
>>> 
>>> ~>Can't modify grep iterator in scalar assignment ...
>>> 
>>> Why is this illegal? 
>>
>>Because grep operates on a list, not an array. You can't modify a
>>list. How would 
>>
>>grep( $_ % 2, (@a, 5, 6) ) = "a" .. "z";
>>
>>work if grep produced an lvalue array? and where would this array be
>>stored?
> 
> That doesn't make sense. The argument between parens for a foreach
> command, is a list too, yet you can still modify it.
>
> 	@a = ('A' .. 'Z');
> 	foreach(@a, $x, $y) {
> 	    $_ = ++$i;
> 	}
> 
> This works: every value, i.e. every array item and every scalar
> variable, will be modified.

But you modify the individual elements of the list, not the list
itself. The suggested (grep) approach is an assignment in list
context, which is a different beast, IMO.

> It works with slices, too.
> 
> The reason why it doesn't work with grep, is IMO incidental: grep()
> doesn't return a list of Lvalues, *that* is the problem. But so far,
> nobody has ever requested that feature, I think, I don't think many
> people have even tried to use it. Perhaps there are even speed
> considerations against it. Funny, the $_ alias in the grep block *is* an
> Lvalue.

Even if it did, it couldn't do what the original intent was. To be
able to do that, it would need to return a list of _aliases_ to the
original elements of the list. To reiterate that:

It was asked why

  @a = 1..4;
  grep( $_ % 2, @a ) = "a".."z";

Does not behave, or couldn't be made to behave like:

  @a[  grep $a[$_] % 2, 0 .. $#a  ] = "a" .. "z";

which produces

  ~> @a = ("a", 2, "b", 4)

If grep suddenly started returning aliases, many things could go very
strange.

This would be a consequence of it:

@a = 1 .. 4;
@b = grep { $_ % 2 } @a;

$b[0] = 3;

Now, @a would be (3, 2, 3, 4);

I suspect that grep could be made to return a list of aliases only if
used as an lvalue function, but that would, IMO only be confusing. And
besides that, what would be the result of:

@b = (grep { $_ % 2 } @a) = "a" .. "z";

Would @b contain aliases to two of the elements of @a?

Martien
-- 
                                | 
Martien Verbruggen              | Hi, Dave here, what's the root
Trading Post Australia Pty Ltd  | password?
                                | 


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

Date: Thu, 29 Nov 2001 02:34:54 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: Why isn't grep an l-valued function?
Message-Id: <ll7b0ukg6l0mndepjv82m4lf68gd52c2ih@4ax.com>

Martien Verbruggen wrote:

>If grep suddenly started returning aliases, many things could go very
>strange.
>
>This would be a consequence of it:
>
>@a = 1 .. 4;
>@b = grep { $_ % 2 } @a;
>
>$b[0] = 3;
>
>Now, @a would be (3, 2, 3, 4);

Yup, you're right. I hadn't thought of that. But you are absolutely
right. And we can't have that.

-- 
	Bart.


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

Date: 28 Nov 2001 21:48:19 -0500
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: Why isn't grep an l-valued function?
Message-Id: <m3wv0a2sy4.fsf@mumonkan.sunstarsys.com>

Bart Lateur <bart.lateur@pandora.be> writes:

> Martien Verbruggen wrote:
> 
> >If grep suddenly started returning aliases, many things could go very
> >strange.
> >
> >This would be a consequence of it:
> >
> >@a = 1 .. 4;
> >@b = grep { $_ % 2 } @a;
> >
> >$b[0] = 3;
> >
> >Now, @a would be (3, 2, 3, 4);
> 
> Yup, you're right. I hadn't thought of that. But you are absolutely
> right. And we can't have that.

OK- now I'm confused again.  Here's what the documentation for grep
says:
  
       grep BLOCK LIST
       grep EXPR,LIST

       [...] 
               Similarly, grep returns aliases into
               the original list, much as a for loop's index
               variable aliases the list elements.  That is,
               modifying an element of a list returned by grep
               (for example, in a "foreach", "map" or another
               "grep") actually modifies the element in the
               original list.


So grep *already* returns a list of aliases, right?  Why do
you think what I'm proposing would cause @a above to be altered?


-- 
Joe Schaefer   "There is nothing so annoying as to have two people talking when
                                  you're busy interrupting."
                                               --Mark Twain



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

Date: Thu, 29 Nov 2001 03:20:15 GMT
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: Why isn't grep an l-valued function?
Message-Id: <slrna0babs.rql.mgjv@verbruggen.comdyn.com.au>

On 28 Nov 2001 21:48:19 -0500,
	Joe Schaefer <joe+usenet@sunstarsys.com> wrote:
> Bart Lateur <bart.lateur@pandora.be> writes:
> 
>> Martien Verbruggen wrote:
>> 
>> >If grep suddenly started returning aliases, many things could go very
>> >strange.
>> >
>> >This would be a consequence of it:
>> >
>> >@a = 1 .. 4;
>> >@b = grep { $_ % 2 } @a;
>> >
>> >$b[0] = 3;
>> >
>> >Now, @a would be (3, 2, 3, 4);
>> 
>> Yup, you're right. I hadn't thought of that. But you are absolutely
>> right. And we can't have that.
> 
> OK- now I'm confused again.  Here's what the documentation for grep
> says:
>   
>        grep BLOCK LIST
>        grep EXPR,LIST
> 
>        [...] 
>                Similarly, grep returns aliases into
>                the original list, much as a for loop's index
>                variable aliases the list elements.  That is,
>                modifying an element of a list returned by grep
>                (for example, in a "foreach", "map" or another
>                "grep") actually modifies the element in the
>                original list.
> 
> 
> So grep *already* returns a list of aliases, right?  Why do
> you think what I'm proposing would cause @a above to be altered?

Well, look at that... That is interesting. I never saw that bit, but
it seems to be true:

$ perl -wl
@a = 1..4;
@b = grep { $_ *= 10 } grep { $_ % 2 } @a;
print "@a";
print "@b";
__END__
10 2 30 4
10 30

Of course, @b doesn't contain the aliases anymore, but it contains
copies. However, this is not because grep returns copies, but because
of the assignment operation.

Let me think for a bit about this.

Ok... I think I need to retract everything I said before, both in
response to your post, and Bart Lateur's. Apart from the fact that
grep is not an lvalue function, I don't see any reason why

@a = 1..4;
@b = (grep { $_ % 2 } @a) = "a".."z";
$b[0] = 12;

_couldn't_ be made to result in 

 @a : ("a", 2, "b", 4)
 @b : (12, "b")

Of course, on the other hand, I don't feel it fits the semantics of
grep very well to have it behave this way. It's almost like grep or
map in a void context, i.e. it isn't what grep is meant to do. I doubt
that anyone will want this changed, even though it may be possible.

Martien
-- 
                                | 
Martien Verbruggen              | Unix is the answer, but only if you
Trading Post Australia Pty Ltd  | phrase the question very carefully
                                | 


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

Date: 28 Nov 2001 21:44:39 -0600
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: Why isn't grep an l-valued function?
Message-Id: <9u4av7$lg7$1@starbuck.cs.utexas.edu>

In article <slrna0b63g.rql.mgjv@verbruggen.comdyn.com.au>,
Martien Verbruggen  <mgjv@tradingpost.com.au> wrote:
>Even if it did, it couldn't do what the original intent was. To be
>able to do that, it would need to return a list of _aliases_ to the
>original elements of the list.

So are you saying Perl handles things in such a way that it's not
possible to return a list which is itself an lvalue?  A list whose
elements are all lvalues isn't the same idea as a list that itself is
an lvalue.

Anyway, I now present lvgrep():

	#! /packages/perl-5.6.1/bin/perl

	sub lvgrep (&@) : lvalue
	{
	    my $code = shift;

	    my @indices;
	    my $index = 0;

	    foreach $_ (@_)
	    {
		push (@indices, $index) if &$code;
		$index++;
	    }

	    @_[@indices];
	}

	# try it with an array
	@a = 1 .. 10;
	(lvgrep { $_ % 2 } @a) = a .. z;
	print "\@a:  ", join (" ", @a), "\n";

	# try it with a list
	($x, $y, $z) = 1 .. 3;
	(lvgrep { $_ % 2 } $x, $y, $z ) = a .. z;
	print "\$x, \$y, and \$z:  $x $y $z\n";

It's not the most beautiful thing imaginable, but it works:

	$ ./lvgrep.pl
	@a:  a 2 b 4 c 6 d 8 e 10
	$x, $y, and $z:  a 2 b
	$ 

Hmm.  Why was it again that anyone would ever actually want to use
this?

  - Logan
-- 
"In order to be prepared to hope in what does not deceive,
 we must first lose hope in everything that deceives."

                                          Georges Bernanos


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

Date: Thu, 29 Nov 2001 03:50:44 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: Why isn't grep an l-valued function?
Message-Id: <uubb0uckqurk9qguq0vq1c93tq94g6et85@4ax.com>

Martien Verbruggen wrote:

>Ok... I think I need to retract everything I said before, both in
>response to your post, and Bart Lateur's. 

I hate  to admit that I was wrong about being wrong. This is all too
confusing. Now if that's not a good enough reason to stay away from
it... :-)

Anyway, if grep returns a list of lvalues, there's not really a reason
why you shouldn't be able to assign to it. Except for the list I gave
earlier: nobody ever asked for this feature (until now), or it might be
slower when implemented, or... or?

-- 
	Bart.


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

Date: Thu, 29 Nov 2001 05:03:45 GMT
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: Why isn't grep an l-valued function?
Message-Id: <slrna0bgdt.rql.mgjv@verbruggen.comdyn.com.au>

On 28 Nov 2001 21:44:39 -0600,
    Logan Shaw <logan@cs.utexas.edu> wrote:
> In article <slrna0b63g.rql.mgjv@verbruggen.comdyn.com.au>,
> Martien Verbruggen  <mgjv@tradingpost.com.au> wrote:
>
>>Even if it did, it couldn't do what the original intent was. To be
>>able to do that, it would need to return a list of _aliases_ to the
>>original elements of the list.
> 
> So are you saying Perl handles things in such a way that it's not
> possible to return a list which is itself an lvalue?  A list whose
> elements are all lvalues isn't the same idea as a list that itself is
> an lvalue.

A list isn't the same as an array or array slice :) But it doesn't
really matter, since all that is really required for this to work is
that a list of lvalues is returned. We're not assigning to the 'list',
but to the individual elements of it. The whole list thing is a red
herring, which is my fault.

If grep was an lvalue function, it could probably arrange for things
to be handled in very much the same way as your subroutine does
(which, BTW, is quite nice).

> Anyway, I now present lvgrep():
> 
>   #! /packages/perl-5.6.1/bin/perl
> 
>   sub lvgrep (&@) : lvalue
>   {
>       my $code = shift;
> 
>       my @indices;
>       my $index = 0;
> 
>       foreach $_ (@_)
>       {
>           push (@indices, $index) if &$code;
>           $index++;
>       }
> 
>       @_[@indices];
>   }

> It's not the most beautiful thing imaginable, but it works:

And it also neatly returns a list of (expected) values:

@a = 1 .. 10;
@b = (lvgrep { $_ % 2 } @a) = "a" .. "z";
print "\@a:  @a";
print "\@b:  @b";

@a:  a 2 b 4 c 6 d 8 e 10
@b:  a b c d e

And it even returns a list of aliases to the original elements, just
like grep:

@a = 1 .. 10;
@b = lvgrep { $_ *= 2 } lvgrep { $_ % 2 } @a;
print "@a";
print "@b";

@a = 1 .. 10;
@b = grep { $_ *= 2 } grep { $_ % 2 } @a;
print "@a";
print "@b";

2 2 6 4 10 6 14 8 18 10
2 6 10 14 18
2 2 6 4 10 6 14 8 18 10
2 6 10 14 18

Pretty cool.

> Hmm.  Why was it again that anyone would ever actually want to use
> this?

Semantics, maybe. Assigning to a grep is just... well.. weird. But
that may be unfamiliarity :)

Does anyone have a real-world use for this?

Martien
-- 
                                | 
Martien Verbruggen              | I took an IQ test and the results
Trading Post Australia Pty Ltd  | were negative.
                                | 


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

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:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

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 V10 Issue 2233
***************************************


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