[32369] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3636 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Mar 10 21:09:24 2012

Date: Sat, 10 Mar 2012 18:09:07 -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           Sat, 10 Mar 2012     Volume: 11 Number: 3636

Today's topics:
        First Commercial Perl Program <ronaldljohnson@gmail.com>
    Re: First Commercial Perl Program <ben@morrow.me.uk>
    Re: First Commercial Perl Program <ronaldljohnson@gmail.com>
    Re: First Commercial Perl Program (Randal L. Schwartz)
    Re: how to permute multi arrays with different numbers  <m@rtij.nl.invlalid>
    Re: how to permute multi arrays with different numbers  <kiuhnm03.4t.yahoo.it>
    Re: how to permute multi arrays with different numbers  <kiuhnm03.4t.yahoo.it>
    Re: how to permute multi arrays with different numbers  <rvtol+usenet@xs4all.nl>
    Re: how to permute multi arrays with different numbers  <rvtol+usenet@xs4all.nl>
    Re: how to permute multi arrays with different numbers  <ben@morrow.me.uk>
    Re: how to permute multi arrays with different numbers  xhoster@gmail.com
    Re: how to permute multi arrays with different numbers  <rvtol+usenet@xs4all.nl>
    Re: how to permute multi arrays with different numbers  <rvtol+usenet@xs4all.nl>
    Re: how to permute multi arrays with different numbers  <ben@morrow.me.uk>
    Re: how to permute multi arrays with different numbers  <kiuhnm03.4t.yahoo.it>
    Re: how to permute multi arrays with different numbers  <rvtol+usenet@xs4all.nl>
    Re: how to permute multi arrays with different numbers  <hjp-usenet2@hjp.at>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sat, 10 Mar 2012 11:18:20 -0800 (PST)
From: "tbb!/fbr!" <ronaldljohnson@gmail.com>
Subject: First Commercial Perl Program
Message-Id: <f12da94d-608e-48a6-87d8-038ee7ede978@x5g2000pbl.googlegroups.com>

I entered the professional perl programming world by being paid
(that's what I call professional, though the code may be far from)
for
a very small perl script. The user basically wanted a config file
which contained as the first line a username, the second line a
password, and the remaining lines to be hotnames.
ex.
user
pass
127.0.0.1
127.0.0.2
Then I wrote the following script. It gathers the user, pass, and
hostlist, and then establishes an ssh connection to query a 'device'
and return the output in a file named after the host. Following is
that program:
#!/usr/bin/perl
# Code by
# For
# dmon-1.6
use warnings;
use strict;
use Net::SSH::Perl;
my $cfgfile="./config";
open CONFIG, "<", $cfgfile || die $!;
chomp(my @cfgdat=(<CONFIG>));
my $user=shift(@cfgdat);
my $pass=shift(@cfgdat);
my $extcmd="ls -l";
my $stime=3;
while (defined $stime) {
  foreach (@cfgdat) {
    my $ssh=Net::SSH::Perl->new($_);
    $ssh->login($user,$pass);
    my ($stdout,$stderr,$exit)=$ssh->cmd($extcmd);
    open OUTFILE, ">>", $_ || die $!;
    if ($stdout) {
      print OUTFILE $stdout;
    }
    if ($stderr) {
      print OUTFILE $stderr;
    }
    close OUTFILE;
  }
  sleep $stime;
}

I am just looking for critique. I have been a Unix Admin for over 15
years, and have used perl for one off scripts, but I spent time and
master Oreillys Learning Perl and Intermediate Perl (Mastering and
Advanced Perl are next) and am now looking to solely become a
commercial perl programmer. However, as I lack commercial experience,
I probably lack a 'standard' way of approaching things, or at least
don't know what experienced perl programmers know, which I'll learn
as
a function of time. Either way, if you have time, let me know how I
could have done all this better, and maybe even a source of
commercial
perl programs I can look at and see how pro's do it.
Ron


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

Date: Sat, 10 Mar 2012 22:10:47 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: First Commercial Perl Program
Message-Id: <740t29-eh41.ln1@anubis.morrow.me.uk>


Quoth "tbb!/fbr!" <ronaldljohnson@gmail.com>:
>
> #!/usr/bin/perl
> # Code by
> # For
> # dmon-1.6
> use warnings;
> use strict;

Good.

> use Net::SSH::Perl;
> my $cfgfile="./config";
> open CONFIG, "<", $cfgfile || die $!;
               ^^ Good          ^^ Good

That '||' doesn't do what you think it does: || has higher precedence
than the RHS of open, so you are testing $cfgfile rather than the return
value of open. You want either

    open(CONFIG, "<", $cfgfile) || die $!;

or

    open CONFIG, "<", $cfgfile or die $!;

The latter would be standard practice.

Best practice is to use variables to hold filehandles rather than global
barewords, and it's always a good idea to make error messages
descriptive, so I would write that

    open my $CONFIG, "<", $cfgfile 
        or die "can't read '$cfgfile': $!";

If you are using perl 5.10 you can also put
    
    use autodie;

up at the top and it will handle all these error checks for you.

> chomp(my @cfgdat=(<CONFIG>));

If $CONFIG becomes a variable this needs to change to match

    chomp(my @cfgdat = <$CONFIG>);

> my $user=shift(@cfgdat);
> my $pass=shift(@cfgdat);
> my $extcmd="ls -l";
> my $stime=3;
> while (defined $stime) {

I don't see you change $stime anywhere, so this will loop forever.

>   foreach (@cfgdat) {
>     my $ssh=Net::SSH::Perl->new($_);
>     $ssh->login($user,$pass);
>     my ($stdout,$stderr,$exit)=$ssh->cmd($extcmd);
>     open OUTFILE, ">>", $_ || die $!;

As above, with the 'or' and the 'my $OUTFILE'.

>     if ($stdout) {
>       print OUTFILE $stdout;
>     }

I would probably have written that

    $stdout and print $OUTFILE $stdout;

or even combined the two and written

    $_ and print $OUTFILE $_
        for $stdout, $stderr;

but you would be perfectly entitled to consider that a little obscure.
There's always a trade-off between writing for those who know the
language well (and find the shortcuts make it easier to see through to
the logic) and writing for those who don't (and find the shortcuts
confusing).

>     if ($stderr) {
>       print OUTFILE $stderr;
>     }
>     close OUTFILE;

If you are writing to a filehandle, particularly for something like this
that's likely to run unattended, you should check the return value of
'close' to make sure everything was written OK. There isn't any need to
check the individual 'print's: because output is buffered, the error for
a particular 'print' may not show up until later, but perl guarantees
that if there was an error at any point it will be returned from
'close'.

    close $OUTFILE or die "can't write '$_': $!";

(or use autodie, as above).

>   }
>   sleep $stime;
> }
> 
> I am just looking for critique.

That's a great deal better than most first posts here :).

> Either way, if you have time, let me know how I
> could have done all this better, and maybe even a source of
> commercial
> perl programs I can look at and see how pro's do it.

I don't know about 'commercial', but there's a lot of Perl code
distributed with the perl core, and a whole lot more on the CPAN. Some
of it is good, some bad, most indifferent, but there's nearly always
something to be learned from seeing how other people have solved similar
problems in the past (even if sometimes what you learn is 'don't do it
like that'). 

IMHO the only things that really matter are first, making sure you've
solved the whole problem, including any nasty corner cases, and second, 
that you read over the code after you've written it, just as you would a
piece of English, and make sure it makes the logic as clear as it can.

Ben



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

Date: Sat, 10 Mar 2012 15:48:36 -0800 (PST)
From: "tbb!/fbr!" <ronaldljohnson@gmail.com>
Subject: Re: First Commercial Perl Program
Message-Id: <b115cad2-c24a-4c56-b511-b1737943a2fc@s10g2000pbo.googlegroups.com>

On Mar 10, 2:10=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth "tbb!/fbr!" <ronaldljohn...@gmail.com>:
>
>
>
> > #!/usr/bin/perl
> > # Code by
> > # For
> > # dmon-1.6
> > use warnings;
> > use strict;
>
> Good.
>
> > use Net::SSH::Perl;
> > my $cfgfile=3D"./config";
> > open CONFIG, "<", $cfgfile || die $!;
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0^^ Good =A0 =A0 =A0 =A0 =A0^^ Good
>
> That '||' doesn't do what you think it does: || has higher precedence
> than the RHS of open, so you are testing $cfgfile rather than the return
> value of open. You want either
>
> =A0 =A0 open(CONFIG, "<", $cfgfile) || die $!;
>
> or
>
> =A0 =A0 open CONFIG, "<", $cfgfile or die $!;
>
> The latter would be standard practice.
>
> Best practice is to use variables to hold filehandles rather than global
> barewords, and it's always a good idea to make error messages
> descriptive, so I would write that
>
> =A0 =A0 open my $CONFIG, "<", $cfgfile
> =A0 =A0 =A0 =A0 or die "can't read '$cfgfile': $!";
>
> If you are using perl 5.10 you can also put
>
> =A0 =A0 use autodie;
>
> up at the top and it will handle all these error checks for you.
>
> > chomp(my @cfgdat=3D(<CONFIG>));
>
> If $CONFIG becomes a variable this needs to change to match
>
> =A0 =A0 chomp(my @cfgdat =3D <$CONFIG>);
>
> > my $user=3Dshift(@cfgdat);
> > my $pass=3Dshift(@cfgdat);
> > my $extcmd=3D"ls -l";
> > my $stime=3D3;
> > while (defined $stime) {
>
> I don't see you change $stime anywhere, so this will loop forever.
>
> > =A0 foreach (@cfgdat) {
> > =A0 =A0 my $ssh=3DNet::SSH::Perl->new($_);
> > =A0 =A0 $ssh->login($user,$pass);
> > =A0 =A0 my ($stdout,$stderr,$exit)=3D$ssh->cmd($extcmd);
> > =A0 =A0 open OUTFILE, ">>", $_ || die $!;
>
> As above, with the 'or' and the 'my $OUTFILE'.
>
> > =A0 =A0 if ($stdout) {
> > =A0 =A0 =A0 print OUTFILE $stdout;
> > =A0 =A0 }
>
> I would probably have written that
>
> =A0 =A0 $stdout and print $OUTFILE $stdout;
>
> or even combined the two and written
>
> =A0 =A0 $_ and print $OUTFILE $_
> =A0 =A0 =A0 =A0 for $stdout, $stderr;
>
> but you would be perfectly entitled to consider that a little obscure.
> There's always a trade-off between writing for those who know the
> language well (and find the shortcuts make it easier to see through to
> the logic) and writing for those who don't (and find the shortcuts
> confusing).
>
> > =A0 =A0 if ($stderr) {
> > =A0 =A0 =A0 print OUTFILE $stderr;
> > =A0 =A0 }
> > =A0 =A0 close OUTFILE;
>
> If you are writing to a filehandle, particularly for something like this
> that's likely to run unattended, you should check the return value of
> 'close' to make sure everything was written OK. There isn't any need to
> check the individual 'print's: because output is buffered, the error for
> a particular 'print' may not show up until later, but perl guarantees
> that if there was an error at any point it will be returned from
> 'close'.
>
> =A0 =A0 close $OUTFILE or die "can't write '$_': $!";
>
> (or use autodie, as above).
>
> > =A0 }
> > =A0 sleep $stime;
> > }
>
> > I am just looking for critique.
>
> That's a great deal better than most first posts here :).
>
> > Either way, if you have time, let me know how I
> > could have done all this better, and maybe even a source of
> > commercial
> > perl programs I can look at and see how pro's do it.
>
> I don't know about 'commercial', but there's a lot of Perl code
> distributed with the perl core, and a whole lot more on the CPAN. Some
> of it is good, some bad, most indifferent, but there's nearly always
> something to be learned from seeing how other people have solved similar
> problems in the past (even if sometimes what you learn is 'don't do it
> like that').
>
> IMHO the only things that really matter are first, making sure you've
> solved the whole problem, including any nasty corner cases, and second,
> that you read over the code after you've written it, just as you would a
> piece of English, and make sure it makes the logic as clear as it can.
>
> Ben

Yeah, the $stime thing was originally a while (1) loop. $stime was
made variable for the owner of the script so that he can change how
often the loop runs. The infinite loop was by design.

Points taken on || vs. or and how it's properly evaluated.

I was originally getting error messages on the print OUTFILES when
either $stdout or $stderr wasn't set (always an error, though i
originally ignored it, so to suppress the error of the one that wasn't
set, I quickly simply tested to see if the variable was defined and if
it was, then write out the contents. Of course both $stdout and
$stderr were being written to the same file but I'm still sort of
learning, and wasn't sure how or if I could or even should combine
those two if tests.

Will make sure I check the status of closing files in the future.

Yeah, I got paid $100 for that, and that's just the beginning. I
wanted to write the guy a routine to automatically parse $stdout,
trigger on regex match, and then alert, but they were not interested.
I guess I mine as well do it just for fun.

I really appreciate that. I'm going to make those changes.

And I really like this to get rid of those two if's:

 $_ and print $OUTFILE $_
=A0 =A0 =A0 =A0 for $stdout, $stderr;

I had to look up the contsruct, as 'Learning Perl' and 'Intermediate
Perl' by Oreilly briefly touch on shortcuts like this. I completely
understand the logic after a little further reading. I don't want to
just 'use' other people's code, at least not without fully
understanding it and being able to duplicate it on my own.

Thanks again.

Ron


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

Date: Sat, 10 Mar 2012 17:48:09 -0800
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: First Commercial Perl Program
Message-Id: <86pqcj9acm.fsf@red.stonehenge.com>

>>>>> "tbb" == tbb!/fbr!  <ronaldljohnson@gmail.com> writes:

tbb> Yeah, I got paid $100 for that, and that's just the beginning. I
tbb> wanted to write the guy a routine to automatically parse $stdout,
tbb> trigger on regex match, and then alert, but they were not
tbb> interested.

Like "swatch"? (Google for "swatch perl" for links.)

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion


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

Date: Sat, 10 Mar 2012 08:05:54 +0100
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: how to permute multi arrays with different numbers of element?
Message-Id: <i3br29-eh6.ln1@news.rtij.nl>

On Sat, 10 Mar 2012 03:07:51 +0000, Ben Morrow wrote:

> (I seem to keep finding situations where I want to be able to say
> 
>     map my $d { ... } ...
> 
> , and I believe the 'my' is enough to stop it being ambiguous. Hmmm.)

I like it. Makes perfect sense.

M4


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

Date: Sat, 10 Mar 2012 12:19:16 +0100
From: Kiuhnm <kiuhnm03.4t.yahoo.it>
Subject: Re: how to permute multi arrays with different numbers of element?
Message-Id: <4f5b38b2$0$1386$4fafbaef@reader1.news.tin.it>

On 3/10/2012 3:55, Alan Curry wrote:
> In article<jje09l$97t$1@l01news1.ot.hr>, VZD<vdz@vzd.vzd>  wrote:
>> Please help me to solve problem, how to permute multi arrays with different
>> numbers of element? I can make it manually, but what If you have more that
>> three arrays?
>>
>> Thanks
>>
>>
>> # Perl permute multidimensional arrays
>> #
>>
>> my @digits1 = qw(1 2);
>> my @digits2 = qw(1 2);
>> my @digits3 = qw(a b c);
>>
>> foreach my $i (@digits1) {
>>    foreach my $j (@digits2) {
>>       foreach my $k (@digits3) {
>>          print "$i$j$k$l\n";
>>       }
>>    }
>> }
>
> This operation is called a "Cartesian product". Knowing that, it should be
> easy to find several implementations.

It depends on what VZD really wants. Does he want the elements in that 
precise order?

Kiuhnm


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

Date: Sat, 10 Mar 2012 12:24:29 +0100
From: Kiuhnm <kiuhnm03.4t.yahoo.it>
Subject: Re: how to permute multi arrays with different numbers of element?
Message-Id: <4f5b39ec$0$1387$4fafbaef@reader1.news.tin.it>

On 3/10/2012 4:07, Ben Morrow wrote:
> (I seem to keep finding situations where I want to be able to say
>
>      map my $d { ... } ...
>
> , and I believe the 'my' is enough to stop it being ambiguous. Hmmm.)

That should apply to all list operators.

Kiuhnm


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

Date: Sat, 10 Mar 2012 16:19:40 +0100
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: how to permute multi arrays with different numbers of element?
Message-Id: <4f5b710c$0$6919$e4fe514c@news2.news.xs4all.nl>

On 2012-03-10 04:07, Ben Morrow wrote:
> Quoth "VZD"<vdz@vzd.vzd>:

>> Please help me to solve problem, how to permute multi arrays with different
>> numbers of element? I can make it manually, but what If you have more that
>> three arrays?
>
> Hmm, well, since noone's given the obvious answer yet...

Was glob too obvious?

perl -wle'print for glob "{1,2,3}{4,5,6}{7,8}"'

-- 
Ruud


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

Date: Sat, 10 Mar 2012 16:37:32 +0100
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: how to permute multi arrays with different numbers of element?
Message-Id: <4f5b753d$0$6974$e4fe514c@news2.news.xs4all.nl>

On 2012-03-10 02:15, Randal L. Schwartz wrote:
> Ruud:
 >> vdz:

>>> my @digits1 = qw(1 2);
>>> my @digits2 = qw(1 2);
>>> my @digits3 = qw(a b c);
>>
>> Numbered names raise a red flag.
>
> Some call it "code smell".  I like that.

1. Numbered names smell.
2. Numbered names are code smell.
3. Numbered names make your code smell.
4. Code with numbered names smells.

I still prefer my n-n r-r pattern.
(I would translate "pink panther" to "paarse panter",
though "pink" is normally "roze".)

-- 
Ruud


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

Date: Sat, 10 Mar 2012 16:10:50 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: how to permute multi arrays with different numbers of element?
Message-Id: <a1bs29-i501.ln1@anubis.morrow.me.uk>


Quoth "Dr.Ruud" <rvtol+usenet@xs4all.nl>:
> On 2012-03-10 04:07, Ben Morrow wrote:
> > Quoth "VZD"<vdz@vzd.vzd>:
> 
> >> Please help me to solve problem, how to permute multi arrays with
> >> different numbers of element? I can make it manually, but what If
> >> you have more that three arrays?
> >
> > Hmm, well, since noone's given the obvious answer yet...
> 
> Was glob too obvious?
> 
> perl -wle'print for glob "{1,2,3}{4,5,6}{7,8}"'

glob doesn't work with arbitrary strings. I suppose you could assume the
OP was only (ever going to be) permuting digits, but I generally dislike
solutions that involve quoting and reparsing. Besides, the question
asked for an arbtrary number of arrays, and I don't know that

    glob join "",
        map "{$_}",
        map { join ",", @$_ }
        @digits;

is much more concise than the solution I posted.

Ben



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

Date: 10 Mar 2012 16:51:18 GMT
From: xhoster@gmail.com
Subject: Re: how to permute multi arrays with different numbers of element?
Message-Id: <20120310115118.752$go@newsreader.com>

"Dr.Ruud" <rvtol+usenet@xs4all.nl> wrote:
> On 2012-03-10 04:07, Ben Morrow wrote:
> > Quoth "VZD"<vdz@vzd.vzd>:
>
> >> Please help me to solve problem, how to permute multi arrays with
> >> different numbers of element? I can make it manually, but what If you
> >> have more that three arrays?
> >
> > Hmm, well, since noone's given the obvious answer yet...
>
> Was glob too obvious?
>
> perl -wle'print for glob "{1,2,3}{4,5,6}{7,8}"'

I haven't tried this on a newer perl, but at least on older ones this
method will get slow and beat the crap out of your hard drive as the list
gets longs.  It checks whether each string exists as a filename, but the
results of that check are ignored.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.


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

Date: Sat, 10 Mar 2012 17:51:32 +0100
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: how to permute multi arrays with different numbers of element?
Message-Id: <4f5b8694$0$6877$e4fe514c@news2.news.xs4all.nl>

On 2012-03-10 17:10, Ben Morrow wrote:
> Ruud:
>> Ben:
>>> vdz:

>>>> Please help me to solve problem, how to permute multi arrays with
>>>> different numbers of element? I can make it manually, but what If
>>>> you have more that three arrays?
>>>
>>> Hmm, well, since noone's given the obvious answer yet...
>>
>> Was glob too obvious?
>>
>> perl -wle'print for glob "{1,2,3}{4,5,6}{7,8}"'
>
> glob doesn't work with arbitrary strings. I suppose you could assume the
> OP was only (ever going to be) permuting digits, but I generally dislike
> solutions that involve quoting and reparsing. Besides, the question
> asked for an arbtrary number of arrays, and I don't know that
>
>      glob join "",
>          map "{$_}",
>          map { join ",", @$_ }
>          @digits;
>
> is much more concise than the solution I posted.

Good points. It easily gets ugly:

perl -wle '
   my @digits = ( [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, "8,X" ] );
   print for glob join "",
     map "{$_}",
       map join( ",", map quotemeta, @$_ ),
         @digits;
'

-- 
Ruud


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

Date: Sat, 10 Mar 2012 18:13:25 +0100
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: how to permute multi arrays with different numbers of element?
Message-Id: <4f5b8bb5$0$6934$e4fe514c@news2.news.xs4all.nl>

On 2012-03-10 17:51, xhoster@gmail.com wrote:
> "Dr.Ruud"<rvtol+usenet@xs4all.nl>  wrote:

>> perl -wle'print for glob "{1,2,3}{4,5,6}{7,8}"'
>
> I haven't tried this on a newer perl, but at least on older ones this
> method will get slow and beat the crap out of your hard drive as the list
> gets longs.  It checks whether each string exists as a filename, but the
> results of that check are ignored.

Why do you think it would go to disk?

<quote src=`perldoc -f glob`>
   If non-empty braces are the only wildcard characters used in
   the "glob", no filenames are matched, but potentially many
   strings are returned.
</quote>

Still, see Ben's reasons to avoid glob.

-- 
Ruud


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

Date: Sat, 10 Mar 2012 17:33:43 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: how to permute multi arrays with different numbers of element?
Message-Id: <nsfs29-q711.ln1@anubis.morrow.me.uk>


Quoth "Dr.Ruud" <rvtol+usenet@xs4all.nl>:
> On 2012-03-10 17:51, xhoster@gmail.com wrote:
> >
> > I haven't tried this on a newer perl, but at least on older ones this
> > method will get slow and beat the crap out of your hard drive as the list
> > gets longs.  It checks whether each string exists as a filename, but the
> > results of that check are ignored.
> 
> Why do you think it would go to disk?
> 
> <quote src=`perldoc -f glob`>
>    If non-empty braces are the only wildcard characters used in
>    the "glob", no filenames are matched, but potentially many
>    strings are returned.
> </quote>

Let's have a look, shall we?

    ~% ktrace -tn perl -e'glob "{1,2}{3,4}"'
    ~% kdump
    <snip>
     34066 perl     NAMI  "13"
     34066 perl     NAMI  "14"
     34066 perl     NAMI  "23"
     34066 perl     NAMI  "24"
    ~%

So, yes, it goes to the disk, whether it needed to or not. (This is
5.12.2.)

Ben



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

Date: Sat, 10 Mar 2012 19:10:53 +0100
From: Kiuhnm <kiuhnm03.4t.yahoo.it>
Subject: Re: how to permute multi arrays with different numbers of element?
Message-Id: <4f5b992b$0$1379$4fafbaef@reader2.news.tin.it>

On 3/10/2012 4:07, Ben Morrow wrote:
> Hmm, well, since noone's given the obvious answer yet...
>
>      my @digits = (
>          [1, 2, 3],
>          [4, 5, 6],
>          [7, 8],
>      );
>
>      sub permute {
>          map {
>              my $d = $_;
>              @_ ? map "$d$_", permute(@_) : $d;
>          } @{ +shift };
>      }
>
>      say for permute @digits;

Here's a more efficient version:

--->
sub count {
     my $str = shift;
     for (@{+shift}) {
         @_ ? count("$str$_", @_) : say "$str$_"
     }
}

count("", @digits);
<---

Kiuhnm


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

Date: Sat, 10 Mar 2012 19:23:35 +0100
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: how to permute multi arrays with different numbers of element?
Message-Id: <4f5b9c28$0$6975$e4fe514c@news2.news.xs4all.nl>

On 2012-03-10 18:33, Ben Morrow wrote:
> Ruud:
>> xhoster:

>>> I haven't tried this on a newer perl, but at least on older ones this
>>> method will get slow and beat the crap out of your hard drive as the list
>>> gets longs.  It checks whether each string exists as a filename, but the
>>> results of that check are ignored.
>>
>> Why do you think it would go to disk?
>>
>> <quote src=`perldoc -f glob`>
>>     If non-empty braces are the only wildcard characters used in
>>     the "glob", no filenames are matched, but potentially many
>>     strings are returned.
>> </quote>
>
> Let's have a look, shall we?
>
>      ~% ktrace -tn perl -e'glob "{1,2}{3,4}"'
>      ~% kdump
>      <snip>
>       34066 perl     NAMI  "13"
>       34066 perl     NAMI  "14"
>       34066 perl     NAMI  "23"
>       34066 perl     NAMI  "24"
>      ~%
>
> So, yes, it goes to the disk, whether it needed to or not. (This is
> 5.12.2.)

Bug?

-- 
Ruud


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

Date: Sat, 10 Mar 2012 21:04:44 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: how to permute multi arrays with different numbers of element?
Message-Id: <slrnjlncus.67s.hjp-usenet2@hrunkner.hjp.at>

On 2012-03-10 17:13, Dr.Ruud <rvtol+usenet@xs4all.nl> wrote:
> On 2012-03-10 17:51, xhoster@gmail.com wrote:
>> "Dr.Ruud"<rvtol+usenet@xs4all.nl>  wrote:
>
>>> perl -wle'print for glob "{1,2,3}{4,5,6}{7,8}"'
>>
>> I haven't tried this on a newer perl, but at least on older ones this
                                                         ^^^^^^^^^^
>> method will get slow and beat the crap out of your hard drive as the list
>> gets longs.  It checks whether each string exists as a filename, but the
>> results of that check are ignored.
>
> Why do you think it would go to disk?

Because older ones *do* go to disk?

% strace perl -wle'print for glob "{1,2,3}{4,5,6}{7,8}"'
[...]
lstat64("147", 0xbf910610)              = -1 ENOENT (No such file or directory)
lstat64("148", 0xbf910610)              = -1 ENOENT (No such file or directory)
lstat64("157", 0xbf910610)              = -1 ENOENT (No such file or directory)
lstat64("158", 0xbf910610)              = -1 ENOENT (No such file or directory)
lstat64("167", 0xbf910610)              = -1 ENOENT (No such file or directory)
lstat64("168", 0xbf910610)              = -1 ENOENT (No such file or directory)
lstat64("247", 0xbf910610)              = -1 ENOENT (No such file or directory)
lstat64("248", 0xbf910610)              = -1 ENOENT (No such file or directory)
lstat64("257", 0xbf910610)              = -1 ENOENT (No such file or directory)
lstat64("258", 0xbf910610)              = -1 ENOENT (No such file or directory)
lstat64("267", 0xbf910610)              = -1 ENOENT (No such file or directory)
lstat64("268", 0xbf910610)              = -1 ENOENT (No such file or directory)
lstat64("347", 0xbf910610)              = -1 ENOENT (No such file or directory)
lstat64("348", 0xbf910610)              = -1 ENOENT (No such file or directory)
lstat64("357", 0xbf910610)              = -1 ENOENT (No such file or directory)
lstat64("358", 0xbf910610)              = -1 ENOENT (No such file or directory)
lstat64("367", 0xbf910610)              = -1 ENOENT (No such file or directory)
lstat64("368", 0xbf910610)              = -1 ENOENT (No such file or directory)
write(1, "147\n", 4147
)                    = 4
write(1, "148\n", 4148
)                    = 4
write(1, "157\n", 4157
)                    = 4
[...]

% perl -v

This is perl, v5.10.1 (*) built for i486-linux-gnu-thread-multi
(with 56 registered patches, see perl -V for more detail)

Copyright 1987-2009, Larry Wall


><quote src=`perldoc -f glob`>
>    If non-empty braces are the only wildcard characters used in
>    the "glob", no filenames are matched, but potentially many
>    strings are returned.
></quote>

This sentence isn't there in 5.10.1. If newer perls don't try to stat
the files any more (note that "no filenames are matched" doesn't
necessarily imply this), then the change was made after 5.10.1

	hp


-- 
   _  | Peter J. Holzer    | Deprecating human carelessness and
|_|_) | Sysadmin WSR       | ignorance has no successful track record.
| |   | hjp@hjp.at         | 
__/   | http://www.hjp.at/ |  -- Bill Code on asrg@irtf.org


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

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


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