[28184] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 9548 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Aug 2 14:05:43 2006

Date: Wed, 2 Aug 2006 11:05:07 -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           Wed, 2 Aug 2006     Volume: 10 Number: 9548

Today's topics:
    Re: FAQ 4.70 How do I handle binary data correctly? <benmorrow@tiscali.co.uk>
    Re: foreach aliasing, my variables, and visibility in s <benmorrow@tiscali.co.uk>
    Re: foreach aliasing, my variables, and visibility in s xhoster@gmail.com
    Re: foreach aliasing, my variables, and visibility in s <sgt19@tid.es>
    Re: foreach aliasing, my variables, and visibility in s <sgt19@tid.es>
    Re: foreach aliasing, my variables, and visibility in s <bik.mido@tiscalinet.it>
    Re: foreach aliasing, my variables, and visibility in s <benmorrow@tiscali.co.uk>
    Re: foreach aliasing, my variables, and visibility in s <benmorrow@tiscali.co.uk>
        how to "anonymize" Perl script before publishing it? <nobody@dizum.com>
    Re: how to "anonymize" Perl script before publishing it <mritty@gmail.com>
        How to "convert" a string into a variable name? <youcontrol@hispeed.ch>
    Re: How to "convert" a string into a variable name? <youcontrol@hispeed.ch>
    Re: How to "convert" a string into a variable name? <mritty@gmail.com>
    Re: How to "convert" a string into a variable name? <benmorrow@tiscali.co.uk>
    Re: perl editor <tzz@lifelogs.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 2 Aug 2006 15:54:22 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: FAQ 4.70 How do I handle binary data correctly?
Message-Id: <utq6q3-lr9.ln1@osiris.mauzo.dyndns.org>


Quoth PerlFAQ Server <brian@stonehenge.com>:
> This is an excerpt from the latest version perlfaq4.pod, which
> comes with the standard Perl distribution. These postings aim to 
> reduce the number of repeated questions as well as allow the community
> to review and update the answers. The latest version of the complete
> perlfaq is at http://faq.perl.org .
> 
> --------------------------------------------------------------------
> 
> 4.70: How do I handle binary data correctly?
> 
>     Perl is binary clean, so this shouldn't be a problem. For example, this
>     works fine (assuming the files are found):
> 
>             if (`cat /vmunix` =~ /gzip/) {
>                     print "Your kernel is GNU-zip enabled!\n";
>                     }
> 
>     On less elegant (read: Byzantine) systems, however, you have to play
>     tedious games with "text" versus "binary" files. See "binmode" in
>     perlfunc or perlopentut.
 
This entry should be updated re Unicode in 5.8. binmode is now required
on all systems, so the Unix-bigotry should be removed (not that I'm not
in favour of Unix-bigotry in general... :) ).

>     If you're concerned about 8-bit ASCII data, then see perllocale.
                                ^^^^^^^^^^^
                                You what??? :) ITYM '8-bit textual'.

Ben

-- 
If I were a butterfly I'd live for a day, / I would be free, just blowing away.
This cruel country has driven me down / Teased me and lied, teased me and lied.
I've only sad stories to tell to this town: / My dreams have withered and died.
  benmorrow@tiscali.co.uk                                          (Kate Rusby)


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

Date: Wed, 2 Aug 2006 15:49:45 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: foreach aliasing, my variables, and visibility in sub
Message-Id: <9lq6q3-lr9.ln1@osiris.mauzo.dyndns.org>


Quoth Michele Dondi <bik.mido@tiscalinet.it>:
> On Tue, 1 Aug 2006 21:27:47 +0100, Ben Morrow
> <benmorrow@tiscali.co.uk> wrote:
> 
> >> Although that's indeed what both 'perldoc -q closure' and 'perldoc
> >> perlref' claim, I regard it as a standard misconception that closures
> >> *are* anonymous subs.
> >
> >And I that a closure is simply a sub with its own scope :). They are
> 
> You what? Is the missing verb supposed to be "claim"?

Sorry, I wondered if that phrasing was unclear. The missing verb is
'regard it as a standard misconception'. Guess I should have erred on
the side of clarity :).

> >*not* the same thing: the important property of a closure is that you
> >can have *many copies* of the same sub *each with its own scope*.
> 
> Well, it could be debatable whether they are *copies* of the *same*
> sub or different subs altogether:
> 
>   $ perl -le 'print for map { my $v=$_; sub { $v } } 1..3'
>   CODE(0x814f180)
>   CODE(0x814f240)
>   CODE(0x814ff18)

Yes, they have different CVs, as they must as each has its own pad. But
the code is only compiled once, and each CV points to the same optree.

And no, it really doesn't matter whether you consider them to be
'different subs' or 'copies of the same sub' :). I guess you could
rephrase what I'm trying to say as 'taking a ref to a named sub always
returns a ref to the same sub, whereas sub {} constructs a new anon sub
each time'.

> It is largely a matter of interpretation, I guess. As far as the
> general CS concept, I don't know... but of course this is a usefulness
> of closures. And indeed an important property... however this is far
> from being a *defining* one

Yes, it is. It is *the* defining property of closures: that they close
over the lexical variables *currently present*.

> and IMNSH understanding a named sub *is* a
> closure around lexical variables defined in the sorrounding scope.

No, it isn't. Take my named subs example again

    sub mkfoo {
        my $f = shift;
        sub foo {
            return $f;
        }
        return \&foo;
    }

and call it twice

    my $x = mkfoo 1;
    my $y = mkfoo 2;
    print $x->();      # 1
    print $y->();      # 1

 . The two subs returned both refer to the *same* $f, the one that was in
scope the first time mkfoo was run. With anon subs, the two returned
subs have different $f variables, as they should have because they were
created in different scopes (each runtime pass through mkfoo creates a
new lexical scope).

That is, the second named sub didn't close over the variables in its
surrounding scope, but over some random variables in some other scope.
This is pretty much always useless behaviour, so if you see named subs
inside named subs in Perl (as opposed to named subs inside blocks, which
is fine as a top-level block is only ever run once) it's probably a bug.

This is really important when using callbacks. If you try to use a
nested named sub as a callback you'll get real problems. To take an
example from http://www.perl.com/pub/a/2002/05/29/closure.html:

    use XML::Twig;
    
    my %courses;
    
    for (<??.xml>) {
        my $name = $_; $name =~ s/.xml//;
        my $t= XML::Twig->new( 
            TwigHandlers => {
                need => sub { 
                    push @{$courses{$name}{prereqs}}, $_->{'att'}->{course};
                },
                # ...
            }
        );
        $t->parsefile($_);
    }

This would completely fail if it had been

    for (...) {
        my $name = ...;
        sub need { ... }
        my $t = XML::Twig->new(
            TwigHandlers => {
                need => \&need,
                ...
            },
        );
        ...
    }

as the values would all have been pushed onto the same element of
%courses.

Ben

-- 
        I must not fear. Fear is the mind-killer. I will face my fear and
        I will let it pass through me. When the fear is gone there will be 
        nothing. Only I will remain.
benmorrow@tiscali.co.uk                                   Frank Herbert, 'Dune'


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

Date: 02 Aug 2006 15:55:14 GMT
From: xhoster@gmail.com
Subject: Re: foreach aliasing, my variables, and visibility in sub
Message-Id: <20060802120315.190$k4@newsreader.com>

Michele Dondi <bik.mido@tiscalinet.it> wrote:
> On Tue, 1 Aug 2006 15:57:42 +0100, Ben Morrow
> <benmorrow@tiscali.co.uk> wrote:
>
> >> process() is a closure around the variable $v lexically declared in
> >> the sorrounding scope.
> >
> >No it's not. Named subs are not closures in Perl5. It's just in a
> >different scope from the body of the for loop, with a different
> >variable named '$v'. Compare with C:
>
> Although that's indeed what both 'perldoc -q closure' and 'perldoc
> perlref' claim, I regard it as a standard misconception that closures
> *are* anonymous subs. In fact anonymity is orthogonal to the fact that
> subs "magically refer to the lexical variables that were around when
> they were defined", which is the distinctive charachteristic of a
> closure.

The abstract concepts may be orthogonal, but the Perl implementations
certainly are not.  The only useful way to get closures is with anonymous
subs. (You can get closures with named subs by using string eval, but I
think that oddity only qualifies as "slightly deviating from parallel"
rather than being orthogonal.)

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: Wed, 02 Aug 2006 18:06:36 +0200
From: Stephan Titard <sgt19@tid.es>
Subject: Re: foreach aliasing, my variables, and visibility in sub
Message-Id: <eaqiib$34k2@news.hi.inet>



>> and IMNSH understanding a named sub *is* a
>> closure around lexical variables defined in the sorrounding scope.
> 
> No, it isn't. Take my named subs example again
> 
>     sub mkfoo {
>         my $f = shift;
>         sub foo {
>             return $f;
>         }
>         return \&foo;
>     }
> 
> and call it twice
> 
>     my $x = mkfoo 1;
>     my $y = mkfoo 2;
>     print $x->();      # 1
>     print $y->();      # 1
> 
> . The two subs returned both refer to the *same* $f, the one that was in
> scope the first time mkfoo was run. With anon subs, the two returned
> subs have different $f variables, as they should have because they were
> created in different scopes (each runtime pass through mkfoo creates a
> new lexical scope).
> 
a good point actually. Checking with Devel::Peek the 'sub name {...}' 
gets compiled only once.
there are also a few warnings that at least signal something is wrong.
Confusing but probably too hard to fix in the core.

it *does* close on the first value seen...


  >>[23]stephang@aneto(~):
17:56:28:
$ cat make_subs.px
#!/usr/bin/perl
use Devel::Peek;

my $foo = 'out';
sub make_bar {
   my $foo = shift;
   sub bar {
     print ".bar: $foo\n"
   }
}


make_bar 1;
bar;
Dump(\&bar);

print;

make_bar 1;
bar;
Dump(\&bar);




  >>[25]stephang@aneto(~):
18:1:15:
$ perl -w make_subs.px
Variable "$foo" will not stay shared at make_subs.px line 8.
 .bar: 1
SV = RV(0x40035fb0) at 0x40015e34
   REFCNT = 1
   FLAGS = (TEMP,ROK)
   RV = 0x4002a43c
   SV = PVCV(0x4002b6f8) at 0x4002a43c
     REFCNT = 2
     FLAGS = ()
     IV = 0
     NV = 0
     COMP_STASH = 0x40015c78     "main"
     START = 0x4002d718 ===> 1049
     ROOT = 0x4002d840
     XSUB = 0x0
     XSUBANY = 0
     GVGV::GV = 0x4002a424       "main" :: "bar"
     FILE = "make_subs.px"
     DEPTH = 0
     FLAGS = 0x0
     OUTSIDE_SEQ = 93
     PADLIST = 0x400391e0
     PADNAME = 0x40039174(0x4002d870) PAD = 0x400282d0(0x4002d5a0)
        1. 0x400221b4<1> FAKE "$foo"
     OUTSIDE = 0x400221a8 (make_bar)
Use of uninitialized value in print at make_subs.px line 17.




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

Date: Wed, 02 Aug 2006 18:12:00 +0200
From: Stephan Titard <sgt19@tid.es>
Subject: Re: foreach aliasing, my variables, and visibility in sub
Message-Id: <eaqisf$34k3@news.hi.inet>


> The abstract concepts may be orthogonal, but the Perl implementations
> certainly are not.  The only useful way to get closures is with anonymous
> subs. (You can get closures with named subs by using string eval, but I
> think that oddity only qualifies as "slightly deviating from parallel"
> rather than being orthogonal.)
> 
> Xho
> 
actually having
make_sub {
   my $subname = shift;
   sub $subname { ...}
}
could be syntactic sugar for the glob thing

make_sub {
   my subname = shift;
   no strict
   *{$subname} = sub {...}
}


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

Date: 2 Aug 2006 18:46:19 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: foreach aliasing, my variables, and visibility in sub
Message-Id: <fkl1d25oa3h2bam8l3q3hlkl5ov3umvrjg@4ax.com>

On Wed, 2 Aug 2006 15:49:45 +0100, Ben Morrow
<benmorrow@tiscali.co.uk> wrote:
[snip]

Point taken!


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Wed, 2 Aug 2006 18:15:55 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: foreach aliasing, my variables, and visibility in sub
Message-Id: <b737q3-mkb.ln1@osiris.mauzo.dyndns.org>


Quoth sgt19DELCAPS@tidNOSPAM.es:
> 
> 
> >> and IMNSH understanding a named sub *is* a
> >> closure around lexical variables defined in the sorrounding scope.
> > 
> > No, it isn't. Take my named subs example again
> > 
<snip>
> > 
> > . The two subs returned both refer to the *same* $f, the one that was in
> > scope the first time mkfoo was run. With anon subs, the two returned
> > subs have different $f variables, as they should have because they were
> > created in different scopes (each runtime pass through mkfoo creates a
> > new lexical scope).
> > 
> a good point actually. Checking with Devel::Peek the 'sub name {...}' 
> gets compiled only once.
> there are also a few warnings that at least signal something is wrong.
> Confusing but probably too hard to fix in the core.

Impossible in principle, because sub names are global. Which set of
lexicals would you get if you just called foo()?

> it *does* close on the first value seen...

Yes, hence my calling this behaviour weird :).

Ben

-- 
#!/bin/sh
quine="echo 'eval \$quine' >> \$0; echo quined"
eval $quine
#                                                 [benmorrow@tiscali.co.uk]


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

Date: Wed, 2 Aug 2006 18:19:35 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: foreach aliasing, my variables, and visibility in sub
Message-Id: <7e37q3-mkb.ln1@osiris.mauzo.dyndns.org>


Quoth sgt19DELCAPS@tidNOSPAM.es:
> 
> > The abstract concepts may be orthogonal, but the Perl implementations
> > certainly are not.  The only useful way to get closures is with anonymous
> > subs. (You can get closures with named subs by using string eval, but I
> > think that oddity only qualifies as "slightly deviating from parallel"
> > rather than being orthogonal.)
> > 
> > Xho
> > 
> actually having
> make_sub {
>    my $subname = shift;
>    sub $subname { ...}
> }
> could be syntactic sugar for the glob thing
> 
> make_sub {
>    my subname = shift;
>    no strict
>    *{$subname} = sub {...}
> }

That leads to different, just as weird behaviour. Now when you call
&{$subname} you get the lexicals from the last time make_sub was called.

The 'sub foo {...}' semantics differ from '*foo = sub {...}' in that
(effectively) the first is only executed once. It's equivalent to
'BEGIN { *foo = sub {...} }'.

Ben

-- 
Like all men in Babylon I have been a proconsul; like all, a slave ... During
one lunar year, I have been declared invisible; I shrieked and was not heard,
I stole my bread and was not decapitated.
~ benmorrow@tiscali.co.uk ~            Jorge Luis Borges, 'The Babylon Lottery'


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

Date: Wed,  2 Aug 2006 17:20:02 +0200 (CEST)
From: Nomen Nescio <nobody@dizum.com>
Subject: how to "anonymize" Perl script before publishing it?
Message-Id: <d00a2f7b43a5c2d3ef124949e2bba582@dizum.com>

I want to publish a few of my Perl programs on usenet but I don't want
it to be obvious who wrote them.

Any tips on "personal style" aspects of Perl that I need to watch out
for to depersonalize or anonymize them?

Thanks



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

Date: 2 Aug 2006 10:04:49 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: how to "anonymize" Perl script before publishing it?
Message-Id: <1154538289.131655.95210@m79g2000cwm.googlegroups.com>

Nomen Nescio wrote:
> I want to publish a few of my Perl programs on usenet but I don't want
> it to be obvious who wrote them.

I'm afraid to ask, but why?  Why are you concerned that people might
identify these programs with you?

> Any tips on "personal style" aspects of Perl that I need to watch out
> for to depersonalize or anonymize them?

Well, for starters, take out any comments that identify you personally.
 After that, I would recommend reading
perldoc perlstyle
to see examples of generally considered *good* style.

Then you can download the Perl::Tidy module and the perltidy utility
(search CPAN) to reformat your code to a more standardized style...

I'm still worried about the root cause of your overall goal, however.
I can imagine two or three reasons one might want to disassociate
onesself from publically available code.  None of them good...

Paul Lalli



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

Date: Wed, 2 Aug 2006 17:58:05 +0200
From: =?ISO-8859-1?Q?Markus_H=E4nchen?= <youcontrol@hispeed.ch>
Subject: How to "convert" a string into a variable name?
Message-Id: <44d0cb8d@news1.ethz.ch>

Hi,

very simple question:

$TempOut = 25;
$name = '$' . 'Temp' . 'Out';
print $name;

What do I have to change to get '25' printed and not '$TempOut'? Or is 
this not possible?
Thanks.

Markus




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

Date: Wed, 2 Aug 2006 18:16:55 +0200
From: =?ISO-8859-1?Q?Markus_H=E4nchen?= <youcontrol@hispeed.ch>
Subject: Re: How to "convert" a string into a variable name?
Message-Id: <44d0cff7@news1.ethz.ch>

Found the solution myself:

$TempOut = 25;
$name = 'print '. '"$' . 'Temp' . 'Out"';
eval $name;

Sorry for bothering you.
Markus


On 2006-08-02 17:58:05 +0200, Markus Hänchen <youcontrol@hispeed.ch> said:

> Hi,
> 
> very simple question:
> 
> $TempOut = 25;
> $name = '$' . 'Temp' . 'Out';
> print $name;
> 
> What do I have to change to get '25' printed and not '$TempOut'? Or is 
> this not possible?
> Thanks.
> 
> Markus




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

Date: 2 Aug 2006 10:01:18 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: How to "convert" a string into a variable name?
Message-Id: <1154538078.184836.317950@75g2000cwc.googlegroups.com>

Markus H=E4nchen wrote:
> > very simple question:
> >
> > $TempOut =3D 25;
> > $name =3D '$' . 'Temp' . 'Out';
> > print $name;
> >
> > What do I have to change to get '25' printed and not '$TempOut'? Or is
> > this not possible?

> Found the solution myself:
>
> $TempOut =3D 25;
> $name =3D 'print '. '"$' . 'Temp' . 'Out"';
> eval $name;

While it's good that you found *a* solution on your own, I must caution
you that this is a *bad* solution. Very very bad.  eval() is a powerful
but dangerous function.  If at any point, the string you're eval'ing
contains input from a user, you could do some serious damage to your
program and to your system.

A *good* solution is to _not_ try to create a variable name out of a
string.  Instead, store all the "variables" you want to access this way
in a hash.  The keys are whatever you currently have as a variable
name, and the value is the value you wanted to assign to that variable.
 Using that method, your example above becomes:

my %value_of
$value_of{TempOut} =3D 25;
$name =3D 'Temp' . 'Out';
print $value_of{$name};

This is all spelled out much more clearly in the FAQ:
perldoc -q "variable name"

I strongly suggest you read it.

Good luck,
Paul Lalli



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

Date: Wed, 2 Aug 2006 17:03:43 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: How to "convert" a string into a variable name?
Message-Id: <vvu6q3-90b.ln1@osiris.mauzo.dyndns.org>


Quoth =?ISO-8859-1?Q?Markus_H=E4nchen?= <youcontrol@hispeed.ch>:
> Hi,
> 
> very simple question:
> 
> $TempOut = 25;
> $name = '$' . 'Temp' . 'Out';
> print $name;
> 
> What do I have to change to get '25' printed and not '$TempOut'? Or is 
> this not possible?

This is a FAQ. perldoc -q "variable name".

Ben

-- 
           All persons, living or dead, are entirely coincidental.
benmorrow@tiscali.co.uk                                           Kurt Vonnegut


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

Date: Wed, 02 Aug 2006 12:52:11 -0400
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: perl editor
Message-Id: <g69vepbjd1g.fsf@CN1374059D0130.kendall.corp.akamai.com>

On  2 Aug 2006, bik.mido@tiscalinet.it wrote:

On 01 Aug 2006 20:17:20 GMT, Abigail <abigail@abigail.be> wrote:
>
>> vim isn't vi. Despite toy OS distro's firing up 'vim' is you type 'vi'.
>
> What should they do? Avoid it altogether? I don't use vi myself, but I
> wouldn't regard it as such a bad thing...

I think Abigail is referring to the original request for a "free"
editor, which vi isn't but vim is.

Plain vi is pretty awful, so it should probably be avoided altogether.
I speak from experience, having used vi on Solaris for a while.
Compared to vim it's simply outdated, and it has too many limitations
to be useful for any serious programming.  For Perl specifically,
vim's support is much better.

Ted


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

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


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