[29454] in Perl-Users-Digest
Perl-Users Digest, Issue: 698 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jul 30 06:09:46 2007
Date: Mon, 30 Jul 2007 03:09:05 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Mon, 30 Jul 2007 Volume: 11 Number: 698
Today's topics:
Re: @arts <zaxfuuq@invalid.net>
Re: @arts anno4000@radom.zrz.tu-berlin.de
Re: fork command. <rajendra.prasad@in.bosch.com>
Re: fork command. anno4000@radom.zrz.tu-berlin.de
Re: getting arguments <zaxfuuq@invalid.net>
new CPAN modules on Mon Jul 30 2007 (Randal Schwartz)
Re: Objects/Structures in Perl (aka ? the Platypus)
Perl threads ivakras1@gmail.com
Re: Perl with DBI <jwcarlton@gmail.com>
Re: Perl with DBI <jwcarlton@gmail.com>
Re: Perl with DBI <jwcarlton@gmail.com>
question on reference to array slice <rick.peng.du@gmail.com>
Re: question on reference to array slice <noreply@gunnar.cc>
Re: question on reference to array slice <wahab-mail@gmx.de>
Re: question on reference to array slice <dummy@example.com>
Re: question on reference to array slice <wahab-mail@gmx.de>
Re: question on reference to array slice <noreply@gunnar.cc>
Re: question on reference to array slice <attn.steven.kuo@gmail.com>
Re: question on reference to array slice <mritty@gmail.com>
Reading from stdin then launching a program that reads <stefano.sabatini-lala@poste.it>
Re: Variable dump <bill@ts1000.us>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 30 Jul 2007 03:11:40 -0700
From: "Wade Ward" <zaxfuuq@invalid.net>
Subject: Re: @arts
Message-Id: <rrydnRx-kfGqEjDbnZ2dnUVZ_s-pnZ2d@comcast.com>
"A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote in message
news:Xns997BF3BADEEFasu1cornelledu@127.0.0.1...
> "A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote in
> news:Xns997BEE7F47DDCasu1cornelledu@127.0.0.1:
>
>> "Wade Ward" <zaxfuuq@invalid.net> wrote in
>> news:nPOdnbYLBJHDIzbbnZ2dnUVZ_j-dnZ2d@comcast.com:
>>
>>>
>>> "Michele Dondi" <bik.mido@tiscalinet.it> wrote in message
>>> news:pd8ja3hrraji8k1uhjegf6i2v5jvdv7udt@4ax.com...
>>>> On Thu, 26 Jul 2007 18:32:50 -0700, "Wade Ward"
> <zaxfuuq@invalid.net>
>>>> wrote:
>>>
>>>>>> {
>>>>>> my $i;
>>>>>> sub i () : lvalue { $i }
>>>>>> }
>>>>>How does this not have a name?
>>>>
>
> ...
>
>> This way, your for loops can look more like C:
>>
>
> In fact, while you are at it, you can generalize the whole thing:
>
> C:\Home\asu1\Src\test> cat Clikei.pm
> package Clikei;
>
> use strict;
> use warnings;
>
> sub import {
> my $package = shift;
> my $export_to = caller;
>
> for my $name ( @_ ) {
> my $sub_name = "${export_to}::${name}";
> {
> my $x;
> no strict 'refs';
> *{$sub_name} = sub () : lvalue { $x };
> }
> }
> }
>
> "I like C"
>
> __END__
>
> C:\Home\asu1\Src\test> cat i.pl
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> use lib '.';
> use Clikei qw( i j k );
line 7 ^^^^^^
>
> for( i = 0; i < 10; ++i ) {
> printf( "Hello i: %d\n", i );
> }
>
> for( j = i; j < i + 10; ++j ) {
> printf( "Hello j: %d\n", j );
> }
>
> for( k = j; k < j + 10; ++k ) {
> printf( "Hello k: %d\n", k );
> }
>
> __END__
My compiler takes a puke on line 7, which is a use statement. Does this not
mean that I need to install another module on my machine?
--
Wade Ward
------------------------------
Date: 30 Jul 2007 08:33:55 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: @arts
Message-Id: <5h5m3jF3j4vb7U1@mid.dfncis.de>
Wade Ward <zaxfuuq@invalid.net> wrote in comp.lang.perl.misc:
>
> "A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote in message
> news:Xns997BF3BADEEFasu1cornelledu@127.0.0.1...
> > "A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote in
> > news:Xns997BEE7F47DDCasu1cornelledu@127.0.0.1:
> >
> >> "Wade Ward" <zaxfuuq@invalid.net> wrote in
> >> news:nPOdnbYLBJHDIzbbnZ2dnUVZ_j-dnZ2d@comcast.com:
> >>
> >>>
> >>> "Michele Dondi" <bik.mido@tiscalinet.it> wrote in message
> >>> news:pd8ja3hrraji8k1uhjegf6i2v5jvdv7udt@4ax.com...
> >>>> On Thu, 26 Jul 2007 18:32:50 -0700, "Wade Ward"
> > <zaxfuuq@invalid.net>
> >>>> wrote:
> >>>
> >>>>>> {
> >>>>>> my $i;
> >>>>>> sub i () : lvalue { $i }
> >>>>>> }
> >>>>>How does this not have a name?
> >>>>
> >
> > ...
> >
> >> This way, your for loops can look more like C:
> >>
> >
> > In fact, while you are at it, you can generalize the whole thing:
> >
> > C:\Home\asu1\Src\test> cat Clikei.pm
> > package Clikei;
> >
> > use strict;
> > use warnings;
> >
> > sub import {
> > my $package = shift;
> > my $export_to = caller;
> >
> > for my $name ( @_ ) {
> > my $sub_name = "${export_to}::${name}";
> > {
> > my $x;
> > no strict 'refs';
> > *{$sub_name} = sub () : lvalue { $x };
> > }
> > }
> > }
> >
> > "I like C"
> >
> > __END__
> >
> > C:\Home\asu1\Src\test> cat i.pl
> > #!/usr/bin/perl
> >
> > use strict;
> > use warnings;
> >
> > use lib '.';
> > use Clikei qw( i j k );
> line 7 ^^^^^^
>
> >
> > for( i = 0; i < 10; ++i ) {
> > printf( "Hello i: %d\n", i );
> > }
> >
> > for( j = i; j < i + 10; ++j ) {
> > printf( "Hello j: %d\n", j );
> > }
> >
> > for( k = j; k < j + 10; ++k ) {
> > printf( "Hello k: %d\n", k );
> > }
> >
> > __END__
> My compiler takes a puke on line 7, which is a use statement. Does this not
> mean that I need to install another module on my machine?
Not if Clikey.pm is in your current directory.
Anno
------------------------------
Date: Mon, 30 Jul 2007 13:17:33 +0530
From: "rajendra" <rajendra.prasad@in.bosch.com>
Subject: Re: fork command.
Message-Id: <f8k52o$icr$1@news4.fe.internet.bosch.com>
One more query:
Can I use a variable common to both child and parent process block?.
"rajendra" <rajendra.prasad@in.bosch.com> wrote in message
news:f89s46$i07$1@news4.fe.internet.bosch.com...
> Hello All,
> The perl documentation says the fork command generates two copies of the
> program ,one parent and one child.
> If this is correct,can this fork command be used for multitasking?.
>
>
>
------------------------------
Date: 30 Jul 2007 08:37:50 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: fork command.
Message-Id: <5h5mauF3j4vb7U2@mid.dfncis.de>
rajendra <rajendra.prasad@in.bosch.com> wrote in comp.lang.perl.misc:
> One more query:
> Can I use a variable common to both child and parent process block?.
You can. Note that the same name addresses different variables in
the parent and the child. They will have the same initial value in
both, but changes in one don't reflect in the other.
Anno
------------------------------
Date: Mon, 30 Jul 2007 03:15:13 -0700
From: "Wade Ward" <zaxfuuq@invalid.net>
Subject: Re: getting arguments
Message-Id: <1fydnfu1TJSQDTDbnZ2dnUVZ_hGdnZ2d@comcast.com>
"Jens Thoms Toerring" <jt@toerring.de> wrote in message
news:5h1p15F3hu78jU1@mid.uni-berlin.de...
> frytaz@gmail.com <frytaz@gmail.com> wrote:
>> I want to run my per script with arguments, for instance:
>> ./script.pl -one value one text 1 -two value two text 2 -three value
>> three text 3
>
> The usual convention with atrguments is to have them enclosed in
> (double) quotes when they contain spaces, so they arrive as a
> whole in your program
>
>> and i want to get those arguments in
>> $one = value one text 1
>> $two = value two text 2
>> $three = value three text 3
>
>> i tried with regular expression m/-one (.+?) -two (.+?) -three (.+?)/
>> and it works but when i mix-up arguments like:
>> ./script.pl -two value two text 2 -three value three text 3 -one value
>> one text 1
>> then i need to use different regex.
>
> Perhaps this does the trick:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $opts = '^-(one|two|three)$';
>
> $ARGV[ 0 ] =~ /$opts/ or die "Invalid arguments\n";
>
> my $state;
> my %args;
> for ( @ARGV ) {
> if ( /$opts/ ) {
> $state = $1;
> $args{ $state } = [ ];
> next;
> }
> push @{ $args{ $state } }, $_;
> }
>
> my ( $one, $two, $three ) = map { join ' ', @{ $args{ $_ } } }
> qw/ one two three /;
>
Vielen Dank, Jens.
--
Wade Ward
------------------------------
Date: Mon, 30 Jul 2007 04:42:16 GMT
From: merlyn@stonehenge.com (Randal Schwartz)
Subject: new CPAN modules on Mon Jul 30 2007
Message-Id: <JLz6EG.1AB0@zorch.sf-bay.org>
The following modules have recently been added to or updated in the
Comprehensive Perl Archive Network (CPAN). You can install them using the
instructions in the 'perlmodinstall' page included with your Perl
distribution.
Apache-Yaalr-0.02.8
http://search.cpan.org/~jeremiah/Apache-Yaalr-0.02.8/
Yet Another Apache Log Reader
----
Archive-Unzip-Burst-0.01
http://search.cpan.org/~smueller/Archive-Unzip-Burst-0.01/
Featureless but fast ZIP extraction
----
Catalyst-Plugin-PageCache-0.17
http://search.cpan.org/~agrundma/Catalyst-Plugin-PageCache-0.17/
Cache the output of entire pages
----
Etk-Perl-0.07
http://search.cpan.org/~leviathan/Etk-Perl-0.07/
----
Event-Lib-1.03
http://search.cpan.org/~vparseval/Event-Lib-1.03/
Perl extentions for event-based programming
----
Flickr-License-0.02
http://search.cpan.org/~cowfish/Flickr-License-0.02/
Represents the license of a photo from Flickr
----
Games-Dice-0.999_01
http://search.cpan.org/~rjbs/Games-Dice-0.999_01/
a set of dice for rolling
----
Games-Dice-0.999_02
http://search.cpan.org/~rjbs/Games-Dice-0.999_02/
a set of dice for rolling
----
HTML-Tooltip-Javascript-1.01
http://search.cpan.org/~unisolve/HTML-Tooltip-Javascript-1.01/
Provides a perl interface for easy use of Walter Zorn's javascript tooltip library (versions prior to 4.0). See http://www.walterzorn.com/tooltip_old/tooltip_e.htm
----
Imager-Search-0.04
http://search.cpan.org/~adamk/Imager-Search-0.04/
Locate an image inside another image
----
MIME-Lite-3.01_06
http://search.cpan.org/~rjbs/MIME-Lite-3.01_06/
low-calorie MIME generator
----
Mail-SpamAssassin-3.2.2
http://search.cpan.org/~jmason/Mail-SpamAssassin-3.2.2/
Spam detector and markup engine
----
Mediawiki-Blame-0.0.1
http://search.cpan.org/~daxim/Mediawiki-Blame-0.0.1/
see who is responsible for each line of page content
----
OpenGL-0.56
http://search.cpan.org/~bfree/OpenGL-0.56/
----
OpenGL-Shader-1.00
http://search.cpan.org/~bfree/OpenGL-Shader-1.00/
copyright 2007 Graphcomp - ALL RIGHTS RESERVED Author: Bob "grafman" Free - grafman@graphcomp.com
----
Options-1.5.2
http://search.cpan.org/~pchriste/Options-1.5.2/
Yet another Perl module to provide support for command-line option parsing and usage generation.
----
PAR-0.976
http://search.cpan.org/~smueller/PAR-0.976/
Perl Archive Toolkit
----
PAR-Dist-0.25
http://search.cpan.org/~smueller/PAR-Dist-0.25/
Create and manipulate PAR distributions
----
PAR-Packer-0.976
http://search.cpan.org/~smueller/PAR-Packer-0.976/
PAR Packager
----
Palm-TreoPhoneCallDB-1
http://search.cpan.org/~dcantrell/Palm-TreoPhoneCallDB-1/
Handler for Treo PhoneCallDB databases
----
Pugs-Compiler-Rule-0.26
http://search.cpan.org/~agent/Pugs-Compiler-Rule-0.26/
Compiler for Perl 6 regexes
----
Scrabble-Dict-0.01
http://search.cpan.org/~acg/Scrabble-Dict-0.01/
look up words in the official Scrabble dictionary
----
Statistics-Benford-0.01
http://search.cpan.org/~gray/Statistics-Benford-0.01/
calculate the deviation from Benford's Law
----
Sub-Todo-v0.0.1
http://search.cpan.org/~dmuey/Sub-Todo-v0.0.1/
mark subroutines or methods as 'TODO'
----
Template-Plugin-DtFormatter-RelativeDate-0.01
http://search.cpan.org/~bokutin/Template-Plugin-DtFormatter-RelativeDate-0.01/
return finder like relative date.
----
Template-Plugin-DtFormatter-RelativeDate-0.02
http://search.cpan.org/~bokutin/Template-Plugin-DtFormatter-RelativeDate-0.02/
return finder like relative date.
----
Test-Reporter-1.32
http://search.cpan.org/~fhoxh/Test-Reporter-1.32/
sends test results to cpan-testers@perl.org
----
URI-Template-0.08_01
http://search.cpan.org/~bricas/URI-Template-0.08_01/
Object for handling URI templates
If you're an author of one of these modules, please submit a detailed
announcement to comp.lang.perl.announce, and we'll pass it along.
This message was generated by a Perl program described in my Linux
Magazine column, which can be found on-line (along with more than
200 other freely available past column articles) at
http://www.stonehenge.com/merlyn/LinuxMag/col82.html
print "Just another Perl hacker," # the original
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
------------------------------
Date: Sun, 29 Jul 2007 20:50:19 GMT
From: "David Formosa (aka ? the Platypus)" <dformosa@usyd.edu.au>
Subject: Re: Objects/Structures in Perl
Message-Id: <slrnfapvs1.219.dformosa@localhost.localdomain>
On Fri, 27 Jul 2007 05:16:18 -0700, O. Olson <olson_ord@yahoo.it> wrote:
[...]
> This is an interesting thing. I looked at "perlsub" - and though they
> mention creating subroutines with prototypes - they do not elaborate
> on it. For e.g. I tried:
>
> sub printTime($temp_time)
>{
> print "$temp_time";
>}
>
> Assuming $temp_time is a scalar like an integer. The compiler
> complains that $temp_time is not defined.
Unfortunitly in perl5 you can't do that. The best you can do is the
following.
sub print_time ($) {
my $temp_time = shift;
print $temp_time;
}
------------------------------
Date: Mon, 30 Jul 2007 08:55:55 -0000
From: ivakras1@gmail.com
Subject: Perl threads
Message-Id: <1185785755.830610.113400@w3g2000hsg.googlegroups.com>
Hi all!
I have a perl threads question. Of cuz i have read perl threads
tutorial and think its clear to me. But some questions...
Q1: Is threads->create method only creates thread, but not runs theads
until im join it (or detach)?
The folowing code and its output makes me think so:
=====================
...
for (my $i=0;$i<=10;$i++)
{
my $trl=threads->create(\&my_funct,$my_args);
if (@tc=threads->list){print "\t".@tc." threads running";}
}
....
if (@tc=threads->list)
{
print "\nClosing threads: ";
while (@tc-1)
{
@tc=threads->list;
print @tc." ";
threads->list->join;
}
}
else
{
exit(0);
}
...
sub my_funct
{
$id=int(rand(1000));
setlogsock('unix'); #im using syslog to write a log file in /
var/log/messages
openlog('PerlSniffer','','debug'); #open a log
syslog('info', "Thread $id start"); #WRITE THREAD START TIME
$w=int(rand(5));
sleep($w); #SLEEP $w SECONDS
syslog('info', "Thread $id stop after $w s sleep"); #WRITE
THREAD STOP TIME
closelog;
return(1);
}
....
=======================
The output of that program:
0 threads running
1 threads running
2 threads running
3 threads running
4 threads running
5 threads running
6 threads running
7 threads running
8 threads running
9 threads running
10 threads running
Closing threads: 10 9 8 7 6 5 4 3 2 1 0
=======================
/var/log/messages for that:
Jul 30 12:20:43 test_comp PerlSniffer: Thread 914 start
Jul 30 12:20:44 test_comp PerlSniffer: Thread 560 start
Jul 30 12:20:45 test_comp PerlSniffer: Thread 914 stop after 2 s sleep
Jul 30 12:20:45 test_comp PerlSniffer: Thread 560 stop after 1 s sleep
Jul 30 12:20:46 test_comp PerlSniffer: Thread 451 start
Jul 30 12:20:46 test_comp PerlSniffer: Thread 451 stop after 0 s sleep
Jul 30 12:20:48 test_comp PerlSniffer: Thread 303 start
Jul 30 12:20:48 test_comp PerlSniffer: Thread 303 stop after 0 s sleep
Jul 30 12:20:49 test_comp PerlSniffer: Thread 834 start
Jul 30 12:20:50 test_comp PerlSniffer: Thread 311 start
Jul 30 12:20:51 test_comp PerlSniffer: Thread 311 stop after 1 s sleep
Jul 30 12:20:52 test_comp PerlSniffer: Thread 677 start
Jul 30 12:20:52 test_comp PerlSniffer: Thread 677 stop after 0 s sleep
Jul 30 12:20:52 test_comp PerlSniffer: Thread 834 stop after 3 s sleep
Jul 30 12:20:52 test_comp PerlSniffer: Thread 133 start
Jul 30 12:20:52 test_comp PerlSniffer: Thread 133 stop after 0 s sleep
Jul 30 12:20:54 test_comp PerlSniffer: Thread 718 start
Jul 30 12:20:55 test_comp PerlSniffer: Thread 718 stop after 0 s sleep
Jul 30 12:20:56 test_comp PerlSniffer: Thread 909 start
Jul 30 12:20:58 test_comp PerlSniffer: Thread 520 start
Jul 30 12:21:00 test_comp PerlSniffer: Thread 909 stop after 4 s sleep
Jul 30 12:21:02 test_comp PerlSniffer: Thread 520 stop after 4 s sleep
=======================
Coments: i create 11 threads first, and than join them. Each thread
waits for 0-5 (rand(5)) seconds, and than exits, writes a start time
and a stop time in /var/log/messages via syslogd. It looks like thread-
>create only creates the thread, not runs it. The output shoes it:
threads->list increments every threads->create, but some of created
threads would be done during this time, if i wrong!!! When i threads-
>list->join one by one the threads seems to start, and threads->list
starts decreasing! Can anybody answer me?
Q2: threads->list returns a list of unjoind or undetached threads. How
to return a count of all threads, joined and detached both?? Then the
main program exits while some child threads are still running
(detached), perl warns me the count of running parentless threads. How
can i get that count during main program still alive?!
Thanks a lot and forgive me my baaad english, im russian actually.
Praying for the answers! Thanks again!
------------------------------
Date: Sun, 29 Jul 2007 15:15:58 -0700
From: Jason <jwcarlton@gmail.com>
Subject: Re: Perl with DBI
Message-Id: <1185747358.129381.280330@l70g2000hse.googlegroups.com>
> I'm not sure but maybe the ' is the reason. I use ` (backtick) or nothing.
> In other word
>
> $dbh->do("CREATE TABLE Test3 (date VARCHAR(14) NOT NULL,
> or
> $dbh->do("CREATE TABLE `Test3` (`date` VARCHAR(14) NOT NULL,
>
> Now I see that table name contain space. Hmmm ... is this allowed? I'm using
> MySQL 3.x and there spaces are not allowed in database/table name.
Technically, it was allowed with the 4.1 that I'm using, but I realize
now that my structure wasn't logical and that using the whitespace was
a bad idea. Even the idea of having a dynamic table name turned out to
not be such a great idea.
- J
------------------------------
Date: Sun, 29 Jul 2007 15:47:04 -0700
From: Jason <jwcarlton@gmail.com>
Subject: Re: Perl with DBI
Message-Id: <1185749224.019839.159120@k79g2000hse.googlegroups.com>
> But this is very dangerous unless you tightly restrict the users who can
> invoke your CGI script. A user could invoke almost any SQL command this
> way (read up on "SQL injection").
I've moved away from the idea of dynamic table names, but I'm
concerned that they can invoke commands through other means. I read
this warning in a tutorial, but there was no explanation on how to get
around it.
Currently, the only MySQL parts of the script that the user can invoke
are:
# Delete if id exists
if (param('reply') {
$dbh->do("DELETE FROM subjects WHERE id=" . $dbh->quote($id));
}
# Add new id
my $sth = $dbh->prepare(<<SQL);
INSERT INTO subjects (id, lastmodified, subject)
VALUES (?, ?, ?)
SQL
$sth->execute($id, $timestamp, param('subject'));
# Add to Posts Table
my $sth = $dbh->prepare(<<SQL);
INSERT INTO posts (id, subject, postdate, username, email, comment)
VALUES (?, ?, ?, ?, ?, ?)
SQL
$sth->execute($id, param('subject'), $timestamp, $username,
param('email'), param('comment'));
I'm hoping that the use of binding will prevent the users from
inserting things like "; drop database". Someone mentioned that in
passing in another thread, but I haven't been able to confirm whether
this is true. The alternative was to manually remove any "bad"
commands from the strings, but there's no way that I could think of
all variations.
------------------------------
Date: Sun, 29 Jul 2007 15:57:13 -0700
From: Jason <jwcarlton@gmail.com>
Subject: Re: Perl with DBI
Message-Id: <1185749833.921782.52510@w3g2000hsg.googlegroups.com>
I'm tagging this onto the same thread because it's the same topic, but
the issue is a little different.
With the ID field, I'm wanting to create a unique ID for each new
submission. I was originally using auto_increment, but the problem is
that when I remove a row, I do not want the ID to be reused.
I originally considering added a "status" field, but would rather not
do that since I would eventually end up with a lot of wasted space
with rows that are marked as dead.
Is there a logical solution to this? I started to use:
$id = $dbh->selectcol_arrayref("SELECT MAX(id) FROM subjects");
$id++;
But this isn't working quite right, for 2 reasons.
1. it's not finding the maximum number in the ID field; it's finding
the largest number in the entire table. I'm not 100% sure why at this
point, but before I spend a lot of time in figuring it out I wanted to
make sure that there's not a better solution.
2. if I remove the last row, which would have had the maximum ID, then
that ID would be reused... which I don't want.
TIA,
Jason
------------------------------
Date: Sun, 29 Jul 2007 18:04:28 -0000
From: Rick <rick.peng.du@gmail.com>
Subject: question on reference to array slice
Message-Id: <1185732268.029997.319430@k79g2000hse.googlegroups.com>
Hi, guys
In C we can store a set of pointers which point to different arrays
into an array of pointer. This way we could fast access different
array by looking it up in this pointer array. Is there anyway to do
this in Perl. I've been trying different things and no luck so far.
LIke:
@nums = (1 .. 20);
I want to store the reference to @nums[1..10] into $data[0], and the
reference to @nums[11:20] into $data[1], so that I could visit, for
example, the second set of @nums by something like $data[1][5] to get
15.
Is this possible for Perl anyway?
Any input is really appreciated!!
-PD
------------------------------
Date: Sun, 29 Jul 2007 20:33:20 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: question on reference to array slice
Message-Id: <5h451uF3imupgU1@mid.individual.net>
Rick wrote:
> In C we can store a set of pointers which point to different arrays
> into an array of pointer. This way we could fast access different
> array by looking it up in this pointer array. Is there anyway to do
> this in Perl. I've been trying different things and no luck so far.
>
> LIke:
> @nums = (1 .. 20);
>
> I want to store the reference to @nums[1..10] into $data[0], and the
> reference to @nums[11:20] into $data[1], so that I could visit, for
> example, the second set of @nums by something like $data[1][5] to get
> 15.
>
> Is this possible for Perl anyway?
my @nums = 1 .. 20;
my @data;
for ( my $i=0; $i<=$#nums; $i=$i+10 ) { # assigns references to
push @data, [ @nums[$i..$i+9] ]; # copies of respective
} # array slice to @data
print $data[1][5]; # prints 16
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Sun, 29 Jul 2007 20:58:55 +0200
From: Mirco Wahab <wahab-mail@gmx.de>
Subject: Re: question on reference to array slice
Message-Id: <f8iocr$m4j$1@mlucom4.urz.uni-halle.de>
Rick wrote:
> In C we can store a set of pointers which point to different arrays
> into an array of pointer. This way we could fast access different
> array by looking it up in this pointer array. Is there anyway to do
> this in Perl. I've been trying different things and no luck so far.
>
> LIke:
> @nums = (1 .. 20);
>
> I want to store the reference to @nums[1..10] into $data[0], and the
> reference to @nums[11:20] into $data[1], so that I could visit, for
> example, the second set of @nums by something like $data[1][5] to get
> 15.
>
> Is this possible for Perl anyway?
No
You can't do that in Perl because "arrays"
aren't continous memory blocks (C/C++),
but rather _already_ pointer arrays
(pointers to elements as array values)
with a somehow complicated management
built around.
Furthermore, you can't "jump" in the
middle of something arraylike and
expect to find a mechanism there
which treats the rest as a Perl
array.
The only way to come close is the
_copying_ of an array element range
(slice) into a new (anonymous) array,
as Gunnar has already shown.
Regards
M.
------------------------------
Date: Sun, 29 Jul 2007 21:09:35 GMT
From: "John W. Krahn" <dummy@example.com>
Subject: Re: question on reference to array slice
Message-Id: <jm7ri.63932$tB5.37576@edtnps90>
Rick wrote:
>
> In C we can store a set of pointers which point to different arrays
> into an array of pointer. This way we could fast access different
> array by looking it up in this pointer array. Is there anyway to do
> this in Perl. I've been trying different things and no luck so far.
>
> LIke:
> @nums = (1 .. 20);
>
> I want to store the reference to @nums[1..10] into $data[0], and the
> reference to @nums[11:20] into $data[1], so that I could visit, for
> example, the second set of @nums by something like $data[1][5] to get
> 15.
$ perl -le'
@nums = 1 .. 20;
print "@nums";
$data[ 0 ] = [ \@nums[ 0 .. 9 ] ];
$data[ 1 ] = [ \@nums[ 10 .. 19 ] ];
print ${ $data[ 1 ][ 5 ] };
${ $data[ 1 ][ 5 ] } = 0;
print "@nums";
'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 17 18 19 20
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
------------------------------
Date: Mon, 30 Jul 2007 00:09:03 +0200
From: Mirco Wahab <wahab-mail@gmx.de>
Subject: Re: question on reference to array slice
Message-Id: <f8j3hb$pph$1@mlucom4.urz.uni-halle.de>
John W. Krahn wrote:
> $data[ 0 ] = [ \@nums[ 0 .. 9 ] ];
> $data[ 1 ] = [ \@nums[ 10 .. 19 ] ];
This usage of slice reference is something I
didn't consider. So Bellovins word was right
again: "any software problem can be solved by
adding another layer of indirection."
"anonymous slice reference" - thanks
for pointing me on this.
Regards
M.
------------------------------
Date: Mon, 30 Jul 2007 01:02:23 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: question on reference to array slice
Message-Id: <5h4kqdF3iq3f6U1@mid.individual.net>
Mirco Wahab wrote:
> John W. Krahn wrote:
>> $data[ 0 ] = [ \@nums[ 0 .. 9 ] ];
>> $data[ 1 ] = [ \@nums[ 10 .. 19 ] ];
>
> This usage of slice reference is something I
> didn't consider. So Bellovins word was right
> again: "any software problem can be solved by
> adding another layer of indirection."
>
> "anonymous slice reference"
AFAIU, they don't really refer to the slice, but rather to arrays which
in turn refer to the individual elements.
C:\>perl -MData::Dumper -e "@nums=1..20;print Dumper [ \@nums[0..9] ]"
$VAR1 = [
\1,
\2,
\3,
\4,
\5,
\6,
\7,
\8,
\9,
\10
];
C:\>
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Sun, 29 Jul 2007 17:10:36 -0700
From: "attn.steven.kuo@gmail.com" <attn.steven.kuo@gmail.com>
Subject: Re: question on reference to array slice
Message-Id: <1185754236.950809.290310@z28g2000prd.googlegroups.com>
On Jul 29, 11:58 am, Mirco Wahab <wahab-m...@gmx.de> wrote:
> Rick wrote:
> > In C we can store a set of pointers which point to different arrays
> > into an array of pointer. This way we could fast access different
> > array by looking it up in this pointer array. Is there anyway to do
> > this in Perl. I've been trying different things and no luck so far.
>
> > LIke:
> > @nums = (1 .. 20);
>
> > I want to store the reference to @nums[1..10] into $data[0], and the
> > reference to @nums[11:20] into $data[1], so that I could visit, for
> > example, the second set of @nums by something like $data[1][5] to get
> > 15.
>
> > Is this possible for Perl anyway?
>
> No
>
> You can't do that in Perl because "arrays"
> aren't continous memory blocks (C/C++),
> but rather _already_ pointer arrays
> (pointers to elements as array values)
> with a somehow complicated management
> built around.
>
> Furthermore, you can't "jump" in the
> middle of something arraylike and
> expect to find a mechanism there
> which treats the rest as a Perl
> array.
>
> The only way to come close is the
> _copying_ of an array element range
> (slice) into a new (anonymous) array,
> as Gunnar has already shown.
You can try the tie function (perldoc perltie). E.g.,
package Tie::Slice;
use Tie::Array;
@ISA = qw/Tie::Array/;
sub TIEARRAY {
my $class = shift;
my $aref = shift;
my $offset = shift;
my $length = shift || (@$aref - $offset);
return bless {
AREF => $aref,
OFFSET => $offset,
LENGTH => $length,
}, $class;
}
sub FETCH {
my ($self, $index) = @_;
if ($index >= 0) {
return $self->{AREF}->[$self->{OFFSET}+$index];
} else {
my $subtracted = $self->{OFFSET} + $self->{LENGTH};
return $self->{AREF}->[$index - $subtracted];
}
}
sub FETCHSIZE {
my ($self) = @_;
return $self->{LENGTH};
}
# no methods added for writing to array (e.g., STORE, STORESIZE,
etc.).
package main;
use strict;
use warnings;
my @nums = ( 1 .. 20 );
my (@first_half, @second_half);
tie @first_half, 'Tie::Slice', \@nums, 0, 10;
tie @second_half, 'Tie::Slice', \@nums, 10;
my @data = (\@first_half, \@second_half);
print $data[1][5];
__END__
Note that changes made to @nums will be automatically
reflected in @data (but not visa-versa since I've
only implemented the methods needed for "reading" in
Tie::Slice).
--
Regards,
Steven
------------------------------
Date: Sun, 29 Jul 2007 19:22:10 -0700
From: Paul Lalli <mritty@gmail.com>
Subject: Re: question on reference to array slice
Message-Id: <1185762130.939757.263120@z24g2000prh.googlegroups.com>
On Jul 29, 6:09 pm, Mirco Wahab <wahab-m...@gmx.de> wrote:
> John W. Krahn wrote:
> > $data[ 0 ] = [ \@nums[ 0 .. 9 ] ];
> > $data[ 1 ] = [ \@nums[ 10 .. 19 ] ];
>
> This usage of slice reference is something I
> didn't consider. So Bellovins word was right
> again: "any software problem can be solved by
> adding another layer of indirection."
>
> "anonymous slice reference" - thanks
> for pointing me on this.
I wouldn't call it a slice reference. An array slice is a list. The
\ operator applied to a list returns a list of references of the items
in that list. That is:
my @refs = \($a, $b, $c);
is the same as
my @refs = (\$a, \$b, \$c);
So in John's example, you're assigning $data[0] to be a reference to
an array which contains references to each of the first ten elements
of @nums. There is nothing intrinsic about $data[0][2] that
identifies it as a reference to the element immediately prior to
$nums[3]. It's just a list of references to values that happened to
all come from the same array.
Paul Lalli
------------------------------
Date: Mon, 30 Jul 2007 00:38:37 +0200 (CEST)
From: Stefano Sabatini <stefano.sabatini-lala@poste.it>
Subject: Reading from stdin then launching a program that reads from stdin strange behaviour
Message-Id: <slrnfaq5e6.cu8.stefano.sabatini-lala@santefisi.caos.org>
On 2007-07-28, Peter J. Holzer <hjp-usenet2@hjp.at> wrote:
> On 2007-07-27 10:50, Stefano Sabatini <stefano.sabatini-lala@poste.it> wrote:
>> I'm writing a perl script which reads from stdin, then launch an
>> interactive session of gnuplot. I'm on a gnu-linux system, perl 5.8.
> [...]
>> If I feed the script with a file, like this:
>> cat file.txt | perl gnuplot-launcher.pl
>>
>> gnuplot exits immediately from the interactive session.
>>
>> If I filter a regular file (like "file.txt") or type interactively on
>> stdin like this:
>> cat - | gnuplot-launcher.pl
>> I'm writing
>> interactively
>> on
>> stdin
>> ^D
>>
>> then the gnuplot interactive mode seems to work.
>
> I don't see how this can work. It's completely equivalent to the
> previous example, as far as gnuplot-launcher.pl is concerned:
Yes you're right, indeed I tried it again and it didn't work, sorry
for my error.
> cat reads from stdin until it a read returns 0 bytes (this happens on
> EOF on a file, or when the user types ^D on a terminal). It copies all
> input to the pipe, then exits.
>
> gnuplot-launcher.pl reads from the pipe until the write-end of the pipe
> is closed (i.e. cat has exited). Then it launches "gnuplot -", which
> will still try to read from a pipe with no writer, so it gets EOF
> immediately and exits.
>
> Now invoking gnuplot-launcher.pl with stdin from a terminal is a bit
> different. In this case it will read until the user presses ^D, then
> launch gnuplot, which will again run until the user presses ^D. This is
> because a terminal doesn't really have an "end of file", it can only
> return 0 bytes to a read which is interpreted as end of file by most
> programs - and it can do that as often as necessary, of course.
>
>> All I want to do is to be able to access to an interactive session of
>> gnuplot from perl, but I'm evidently missing something.
>
> open(STDIN, '<', '/dev/tty') or die "cannot open /dev/tty: $!";
>
> should work if the process has a controlling tty (otherwise it gets
> tricky: You could start gnuplot in an xterm if $DISPLAY is set).
Thank you Peter for the indepth analysis, using /dev/tty seems like a
good idea.
I think the key to understand the problem is to understand how a perl
script interprets its input.
In the case:
cat - | filter.pl
(or more in general in the case prog | filter.pl) the input is the
pipe output of the program which feeds the input pipe, when it sends
EOF then the STDIN filehandler is closed, and other reads from it are
doomed to get an immediate EOF and exit.
In the case:
filter.pl
STDIN is defined as a sort of alias to /dev/tty if the script is
launched in a terminal, and the script reads from it up to the first
^D, but doesn't close it afterwhile, so successive reads will result
in accepting the input from the terminal (interactively typed by the
user).
In the case of the gnuplot-launcher.pl the correct solution seems to
me:
# diamond operator: reads from input, which can be both the output end
# of a pipe, a file specified in @ARGV, or /dev/tty
while (<>) {
print $_;
}
# at this point it could close STDIN, in the case it is the output end
# of a pipe, and successive reads will fail.
# we need to redefine STDIN as the terminal ouput, since
# gnuplot inherits the STDIN filehandler from his parent,
# the perl script
open (STDIN, "</dev/tty") or die "Cannot open /dev/tty";
system("gnuplot -") == 0 or die "Gnuplot error...: $!"
This works in both the "prog | gnuplot-launcher.pl" and
"gnuplot-launcher.pl" or "gnuplot-launcher.pl file" case.
Hope my analisys is not too far from the truth.
Thank you all for your help.
Cheers!
--
Stefano Sabatini
Linux user number 337176 (see http://counter.li.org)
------------------------------
Date: Sun, 29 Jul 2007 10:49:37 -0700
From: Bill H <bill@ts1000.us>
Subject: Re: Variable dump
Message-Id: <1185731377.565921.297930@d55g2000hsg.googlegroups.com>
On Jul 29, 10:29 am, anno4...@radom.zrz.tu-berlin.de wrote:
> Bill H <b...@ts1000.us> wrote in comp.lang.perl.misc:
>
> > Is there a way in perl to dump all the variables used and their
> > current values to a file without printing everyone of them
> > individually?
>
> Not easily. You'd have a chance with package variables. For
> lexicals you'd need something like PadWalker, which is a bit
> tricky to use.
>
> Most people settle for printing selected values at selected
> places for debugging. The module Data::Dumper helps with
> viewing complex data structures.
>
That was what I thought I would need to do, but since perl has so much
in it I thought maybe it would do this too.
Bill H
------------------------------
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.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
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 V11 Issue 698
**************************************