[27470] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 9083 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Mar 24 09:05:52 2006

Date: Fri, 24 Mar 2006 06:05:06 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 24 Mar 2006     Volume: 10 Number: 9083

Today's topics:
    Re: Code review request. <zen13097@zen.co.uk>
    Re: Code review request. <a24061@yahoo.com>
    Re: Code review request. <tadmc@augustmail.com>
    Re: Great JAPH (was Re: Creating Graphs Dynamically wit jussi.mononen-asdf@asdf-comptel.com
    Re: More help requested on permutation code. <jack@abc.net>
    Re: More help requested on permutation code. <someone@example.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 24 Mar 2006 08:59:50 GMT
From: Dave Weaver <zen13097@zen.co.uk>
Subject: Re: Code review request.
Message-Id: <4423b506$0$5190$db0fefd9@news.zen.co.uk>

John W. Krahn <someone@example.com> wrote:
>  Dave Weaver wrote:
> > Michael Press <jack@abc.net> wrote:
> >>
> >>     printf ("%s\n", join ':', @line), "\n";
> > 
> > 'printf (...) interpreted as function at - line 18.'
> > 
> > Why enable warnings, only to ignore them?
> > 
> >        printf "%s\n", join(':', @tokens), "\n";     #(untested)
> 
>  You have a somewhat similar problem to the OP's code in that the ', "\n"' at
>  the end is not doing anything (there is no format in the format string that
>  uses that value.)

Oops.
Thank goodness for the "(untested)" caveat. :-/

You're quite right. It should be:

        printf "%s\n\n", join(':', @tokens);

or, if you have a distaste for parentheses (which I think, in this
case, help to make things clearer):

        printf "%s\n\n", join ':', @tokens;



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

Date: Fri, 24 Mar 2006 09:20:57 +0000
From: Adam Funk <a24061@yahoo.com>
Subject: Re: Code review request.
Message-Id: <p8qcf3-8io.ln1@news.ducksburg.com>

On 2006-03-23, Tad McClellan <tadmc@augustmail.com> wrote:
> Adam Funk <a24061@yahoo.com> wrote:
>> On 2006-03-23, Dave Weaver <zen13097@zen.co.uk> wrote:
>> 
>>> With modern perls "use warnings" is better than -w
>> 
>> I didn't know that -- what's the difference?
>
> The first sentence of the description in:
>    perldoc warnings
> tells the difference.

--> The "warnings" pragma is a replacement for the command line
--> flag "-w", but the pragma is limited to the enclosing block,
--> while the flag is global. See perllexwarn for more
--> information.

At first, I thought that meant that putting "use warnings" at the
beginning (more or less right after "#!/usr/bin/perl") of a
free-standing Perl program would have the same effect as putting "-w"
on the first line, but on reflection I think that "use warnings"
wouldn't percolate down into module code used by the main program --
is that correct?



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

Date: Fri, 24 Mar 2006 06:49:35 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Code review request.
Message-Id: <slrne27qmv.3rd.tadmc@magna.augustmail.com>

Adam Funk <a24061@yahoo.com> wrote:
> On 2006-03-23, Tad McClellan <tadmc@augustmail.com> wrote:
>> Adam Funk <a24061@yahoo.com> wrote:
>>> On 2006-03-23, Dave Weaver <zen13097@zen.co.uk> wrote:
>>> 
>>>> With modern perls "use warnings" is better than -w
>>> 
>>> I didn't know that -- what's the difference?
>>
>> The first sentence of the description in:
>>    perldoc warnings
>> tells the difference.
> 
> --> The "warnings" pragma is a replacement for the command line
> --> flag "-w", but the pragma is limited to the enclosing block,
> --> while the flag is global. See perllexwarn for more
> --> information.
> 
> At first, I thought that meant that putting "use warnings" at the
> beginning (more or less right after "#!/usr/bin/perl") of a
> free-standing Perl program would have the same effect as putting "-w"
> on the first line, but on reflection I think that "use warnings"
> wouldn't percolate down into module code used by the main program --
> is that correct?


Yes, that is correct.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Fri, 24 Mar 2006 13:35:21 +0000 (UTC)
From: jussi.mononen-asdf@asdf-comptel.com
Subject: Re: Great JAPH (was Re: Creating Graphs Dynamically with Perl)
Message-Id: <e00sip$65r$1@phys-news4.kolumbus.fi>

>> // >  Abigail <abigail@abigail.nl> wrote in
>> // >  news:slrne1eb9n.1v0.abigail@alexandra.abigail.nl: 
>> // > 
>> // > > #!/opt/perl/bin/perl   --    # No trailing newline after the last line! 
>> // > > BEGIN{$|=$SIG{__WARN__}=sub{$_=$_[0];y-_--;print/(.)"$/;seek _,-open(_ 
>> // > > ,"+<$0"),2;truncate _,tell _;close _;exec$0}}//rekcaH_lreP_rehtona_tsuJ 
>> // > 
>> // >  This is an amazing JAPH. Very instructive.
>> //  
>> //  Seconded!
>> 
>> The fun is executing the file *twice*.
> 
> That was the original reason I thought this one was extremely impressive.
> 
> It sure is hard to read ;-)

Thank god for Deparse, we mere mortals would be lost without it.

[tarzan ~]$ perl -MO=Deparse japh.pl
sub BEGIN {
    $| = $SIG{'__WARN__'} = sub {
        $_ = $_[0];
        tr/_/ /;
        print /(.)"$/;
        seek _, -open(_, "+<$0"), 2;
        truncate _, tell _;
        close _;
        exec $0;
    }
    ;
}
//;
japh.pl syntax OK

That makes much more sense :-)

/jUSSi

-- 
	remove -asdf and asdf- for email address



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

Date: Fri, 24 Mar 2006 08:55:11 GMT
From: Michael Press <jack@abc.net>
Subject: Re: More help requested on permutation code.
Message-Id: <jack-0990C3.00551124032006@newsclstr02.news.prodigy.com>

In article <aHLUf.1707$B_1.825@edtnps89>,
 "John W. Krahn" <someone@example.com> wrote:

> Michael Press wrote:

[...]

> >     for (my $idx = $#token_list; $idx >= 0; --$idx)
> >     {
> >         my $it  = $token_list[$idx];
> 
> In Perl that is usually written as:
> 
>       for my $it ( reverse @token_list ) {

I chose not to use `reverse' because I do not want a reversed list, 
and reversing a list takes time. Yes? Or does Perl understand 
the context and simply feed me the elements in reverse order?

[...]

> >             $t = $prev, $prev = $permutation_map{$it}, $permutation_map{$it} = $t;
> 
> In Perl that is usually written as:
> 
>           ( $prev, $permutation_map{$it} ) = ( $permutation_map{$it}, $prev );

I knew that. :)

[...]

> >         next if $permutation_map{$key} =~ m/-$/ ;
> 
> It _may_ be better to use substr() there (YMMV):
> 
>           next if substr( $permutation_map{$key}, -1 ) eq '-';

Noted. This is preferable for me and my style. 

[...]

> >         push @cycles, [@element_list];
> >     }
> >     for my $key (keys %permutation_map) {$permutation_map{$key} =~ tr/-//d }
> 
> In Perl that is usually written as:
> 
>       tr/-//d for values %permutation_map;

Nifty. Another Perl right to left pipeline. 


[...]

> >     print join(' ',  map { sprintf "(%s)", join ' ', @{$_}} sort {$#{$a} <=> $#{$b}} @cycles ), "\n";
> 
> Unless you have changed the value of the $" variable you could write that as:
> 
>       print join( ' ', map "(@$_)", sort { @$a <=> @$b } @cycles ), "\n";
> 
> Or, to extend that to the next level:  :-)
> 
>       print "@{[ map "(@$_)", sort { @$a <=> @$b } @cycles ]}\n";

So what is with the square brackets? 
Capture the list with a reference, 
then dereference in the print statement to interpolate $".
Yes?

[...]

I also see the 

      print "@{[ map qq[(@$_)], sort { @$a <=> @$b } @cycles ]}\n";

from your follow up article. Elegant. 

Looking at other folks exemplary code is valuable. 
Seeing changes to code that I worked over is liberating. 
Thanks.

-- 
Michael Press


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

Date: Fri, 24 Mar 2006 11:04:39 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: More help requested on permutation code.
Message-Id: <bnQUf.4717$K11.2685@clgrps12>

Michael Press wrote:
> In article <aHLUf.1707$B_1.825@edtnps89>,
>  "John W. Krahn" <someone@example.com> wrote:
> 
>>Michael Press wrote:
> 
> [...]
> 
>>>    for (my $idx = $#token_list; $idx >= 0; --$idx)
>>>    {
>>>        my $it  = $token_list[$idx];
>>In Perl that is usually written as:
>>
>>      for my $it ( reverse @token_list ) {
> 
> I chose not to use `reverse' because I do not want a reversed list, 
> and reversing a list takes time. Yes? Or does Perl understand 
> the context and simply feed me the elements in reverse order?

I was just presenting the usual Perl idiom.  Your way may in fact be better.
:-)

>>>        push @cycles, [@element_list];
>>>    }
>>>    for my $key (keys %permutation_map) {$permutation_map{$key} =~ tr/-//d }
>>In Perl that is usually written as:
>>
>>      tr/-//d for values %permutation_map;
> 
> Nifty. Another Perl right to left pipeline. 

Not a pipeline, a for loop just like:

      for ( values %permutation_map ) { tr/-//d }

but using the for statement modifier.  A pipeline would imply something on the
left to collect the modified values:

@permutation_map{ keys %permutation_map } = map { tr/-//d; $_ } values
%permutation_map;

BTW, because hash keys cannot be modified, you could (but probably shouldn't)
write it like this:

     tr/-//d for %permutation_map;

>>>    print join(' ',  map { sprintf "(%s)", join ' ', @{$_}} sort {$#{$a} <=> $#{$b}} @cycles ), "\n";
>>Unless you have changed the value of the $" variable you could write that as:
>>
>>      print join( ' ', map "(@$_)", sort { @$a <=> @$b } @cycles ), "\n";
>>
>>Or, to extend that to the next level:  :-)
>>
>>      print "@{[ map "(@$_)", sort { @$a <=> @$b } @cycles ]}\n";
> 
> So what is with the square brackets? 
> Capture the list with a reference, 
> then dereference in the print statement to interpolate $".
> Yes?

perldoc perlref
[snip]
       Here's a trick for interpolating a subroutine call into a string:

           print "My sub returned @{[mysub(1,2,3)]} that time.\n";

       The way it works is that when the "@{...}" is seen in the double-quoted
       string, it's evaluated as a block.  The block creates a reference to an
       anonymous array containing the results of the call to "mysub(1,2,3)".
       So the whole block returns a reference to an array, which is then
       dereferenced by "@{...}" and stuck into the double-quoted string. This
       chicanery is also useful for arbitrary expressions:

           print "That yields @{[$n + 5]} widgets\n";

> I also see the 
> 
>       print "@{[ map qq[(@$_)], sort { @$a <=> @$b } @cycles ]}\n";
> 
> from your follow up article. Elegant. 
> 
> Looking at other folks exemplary code is valuable. 
> Seeing changes to code that I worked over is liberating. 

Those are just suggestions.  Benchmark/profile to determine efficiency of any
code.  :-)


John
-- 
use Perl;
program
fulfillment


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

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


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