Stuffing @users into $self->{'users'}

P

Peter J. Holzer

I already own programming perl by Larry Wall. It's sitting here on my
desk. However it happens to be over 900 pages and the author spends
all his time setting up jokes and "funny" lines of code rather than
actually explain in programming terms what the hell is going on.

The "camel book" probably isn't the best book to learn perl. It is
intended as a reference, not a tutorial. The "llama book", Learning
Perl, Fourth Edition, by Randal L. Schwartz, Tom Phoenix, brian d foy,
is generally recommended for beginners. Or you can just read the
documentation that comes with perl (I learned Perl that way, although I
think I might have figured out some things faster if I had read the
O'Reilly books) or one of the gazillion other books about perl (although
Sturgeon's law applies to Perl books just as to SF novels).

As for the "funny lines of code": Fun is for me an intrinsic part of the
Perl culture. You may or may not appreciate Larry's sense of humor or
writing style, but if you don't enjoy playing with a language, Perl
probably isn't the language of your choice.

Look I know I am going to have to read the book no matter how much of
a frustrating read it is. However I do want to get this prototype
running prior to my intellectual investment. I don't want to read 900
pages when effectively the prototype I'm writing is going to say
whether the language is feasible or not, not my willingness or
unwillingness to learn it.

You won't know whether Perl is feasible for some (reasonably complex)
task until you know it fairly well. Hacking together a few lines of code
without understanding the language won't tell you that. If you do
succeed in writing a few lines, you still won't know if it is well
suited to more complex tasks. And when you don't succeed all you know is
that you haven't learned enough. Perl is a general purpose programming
language. You can write anything in Perl. But for some tasks, other
languages are better suited.

hp

PS: I recommend to read this: http://www.norvig.com/21-days.html
 
B

Ben Morrow

Quoth "Peter J. Holzer said:
Yup. That's one of the two rules to remember. The other is to use -> to
access individual members.

I'm currently considering writing a module that would allow the syntax

my @users = $self->{users}[];

instead[1]. Given that it will be rather tricky, would people actually
find such a thing useful, or would the 'effort' of loading a module be
enough to put people off using it?

Ben

[1] I originally did it as a patch for core perl, which was considerably
easier; the consensus on p5p was that such minor and not-obviously-
terribly-useful syntax changes should be tested on CPAN first, if at all
possible.
 
X

xhoster

Ben Morrow said:
Quoth "Peter J. Holzer said:
Yup. That's one of the two rules to remember. The other is to use -> to
access individual members.

I'm currently considering writing a module that would allow the syntax

my @users = $self->{users}[];

instead[1]. Given that it will be rather tricky, would people actually
find such a thing useful, or would the 'effort' of loading a module be
enough to put people off using it?

The module would have to be based on something like Filter::Util::Call,
right? I've heard bad things about the stability of that, which would put
me off using such a module on a regular basis. Other than that, I'd like
it very much.


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.
 
B

Ben Morrow

Quoth (e-mail address removed):
Ben Morrow said:
Quoth "Peter J. Holzer said:
If $x is a reference to an array, then @{ $x } dereferences it.

my @users = @{ $self->{users} };

Yup. That's one of the two rules to remember. The other is to use -> to
access individual members.

I'm currently considering writing a module that would allow the syntax

my @users = $self->{users}[];

instead[1]. Given that it will be rather tricky, would people actually
find such a thing useful, or would the 'effort' of loading a module be
enough to put people off using it?

The module would have to be based on something like Filter::Util::Call,
right? I've heard bad things about the stability of that, which would put
me off using such a module on a regular basis.

Source filters that try to heuristically parse Perl, or something like
it, (Switch being the canonical example) are typically unreliable, since
it's essentially impossible to parse Perl except by running it through
perl and looking at the resulting optree. Even PPI (a newish Perl module
that almost-perfectly parses Perl) isn't good enough, as there are cases
that simply cannot be parsed correctly without actually running the
code.

There are two ways I can see to avoid this. Devel::Declare gets around
the issue by (extremely hackishly) inserting hooks into the real parser,
which can then parse a short section of new syntax and return control.
While this has the advantage of using the real parser to parse
everything that isn't the new syntax, I'm not sure it can be done in
this case: I suspect the 'Syntax error' case fires too soon.

The alternative would be to ship my modified versions of toke.c and
perly.y (and derived files) with the module. This way I can compile
copies of yylex() and yyparse() that are actually correct, and still
call the functions in op.c to build the optree. Then I should be able to
install this parser as a source filter (in this case, one that simply
builds an optree and doesn't return any text to the 'real' parser, much
like ByteLoader) while avoiding the heuristics that usually make them so
unreliable.
Other than that, I'd like it very much.

OK, cool. As you can see, it's quite a bit of work, so I'm not making
any promises :).

Ben
 
P

Peter J. Holzer

Quoth "Peter J. Holzer said:
Yup. That's one of the two rules to remember. The other is to use -> to
access individual members.

I'm currently considering writing a module that would allow the syntax

my @users = $self->{users}[];

instead[1].

Nice. Is there a way to extend this to array slices?

my @users = $self->{users}[1, 3 .. 5]

But I guess that collides with existing valid syntax.
Given that it will be rather tricky, would people actually
find such a thing useful, or would the 'effort' of loading a module be
enough to put people off using it?

Ben

[1] I originally did it as a patch for core perl, which was considerably
easier; the consensus on p5p was that such minor and not-obviously-
terribly-useful syntax changes should be tested on CPAN first, if at all
possible.

I would find that very useful. The effort of loading a module wouldn't
put me off, instabilities introduces by a source-filter might, though.

hp
 
P

Peter Makholm

Philluminati said:
Oh wow it works perfectly.

Thank you Peter I really appreciate the time you've taken to answer my
post. :)

Note that it took less than an hour from you posted the right pieces
of code till you got an working answer. Before you posted the code
where you actually uses you package to get the error nobody would be
able to help without making som wild assumptions about what you were
doing.

Posting a complete minimal example showing you problem would have
helped you from the first post.

//Makholm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top