develooper Front page | perl.perl5.porters | Postings from February 2012

[perl #109408] Documentation that refers to Perl 5 as new

Thread Previous | Thread Next
Father Chrysostomos via RT
February 1, 2012 22:37
[perl #109408] Documentation that refers to Perl 5 as new
Message ID:
On Wed Feb 01 16:49:50 2012, tom christiansen wrote:
> >On Wed Feb 01 12:30:58 2012, sprout wrote:
> > =item *
> > 
> > One cannot easily add arbitrary properties to an existing object.  For a
> > publicly-accessible property, make a subclass, and maybe consider
> > C<AUTOLOAD>.  To associate data with arbitrary objects, see
> > L<Hash::Util::FieldHash>.
> You mean data members?  I don't believe in letting your member
> s hang out in public. :)  I consider the class a members-only area.

I suppose that entry is a bit vague.  In a web browser environment, it
is possible and very tempting (and also safe, when done carefully) to
add arbitrary properties to DOM elements.  I’ve abused that many times.
 Just use underscores, as underscores are not used in any APIs.  It’s
quite safe if you control all the code that runs in a particular page,
which is the usual case.

But in general adding properties to objects of another class is not a
good idea.  So I suppose it shouldn’t be encouraged. :-)

> > =item *
> >
> > An object's members cannot be made accessible as variables.
> > The closest Perl equivalent to C<with(object) { method() }> is
> > C<for>, which can alias C<$_> to the object:
> >
> >     for ($object) {  $_->method; }
> A feature, perhaps somewhat disguised, but a feature nonetheless.

Yes, but JavaScript has a very different model.  There is no difference
between a property and a variable in JavaScript.  Every variable is a
property of some object.  An unqualified variable refers to the property
of an object in scope.  (A function’s run-time scope is a special object
not directly accessible to JavaScript code.)

> Then again, lvaluable methods are kinda variabili(t)ous.
> > =item *
> >
> > Variable declarations only affect code I<after> the
> > declaration.  You cannot write C<$x = 1; my $x;> and expect the
> > first assignment to affect the same variable.  It will instead
> > assign to an C<$x> declared previously in an outer scope, or to
> > a global variable.
> Good thing we don't want confuse them with 
>     my $x = 1 + $x;
> do we now? :)

Actually, that might be good to bring up, too.

> > =item *
> >
> > C<my> variables are scoped to the current block, not to the
> > current function.  If you write C<{my $x;} $x;>, the second
> > C<$x> does not refer to the one declared inside the block.
> In general, Perl has *lexical* scope, and indeed, the 
> scope is the ‘font of all privacy’(⋆) in Perl.  Think of
> (most) pragmas, for example.  
> > =item *
> >
> > The object on which a method is called is passed as one of the
> > method's arguments, not as a separate C<this> value.
> Not just the object, but any invocant, so classes, too.
> The invocant is always unshift()ed onto the arglist.
> --tom
>   PS: Yeah ok, if you can't bring yourself to posh up “the /fɔːnt/

That should be /fɒnt/.  If you say /fɔːnt/ (British pronunciation of
‘faunt’, whatever that means), you’ll sound like an American trying to
put on a British accent. :-)

> of all
>       /ˈprɪvəsi/”, you can just say “the source of all /ˈpraɪvəsi/”.  :)


Father Chrysostomos

via perlbug:  queue: perl5 status: open

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About