[31111] in Perl-Users-Digest
Perl-Users Digest, Issue: 2356 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Apr 21 00:09:48 2009
Date: Mon, 20 Apr 2009 21:09:10 -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, 20 Apr 2009 Volume: 11 Number: 2356
Today's topics:
Re: Capturing Port Data (Jens Thoms Toerring)
Re: Capturing Port Data <XXjbhuntxx@white-star.com>
Re: Capturing Port Data <ben@morrow.me.uk>
Re: dmake.exe: Error: -- 'C:\Perl\libConfig.pm' not fou <sisyphus359@gmail.com>
Re: F<utf8.pm> is evil (was: XML::LibXML UTF-8 toString <hjp-usenet2@hjp.at>
Re: How can I backreference a ?: group in perl regular <someone@example.com>
Re: Is there a better way to convert foreign characters <rvtol+usenet@xs4all.nl>
Re: Is there a better way to convert foreign characters <noreply@gunnar.cc>
Re: Print strings based on regular expression <tzz@lifelogs.com>
Re: What does `my' do?! (Tim McDaniel)
Re: What does `my' do?! <ben@morrow.me.uk>
Re: What does `my' do?! <blgl@hagernas.com>
Re: What does `my' do?! <ben@morrow.me.uk>
Re: What does `my' do?! <devnull4711@web.de>
Re: What does `my' do?! derykus@gmail.com
Re: What does `my' do?! <nospam-abuse@ilyaz.org>
Re: What does `my' do?! <ben@morrow.me.uk>
Re: What does `my' do?! <ben@morrow.me.uk>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 20 Apr 2009 23:14:08 GMT
From: jt@toerring.de (Jens Thoms Toerring)
Subject: Re: Capturing Port Data
Message-Id: <754du0F15fr06U1@mid.uni-berlin.de>
Cosmic Cruizer <XXjbhuntxx@white-star.com> wrote:
> I could not get the yahoo modules to work, nor did I have success with
> IO::Socket. So now I'm trying to use Net::Telnet. I think the problem has
> to do with needing two returns after $msg. I've tried using both print and
> cmd without success.
> Anybody know why I'm not able to capture the output?
> use strict;
> use warnings;
> use Net::Telnet;
> my $msg = 'GET /capacity HTTP/1.1';
> my $port = 5050;
> my $timeout = 5;
> my $telnet = new Net::Telnet (
> Timeout => $timeout,
> Errmode => 'die',
> Port => $port,);
> $telnet->open('66.163.181.177');
> if(!$telnet){
> print "No connection\n";
> }
> else {
> # Tried using combinations of print and cmd
> my @output1 = $telnet->print($msg);
> print "First: $_\n" foreach(@output1);
> my @output2 = $telnet->print("\n");
> print "Second: $_\n" foreach(@output2);
> $telnet->close;
> }
> I'm trying to capture the following: "CS_IP_ADDRESS=66.163.181.177"
I'm not sure if it's relevant to your problem (and you don't
tell what exactly goes wrong), but the message you send to
the server
> my $msg = 'GET /capacity HTTP/1.1';
looks wrong. HTTP version 1.1 requires you to send quite a
bit more than just that (you must send at least another line
starting with 'Host:' and followed by the name of the client).
For a start try HTTP version 1.0 with
my $msg = "GET /capacity HTTP/1.0\n"
Note that sending an empty line (thus the extra "\n") is what
tells the server that it has seen all of the request from the
client. If that does the trick you better read the HTTP spec-
ifications (RFC 2616 for HTTP 1.1 IIRC) carefully;-)
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
------------------------------
Date: Tue, 21 Apr 2009 01:04:48 GMT
From: Cosmic Cruizer <XXjbhuntxx@white-star.com>
Subject: Re: Capturing Port Data
Message-Id: <Xns9BF3B7DE4D0E1ccruizermydejacom@207.115.33.102>
Ben Morrow <ben@morrow.me.uk> wrote in
news:oeoub6-d413.ln1@osiris.mauzo.dyndns.org:
>
> Well, AFAICS you are trying to do a GET of
> http://88.xx.xx.161:5050/capacity (sorry, I can't remember the exact
> IP address). What happens if you simply pass that URL to
> LWP::Simple::get?
>
> Ben
>
>
Interesting. This actually works. It does not give me all of the info I get
when I telnet to the port, but it at least returns the IP address.
Even though nobody could help with the Net::Telnet (which I have used
successfully for other problems), you came up with a viable solution.
Thank you very much Ben!
------------------------------
Date: Tue, 21 Apr 2009 02:43:31 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Capturing Port Data
Message-Id: <3vkvb6-l47.ln1@osiris.mauzo.dyndns.org>
Quoth Cosmic Cruizer <XXjbhuntxx@white-star.com>:
> Ben Morrow <ben@morrow.me.uk> wrote in
> news:oeoub6-d413.ln1@osiris.mauzo.dyndns.org:
>
> >
> > Well, AFAICS you are trying to do a GET of
> > http://88.xx.xx.161:5050/capacity (sorry, I can't remember the exact
> > IP address). What happens if you simply pass that URL to
> > LWP::Simple::get?
>
> Interesting. This actually works. It does not give me all of the info I get
> when I telnet to the port, but it at least returns the IP address.
I strongly suspect that if you use the full LWP::UserAgent rather than
LWP::Simple you will find the rest of the information in the headers of
the returned HTTP::Response.
Ben
------------------------------
Date: Mon, 20 Apr 2009 11:45:26 -0700 (PDT)
From: sisyphus <sisyphus359@gmail.com>
Subject: Re: dmake.exe: Error: -- 'C:\Perl\libConfig.pm' not found, and can't be made
Message-Id: <63beca82-d0e6-4c15-985a-135f3fa29ea7@x29g2000prf.googlegroups.com>
On Apr 20, 10:08=A0pm, tus...@gmail.com wrote:
> On Apr 20, 11:05=A0am, tus...@gmail.com wrote:
>
> > Where does the value "$self->make" originate ?
>
> I've found the problem...
>
> ...in "lib\ActivePerl\Config.pm":
Yes - you've pretty much nailed it. And if I had been more astute and
thorough, the problem would have got solved considerably quicker.
Another way to resolve the matter is, instead of renaming nmake.exe,
to remove its location from the path. (I do have nmake.exe on my
system, but the location of that file is not in my path.)
I even managed to get the same errors as you by adding the location of
nmake.exe to my path.
Anyway, thanks for following this through, and sorry it dragged out
for so long. At least now we know that whenever we see that error it
simply means that nmake is being found, and the generated Makefile is
being written in accordance with nmake's syntax rules. (Yet another
solution would have been to run 'nmake' instead of 'dmake' - but if
it's that old crappy nmake-1.5, you don't really want to do that.)
Cheers,
Rob
------------------------------
Date: Tue, 21 Apr 2009 00:16:34 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: F<utf8.pm> is evil (was: XML::LibXML UTF-8 toString() -vs- nodeValue())
Message-Id: <slrngupt23.cc.hjp-usenet2@hrunkner.hjp.at>
On 2009-04-17 00:23, Eric Pozharski <whynot@pozharski.name> wrote:
> On 2009-04-15, Peter J. Holzer <hjp-usenet2@hjp.at> wrote:
>> On 2009-04-14 23:45, Eric Pozharski <whynot@pozharski.name> wrote:
>>> On 2009-04-12, Peter J. Holzer <hjp-usenet2@hjp.at> wrote:
>>>
>>> I've thought a lot. I should admit, whenever I see C<use 'utf8';>
>>> instead of C<use encoding 'utf8';> I'm going nuts.
>>
>> I'm going nuts when I see "use encoding". It does way too many magic
>> things and most of them only half-baked. Here be dragons! Don't use that
>> stuff, if you value your sanity.
>
> I'm puzzled. However, I should admit, that I've yet found those dark
> corners of C<use encoding 'utf8';>.
I found quite a few of them, but that was in the early times of 5.8.x,
so some of them may have been bugs which have since been fixed and some
of them may have been caused by my lack of understanding how the (at the
time) new "utf8" strings were supposed to work. However, I think that
Encoding.pm (the module itself and its documentation) added
significantly to my confusion instead of lessening it. It took me some
time to figure things out.
But a couple of points which immediatly come to mind while scrolling
through perldoc encoding:
* encoding automatically pushes an :encoding() layer on STDIN and
STDOUT, but not on STDERR. Why not?
* And why does it do that at all? The user of the script may use a
completely different locale than the author, so mixing source code
encoding with runtime I/O conversion just isn't a good idea.
(and "use encoding ':locale'" is just a desaster waiting to happen,
but at least the pod warns you)
* All strings are converted, even if they contain \x escapes. So
"\xF1\xD1\xF1\xCC" is a string with *two* characters if "encoding
'euc-jp'" is in effect. This makes it quite hard to write scripts
which work in binary data.
* utf8::upgrade and utf8::downgrade aren't symmetric.
>>> And with C<use encoding 'utf8';> you'll get the same character string,
>>> and lots of other useful stuff.
>>
>> Correction: Lots of stuff which looks useful at first glance but which
>> works in subly different ways than you expect (and some stuff which you
>> simply don't expect). "use utf8" OTOH does only one thing, and it does
>> it well.
>
> I fail to see how utfizing both literals and symbols makes F<utf8.pm>
> doing one thing. I don't say that it doesn't do it well.
It doesn't utf8ize "both literals and symbols", it simply reads the
complete source text in character mode (with :encoding(utf8)). A very
simple operation, and everything else is a logical consequence from
that. So "\x{D0}\x{BF}" is still a string of 2 bytes (because it
reads "double-quote backslash open-brace capital D 0
closed-brace backslash ..." and that parses to two bytes by the normal
perl parsing rules, but "п" parses as one character because that reads
"double-quote cyrillic-small-letter-pe double-quote". And a variable $п
is allowed because п is a letter and Perl allows letters, digits and the
underscore in variable names. No surprises there.
>>> (I just can't get why anyone would need
>>> implicit upgrade of scalars into characters and yet then maintain wide
>>> IO.) But my point isn't that F<encoding.pm> outperforms F<utf8.pm>.
>>> I'm scared. I consider F<utf8.pm> kind of Pandora box. Read this, if
>>> you can
>>>
>>> проц запросить {
>>> мое ($имяфайла) = @_;
>> [...]
>>> }
>>>
>>> I admit, it's imposible to write this with F<utf8.pm> alone
>>
>> Right. "sub" still is "sub", not "проц", and "my" is still "my", not
>> "мое". Your example is more like a Russian(?) equivalent to
>> Lingua::Romana::Perligata.
>>
>> And frankly, "проц запросить" is only marginable less readable to me
>> than "sub zaprosit". I need a dictionary for both, and "запросить" at
>> least has the advantage that I can actually find it in a Russian
>> dictionary :-). If you want your software to be maintainable by authors
>> from other countries, stick to English and write "sub request". If you
>> want to use Russian names you might as well go all the way and use
>> cyrillic letters instead of a transliteration into latin letters which
>> neither those who speak Russian nor those who don't speak Russian
>> understand easily.
>
> So you suggest that localizing Perl (or actually any other language) is
> kind of online dictionary providers conspiracy?
No, not at all. What I am saying is that people will use localized
variable and subroutine names, and write comments in their native
language, no matter if the programming language makes it easy or not.
Sometimes this is because they don't speak English too well, sometimes
it's because the problem domain is language-specific (for example, when
I was a teaching assistant at the university, we often wrote programs to
help in some administrative task - students here are identified by a
"Matrikelnummer". According to the dictionary, that's "enrollment
number" in English, but it simply didn't make sense to use a variable
$enrollment_number, because *nobody* who had to maintain that program
would know what an "enrollment number" is, but everybody would know what
$matrikelnummer means (you simply can't study or work at an Austrian
university without knowing that)). Now, using German variable names is
simple: We use only 7 non-ASCII letters, and there are standardized and
well-known transliterations for all of them. So it isn't a great
inconvenience if we need to write $uebung instead of $übung. But in
Russian (or Greek, or Japanese, or Hindi, ...) the entire Alphabet is
non-ASCII, and there are usually many transliteration systems. So if a
Russian (or Greek, or Japanese, or Indian, ...) programmer wants to use
some word from their native tongue, they need to choose one
transliteration which is not natural to them and may be almost
unreadable for the next programmer who has to maintain it.
Since this is simply a fact of life, it is much better to allow
non-English languages to be written in their native alphabet than to
force transliteration into US-ASCII. At least then programs written in
Russian are readable to people speaking Russian instead of being
unreadable for everybody except the programmer.
>>> I bet you've seen this before,
>>
>> I've seen German versions of BASIC in the 1980's. They weren't a huge
>> success, to put it mildly. About the only successful localization of a
>> programming language i can think of is MS-Excel (and I guess it's
>> debatable if this is even a programming language (without VBA) - is it
>> turing-complete?).
>
> That's in case you have an option. There're places you have no option.
I don't understand your objection. I was relating the historic fact that
localized programming languages (i.e., programming languages where the
keywords (and to a lesser amount, the grammar) were localized, so that
you would write "wenn ... dann ... sonst ..." instead of "if ... then
... else") were a failure. People had the option to use them, but they
didn't want to.
hp
------------------------------
Date: Mon, 20 Apr 2009 12:11:44 -0700
From: "John W. Krahn" <someone@example.com>
Subject: Re: How can I backreference a ?: group in perl regular expression?
Message-Id: <SN3Hl.30120$Ji5.20088@newsfe21.iad>
kun niu wrote:
> On 4?21?, ??12?50?, Ben Morrow <b...@morrow.me.uk> wrote:
>> Quoth kun niu <haoniu...@gmail.com>:
>>
>>> Dear all,
>>> My question is listed as title.
>>> I want to use strings like \1,\2 etc to represent the group matched
>>> last time.
>>> But the group is not captured, as signed by ?:.
>>> Can I implement this in perl?
>> No. That, in fact, is the whole point of (?:) groups.
>>
>> Why do you think you need to do this?
>=20
> Thank you for your attention to my question and your precious time.
> In my application, I'll have reformat the telephone number in my
> company.
> Since they came from all over the world, we have various telephone
> format.
> Here's some examples:
> 000-000-000
> (00)00-000-0000
> !00!00-000-0000
> 0000000
> 0000-0000
> 0000 0000 000
> you dont care
> What my job is to check the valid telephone number and to give them
> out.
> In case that I meet a ')' character, I'll have to check if there's a
> '(' in the front.
> '!' is also valid here.
> But all these characters should not be captured.
> So I'll have to backreference a previous group
> Expression like "((?:[!|]*)\d+\1\d+)" gives a bad result.
> Any further hints or advice here?
"(([!|]*)\d+\2\d+)"
John
--=20
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
------------------------------
Date: Mon, 20 Apr 2009 21:50:16 +0200
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: Is there a better way to convert foreign characters?
Message-Id: <49ecd1f9$0$201$e4fe514c@news.xs4all.nl>
Guy wrote:
> I'm sure there are many ways to do this, but is there a much better way?
>
> $value=~tr//aaaeeeeeecciioouu/;
> $word=lc($value);
>
> I want $word to equal the english version of $value. So if
> $value="Thodore", I want $word="theodore". I'd like to do it in one
> statement if possible but I think I have to convert $value in one statement
> and then assign it to $word in another statement.
perl -Mstrict -Mutf8 -MText::Unidecode -wle '
my $s = "";
print Text::Unidecode::unidecode( $s );
'
aaAeeeeEEcCiioOuu
--
Ruud
------------------------------
Date: Mon, 20 Apr 2009 23:40:56 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Is there a better way to convert foreign characters?
Message-Id: <7548fhF1621ocU1@mid.individual.net>
Dr.Ruud wrote:
>
> perl -Mstrict -Mutf8 -MText::Unidecode -wle '
> my $s = "";
> print Text::Unidecode::unidecode( $s );
> '
> aaAeeeeEEcCiioOuu
The purpose of that module is to handle non-Roman characters. What makes
you believe those characters are Unicode?
$ perl -MEncode -le '
$octets = "";
print "Raw: ", $octets;
print "Latin-1: ", decode "ISO-8859-1", $octets;
print "ANSI: ", decode "Windows-1252", $octets;
'
Raw:
Latin-1:
ANSI:
$
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Mon, 20 Apr 2009 13:58:43 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Print strings based on regular expression
Message-Id: <86bpqrywws.fsf@lifelogs.com>
On Sun, 19 Apr 2009 11:36:35 -0700 (PDT) Yong Huang <yong321@yahoo.com> wrote:
YH> Is there a program that prints all possible strings based on a regular
YH> expression?
YH> $ thisprogram '^\w\d$'
YH> A0
YH> A1
YH> ...
YH> z9
YH> The output could easily be made infinite. That's fine. The point is to
YH> let users visualize what the matching strings look like. I think this
YH> program has great educational value in teaching regular expressions.
See _Higher-Order Perl_ by Mark-Jason Dominus, chapter 6.5 (generating a
list of all the strings matched by a regular expression). The whole
book is terrific.
It's available online for free but it's also one of the few Perl books I
recommend purchasing without reservation.
Ted
------------------------------
Date: Mon, 20 Apr 2009 18:20:19 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: What does `my' do?!
Message-Id: <gsied3$pnd$1@reader1.panix.com>
In article <753q48F15pqqlU1@mid.individual.net>,
Frank Seitz <devnull4711@web.de> wrote:
>The scope of a lexical (my) variable begins AFTER the expression in
>which it is declared, NOT inside the expression.
I've seen that statement before, but it now seems unclear to me. If
it were strictly and completely true, I don't see how you could assign
to a variable in a "my" -- because I use "scope of a variable" to mean
(roughly) "for an instance of $x, the places where $x refers to the
instance". For example, in
my $x = 1;
the new $x has to be assigned to for the statement to work, so I
interpret it that $x has to be in scope as an lvalue before the "="
(and therefore not "AFTER the expression") so it can be assigned to.
For a sillier distinguishing example,
my $x = $x;
as I see it, the new $x is not in the scope of the right-hand side but
IS in scope on the left-hand side.
--
Tim McDaniel, tmcd@panix.com
------------------------------
Date: Mon, 20 Apr 2009 21:53:27 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: What does `my' do?!
Message-Id: <7v3vb6-om1.ln1@osiris.mauzo.dyndns.org>
Quoth tmcd@panix.com:
> In article <753q48F15pqqlU1@mid.individual.net>,
> Frank Seitz <devnull4711@web.de> wrote:
> >The scope of a lexical (my) variable begins AFTER the expression in
> >which it is declared, NOT inside the expression.
>
> I've seen that statement before, but it now seems unclear to me. If
> it were strictly and completely true, I don't see how you could assign
> to a variable in a "my" -- because I use "scope of a variable" to mean
> (roughly) "for an instance of $x, the places where $x refers to the
> instance". For example, in
>
> my $x = 1;
>
> the new $x has to be assigned to for the statement to work, so I
> interpret it that $x has to be in scope as an lvalue before the "="
> (and therefore not "AFTER the expression") so it can be assigned to.
You can assign to a variable that isn't in scope; for example,
my $rv = do { \my $x };
$$rv = 4;
The name $x has gone out of scope by the time the second statement
executes, but you can still assign to the variable it used to represent.
Similarly, one of the runtime effects of 'my' is that it returns (as an
lvalue) a variable that doesn't have a name in scope yet.
> For a sillier distinguishing example,
>
> my $x = $x;
>
> as I see it, the new $x is not in the scope of the right-hand side but
> IS in scope on the left-hand side.
No, the name isn't in scope yet, even though the variable has been
created.
Ilya is right that this is very annoying, and could (should?) be
considered a bug in Perl. I don't really know why it was implemented
like that in the first place, though I can see it's obviously not going
to change now.
Ben
------------------------------
Date: Tue, 21 Apr 2009 01:50:34 +0200
From: Bo Lindbergh <blgl@hagernas.com>
Subject: Re: What does `my' do?!
Message-Id: <gsj1oh$692$1@aioe.org>
In article <7v3vb6-om1.ln1@osiris.mauzo.dyndns.org>,
Ben Morrow <ben@morrow.me.uk> wrote:
> I don't really know why it was implemented like that in the first place,
To make Lisp programmers feel at home? Compare:
(let ((x x)
(y y))
...whatever...)
and contrast:
{
my($x,$y)=($x,$y);
...whatever...
}
/Bo Lindbergh
------------------------------
Date: Tue, 21 Apr 2009 02:08:49 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: What does `my' do?!
Message-Id: <1uivb6-b07.ln1@osiris.mauzo.dyndns.org>
Quoth Bo Lindbergh <blgl@hagernas.com>:
> In article <7v3vb6-om1.ln1@osiris.mauzo.dyndns.org>,
> Ben Morrow <ben@morrow.me.uk> wrote:
> > I don't really know why it was implemented like that in the first place,
>
> To make Lisp programmers feel at home? Compare:
>
> (let ((x x)
> (y y))
> ...whatever...)
>
> and contrast:
>
> {
> my($x,$y)=($x,$y);
> ...whatever...
> }
I don't know Lisp at all well: the closest language I am at all familiar
with is Haskell, where
let x = 1
y = x + 1
in print y
will indeed print '2' as I would expect. Am I to take it that Lisp
introduces all of a set of let-bindings in one go?
I rather doubt this is why Perl does it, though it wouldn't be the first
time something was added to make other programmers feel at home ($[
anyone? The original ' package separator?). I suspect it was just too
hard to work out which parts of the expression should have the new name
in scope and which shouldn't from inside the guts of perl's parser.
Ben
------------------------------
Date: Tue, 21 Apr 2009 03:36:43 +0200
From: Frank Seitz <devnull4711@web.de>
Subject: Re: What does `my' do?!
Message-Id: <754m97F15pqqlU2@mid.individual.net>
Tim McDaniel wrote:
> In article <753q48F15pqqlU1@mid.individual.net>,
> Frank Seitz <devnull4711@web.de> wrote:
>> The scope of a lexical (my) variable begins AFTER the expression in
>> which it is declared, NOT inside the expression.
>
> I've seen that statement before, but it now seems unclear to me. If
> it were strictly and completely true, I don't see how you could assign
> to a variable in a "my" [...]
Okay, I try to be more precise:
STMT;
You can declare a lexical variable in STMT and - optionally -
assign a value to it, but nothing else. Within STMT you can't read it's value,
nor can you (re-)assign another value. The (real?) scope of the
variable begins after STMT.
Example:
f(my $x = 1, ...); # it's not possible to access $x in ...
# <- the scope of $x begins here
$x += 99;
Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen fr Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
------------------------------
Date: Mon, 20 Apr 2009 19:36:51 -0700 (PDT)
From: derykus@gmail.com
Subject: Re: What does `my' do?!
Message-Id: <c6f6fefb-828a-4302-b36b-9ed2b324443b@a5g2000pre.googlegroups.com>
On Apr 20, 1:53=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth t...@panix.com:
>
>
>
> > In article <753q48F15pqq...@mid.individual.net>,
> > Frank Seitz =A0<devnull4...@web.de> wrote:
> > >The scope of a lexical (my) variable begins AFTER the expression in
> > >which it is declared, NOT inside the expression.
>
> > I've seen that statement before, but it now seems unclear to me. =A0If
> > it were strictly and completely true, I don't see how you could assign
> > to a variable in a "my" -- because I use "scope of a variable" to mean
> > (roughly) "for an instance of $x, the places where $x refers to the
> > instance". =A0For example, in
>
> > =A0 =A0 my $x =3D 1;
>
> > the new $x has to be assigned to for the statement to work, so I
> > interpret it that $x has to be in scope as an lvalue before the "=3D"
> > (and therefore not "AFTER the expression") so it can be assigned to.
>
> You can assign to a variable that isn't in scope; for example,
>
> =A0 =A0 my $rv =3D do { \my $x };
> =A0 =A0 $$rv =3D 4;
>
> The name $x has gone out of scope by the time the second statement
> executes, but you can still assign to the variable it used to represent.
> Similarly, one of the runtime effects of 'my' is that it returns (as an
> lvalue) a variable that doesn't have a name in scope yet.
>
> > For a sillier distinguishing example,
>
> > =A0 =A0 my $x =3D $x;
>
> > as I see it, the new $x is not in the scope of the right-hand side but
> > IS in scope on the left-hand side.
>
> No, the name isn't in scope yet, even though the variable has been
> created.
>
> Ilya is right that this is very annoying, and could (should?) be
> considered a bug in Perl. I don't really know why it was implemented
> like that in the first place, though I can see it's obviously not going
> to change now.
>
In the case of 'my $x =3D $x', wouldn't that be
somewhat confusing though...?
At least with current semantics, you could
do something that'd be arguably clearer
about the intent, eg:
my $x =3D 'what if";
{(my $x =3D $x) =3D~ s/what/then/;...}
--
Charles DeRykus
------------------------------
Date: Tue, 21 Apr 2009 02:41:03 GMT
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: What does `my' do?!
Message-Id: <slrnguqchv.i26.nospam-abuse@chorin.math.berkeley.edu>
On 2009-04-20, Ben Morrow <ben@morrow.me.uk> wrote:
> You can assign to a variable that isn't in scope;
No.
> for example,
>
> my $rv = do { \my $x };
> $$rv = 4;
>
> The name $x has gone out of scope by the time the second statement
> executes, but you can still assign to the variable it used to represent.
No, you do not assign to a variable here. Any more than
shift()->{key} = 12;
assigns to a variable...
> Similarly, one of the runtime effects of 'my' is that it returns (as an
> lvalue) a variable that doesn't have a name in scope yet.
Yes, the (correct part of) this was the content of my previous
correction to (your?) message ;-). The incorrectness may be
"corrected" as
`my' returns an lvalue assignment to which modifies the same
container as "available in the future" variable
(if one would consider it as prose... :-[).
> Ilya is right that this is very annoying, and could (should?) be
> considered a bug in Perl. I don't really know why it was implemented
> like that in the first place, though I can see it's obviously not going
> to change now.
The *initial* intent (lost in the mist of centuries) was to be able to
"temporalize" changes by
local $foo = $foo;
`my' just behave symmetric to `local' - which was a very bad choice...
Yours,
Ilya
------------------------------
Date: Tue, 21 Apr 2009 03:51:04 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: What does `my' do?!
Message-Id: <otovb6-209.ln1@osiris.mauzo.dyndns.org>
Quoth Frank Seitz <devnull4711@web.de>:
> Tim McDaniel wrote:
> > In article <753q48F15pqqlU1@mid.individual.net>,
> > Frank Seitz <devnull4711@web.de> wrote:
> >> The scope of a lexical (my) variable begins AFTER the expression in
> >> which it is declared, NOT inside the expression.
> >
> > I've seen that statement before, but it now seems unclear to me. If
> > it were strictly and completely true, I don't see how you could assign
> > to a variable in a "my" [...]
>
> Okay, I try to be more precise:
>
> STMT;
>
> You can declare a lexical variable in STMT and - optionally -
> assign a value to it, but nothing else. Within STMT you can't read it's value,
> nor can you (re-)assign another value. The (real?) scope of the
> variable begins after STMT.
You just *know* I have to argue with that, don't you :)?
my $x;
$x = \(my $y = 3),
# $y isn't in scope yet
$$x = 4;
say $y;
gives '4', so you *can* assign to $y before the statement has ended. You
just can't use the *name* $y to refer to it, as the name isn't in scope
yet.
Names have different scopes from variables. The scope of the name begins
at the end of the statement and ends at the end of the innermost
containing block. The scope of the variable begins when the 'my'
expression is reached at runtime and ends when the last ref to it is
removed (it gets a temporary free pass for the remainder of its first
statement, otherwise it would immediately vanish (actually, it always
gets a free pass for the remainder of the statement in which it loses
all refs. Consider
use Scalar::Util qw/weaken/;
my $x = [4];
my $wr = $x;
weaken $wr;
$x = undef, my $y = $wr;
say $wr;
and then replace that , with a ; .)).
Ben
------------------------------
Date: Tue, 21 Apr 2009 03:59:15 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: What does `my' do?!
Message-Id: <3dpvb6-209.ln1@osiris.mauzo.dyndns.org>
Quoth Ilya Zakharevich <nospam-abuse@ilyaz.org>:
> On 2009-04-20, Ben Morrow <ben@morrow.me.uk> wrote:
> > You can assign to a variable that isn't in scope;
>
> No.
>
> > for example,
> >
> > my $rv = do { \my $x };
> > $$rv = 4;
> >
> > The name $x has gone out of scope by the time the second statement
> > executes, but you can still assign to the variable it used to represent.
>
> No, you do not assign to a variable here. Any more than
>
> shift()->{key} = 12;
>
> assigns to a variable...
>
> > Similarly, one of the runtime effects of 'my' is that it returns (as an
> > lvalue) a variable that doesn't have a name in scope yet.
>
> Yes, the (correct part of) this was the content of my previous
> correction to (your?) message ;-). The incorrectness may be
> "corrected" as
>
> `my' returns an lvalue assignment to which modifies the same
> container as "available in the future" variable
>
> (if one would consider it as prose... :-[).
OK, this is just arguing about terms. We are saying the same thing, you
just use 'variable' to mean 'pad/stash entry' and 'container' to mean 'SV',
whereas I use 'name' and 'variable'.
> > Ilya is right that this is very annoying, and could (should?) be
> > considered a bug in Perl. I don't really know why it was implemented
> > like that in the first place, though I can see it's obviously not going
> > to change now.
>
> The *initial* intent (lost in the mist of centuries) was to be able to
> "temporalize" changes by
>
> local $foo = $foo;
>
> `my' just behave symmetric to `local' - which was a very bad choice...
Ah, yes, of course. I always forget (every single time I use it) that
local $foo;
doesn't do that anyway :). I almost never want it to do anything else,
(and I would dearly like to be able to say
local $foo++;
and have it DWIM) but I can see that the implemented semantics would
have been useful when local was the only way of getting local variables.
Ben
------------------------------
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 2356
***************************************