Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Re: bad code to good golf
Uri Guttman wrote:
> is there any guarantee of evaluation order in arg lists? will the
> bless/splice always be executed before the pop?

I believe it is undefined, so it's not recommended that you do anything orde
r
dependent in the argument list.  But it is stable.

As implemented, "complex" expressions are evaluated first, then simple ones.
This leads to classic oddities like this:

perl -wle 'sub foo { print join " ", @_ }  $i = 1;  foo($i, $i++, $i++)'
3 1 2

This is not specific to Perl, but I believe a C thing as well.


> my longer but amusing answer is this:
>
> sub new {
> 	bless{reverse%{{reverse@_}}}
> }
>
> it is not warning safe and it leaves '' => CLASS in the hash. but it has
> no temp vars, calls to splice or shift.

You fell into the trap!  That's an improper constructor, can you tell us why
?


--
Robrt:   People can't win
Schwern: No, but they can riot after the game.

Report this thread to moderator Post Follow-up to this message
Old Post
Michael G Schwern
12-10-07 12:35 AM


Re: bad code to good golf
>>>>> "MGS" == Michael G Schwern <schwern@pobox.com> writes:

MGS> Uri Guttman wrote: 

MGS> I believe it is undefined, so it's not recommended that you do
MGS> anything order dependent in the argument list.  But it is stable.

so that breaks the pop solutions IMO. it may not always work in all perl
versions.
 

MGS> You fell into the trap!  That's an improper constructor, can you tell u
s why?

i forgot the shift of the class arg which i had on my test code. it was
a paste error. and it makes it even longer.

uri

--
Uri Guttman  ------  uri@stemsystems.com  -------- [url]http://www.stemsystems.com[/url
]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding
-
Search or Offer Perl Jobs  ----------------------------  [url]http://jobs.perl.org[/url
]

Report this thread to moderator Post Follow-up to this message
Old Post
Uri Guttman
12-10-07 12:35 AM


Re: bad code to good golf

Report this thread to moderator Post Follow-up to this message
Old Post
shmem
12-10-07 01:31 PM


Re: bad code to good golf
* shmem <georg.moritz@millenux.com> [2007-12-10 12:55]:
> Well, if we were to return an array reference instead of a hash
> ref,
>
> sub new{bless\@_,shift}
>
> works. Why does the shift get executed before an array
> reference is constructed - but not if a hashref is constructed
> - from an array?

It probably doesn’t. Either way, evaluation order is a red
herring: your construction will work regardless of which
subexpression is evaluated first!

You managed to confuse yourself. Congratulations. :-)  (Or you
are underhandedly trying to confuse us. In that case, sorry bub,
better luck next time. :-) )

What happens is that `\` takes a *reference* to the array. And
of course when you do that, any modifications of the referent,
including *subsequent* modifications, will be seen by anyone who
holds a reference to it.

In contrast, the hash constructors make a *copy* of the array,
and that copy will not be affected but subsequent modifications
to the source array.

Anyone who has trouble following should consider the difference
between the following two:

sub new { bless \@_, shift }
# vs
sub new { bless [@_], shift }

Consider how many arrays are involved in either case, and which
one is affected by the `shift`.

--
*AUTOLOAD=*_;sub _{s/(.*)::(.*)/print$2,(",$\/"," ")[defined wantarray]/e;$1
}
&Just->another->Perl->hack;
#Aristotle Pagaltzis // <http://plasmasturm.org/>

Report this thread to moderator Post Follow-up to this message
Old Post
A. Pagaltzis
12-10-07 01:31 PM


Re: bad code to good golf
>>>>> "AP" == A Pagaltzis <pagaltzis@gmx.de> writes:

AP> * shmem <georg.moritz@millenux.com> [2007-12-10 12:55]: 

AP> In contrast, the hash constructors make a *copy* of the array,
AP> and that copy will not be affected but subsequent modifications
AP> to the source array.

and that is beside that point that it totally is broken. the specified
API is a list of key/value pairs. how would an array of those be used as
an object? i don't feel like doing a linear search to get arg values. if
i wanted that i would use lisp assoc lists! :)

uri

--
Uri Guttman  ------  uri@stemsystems.com  -------- [url]http://www.stemsystems.com[/url
]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding
-
Search or Offer Perl Jobs  ----------------------------  [url]http://jobs.perl.org[/url
]

Report this thread to moderator Post Follow-up to this message
Old Post
Uri Guttman
12-10-07 01:31 PM


Re: bad code to good golf
On Dec 10, 2007 3:53 AM, shmem <georg.moritz@millenux.com> wrote:
> Well, if we were to return an array reference instead of a hash ref,
>
> sub new{bless\@_,shift}
>
> works. Why does the shift get executed before an array reference is
> constructed - but not if a hashref is constructed - from an array?

No it doesn't. It breaks in various and subtle ways, at least on a
couple versions of Perl that I used.

I actually did this once. (The class was representing vectors to
overload various operators, so it made sense to use a blessed array
ref.) The problem is that perl doesn't really refcount @_, so \@_
gives you a very unstable object whose contents are apt to change
underneath you for no apparent reason. I think you're probably fine as
long as you're within the call that the @_ belongs to, but if you ever
return it, weird stuff happens.

I can't come up with a simple test case for it. But it seems
reasonable that "fixing" the problem would slow everything down, and
in this case I'd rather have the speed. I'm not convinced that it's
really a bug anyway, as long as it doesn't crash. Making a copy is
what I really intended with that code; I didn't *want* a reference to
an argument list. I'm not even sure what that would mean.

Report this thread to moderator Post Follow-up to this message
Old Post
Steve Fink
12-11-07 12:34 AM


Re: bad code to good golf

Report this thread to moderator Post Follow-up to this message
Old Post
shmem
12-14-07 12:37 AM



cybers ex chat room]xxxv 
ideo sites[/URL]
melinda duckett por nvideo teen orgasmv ideo fat joe make it rainr emix video xxxv ideo clips [ur
l=http://fatbangs.net/adul-tvideo-forum.html]adul tvideo forum[/url] free online xx xvideo sexdoll free online xx xvideo asia nporn live se xwebcam teen videog ame watch free adul twebcam sex
 lives ex webcam sites free porn videoc lips pictures of
f at chicks live se xvideo thre efat chicks plum pchicks[/ur
l] [url=http://fatbangs.net/livesexact.html]livesexact hardcorev ideo teen 
titans thev ideo game chubbychick adult video pproduction

free fulll ength xxx video]
tee nviolence and video games[/URL]
adult videog allery vedi ogirls video clips ofp orn video gam eporn liveadultvideochat hot pornv ideo adultv ideo blog live webc am sex free xxx vide ofeeds livel esbian sex teenv ideo chat rooms free sex girl son webcam maturev ideos[/ur
l] [url=http://fatbangs.net/adult-vide-osamples.html]adult vide osamples amateu rbang xxxp orn fatm ovie webcam sexj asmine xxx videog allery teennude adult por nvideo blond echick
s miss teen us video[/U
RL]
[URL=[url=http://fatbangs.net/free-porn-vide-osites.html]free porn vide osites]a
dultf ilms[/URL]

Report this thread to moderator Post Follow-up to this message
Old Post
Doaeertaplen3
02-25-08 06:20 AM


Sponsored Links




Last Thread Next Thread Next
Pages (2): « 1 [2]
Search this forum -> 
Post New Thread

PERL Tricks archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 12:40 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.