Concise idiom sought

A

Anno Siegel

1. $$v=1 if (!defined $v=\$base_ref->{pl});

What is that supposed to do?

As given it produces a run-time error. After adding parentheses for
precedence

$$v=1 if (!defined( $v=\$base_ref->{pl}));

it will set $base_ref{ pl} to 1, no matter what.
2. if (!defined $base_ref->{pl}) {
$base_ref->{pl} = 1;
}

Would seem 1. incurrs an overhead assignment
that 2. doesen't if $base_ref->{pl} is defined.

Huh?

Anno
 
T

Tim Kazner

What is that supposed to do?

As given it produces a run-time error. After adding parentheses for
precedence

$$v=1 if (!defined( $v=\$base_ref->{pl}));

it will set $base_ref{ pl} to 1, no matter what.


Huh?

Anno

Revised, however the conclusion remains the same:

1. $$v=1 if ($v=\$base_ref->{pl} and !defined $$v);
2. if (!defined $base_ref->{pl}) {
$base_ref->{pl} = 1;
}

Would seem 1. incurrs an overhead assignment and two de-references
that 2. doesen't if $base_ref->{pl} is defined.

==============================

use strict;
use warnings;

my ($base_ref,$v) = ({});

$$v=1 if ($v=\$base_ref->{pl} and !defined $$v);
print "$$v\n";

($base_ref,$v) = ({pl=>8});

$$v=1 if ($v=\$base_ref->{pl} and !defined $$v);
print "$$v\n";

__END__
1
8

Not really sure why this won't work:

$$v=1 if (!defined $($v=\$base_ref->{pl}));
 
A

Anno Siegel

[...]

use strict;
use warnings;

my ($base_ref,$v) = ({});

$$v=1 if ($v=\$base_ref->{pl} and !defined $$v);
print "$$v\n";

($base_ref,$v) = ({pl=>8});

$$v=1 if ($v=\$base_ref->{pl} and !defined $$v);
print "$$v\n";

__END__
1
8

What is the advantage over

defined or $_ = 1 for $base_ref->{pl};

Especially considering that $v is undeclared. That would also have
to be done somewhere.
Not really sure why this won't work:

$$v=1 if (!defined $($v=\$base_ref->{pl}));

That fails because de-referencing doesn't take an arbitrary expression.
You must give it a simple variable or a block:

$$v=1 unless defined ${ $v=\$base_ref->{pl} };

Again, what is the possible advantage?

Anno
 
T

Tim Kazner

<Tim Kazner> wrote in comp.lang.perl.misc:
Randal L. Schwartz schreef:

defined $_ or $_ = 1 for $base_ref->{pl};
[...]

use strict;
use warnings;

my ($base_ref,$v) = ({});

$$v=1 if ($v=\$base_ref->{pl} and !defined $$v);
print "$$v\n";

($base_ref,$v) = ({pl=>8});

$$v=1 if ($v=\$base_ref->{pl} and !defined $$v);
print "$$v\n";

__END__
1
8

What is the advantage over

defined or $_ = 1 for $base_ref->{pl};

Especially considering that $v is undeclared. That would also have
to be done somewhere.
Yes it might be something to benchmark.

Implicit alias for lvalue works too. $_ and $v are both asignments.
Not really sure but 'for' may have more overhead than 'if'
and aliasing may be internal shorthand so that $_ ~ $$v;

Either way, both these

defined or $_ = 1 for $base_ref->{pl};
$$v=1 unless defined ${ $v=\$base_ref->{pl} };

would seem to incurr an overhead asignment that

if (!defined $base_ref->{pl}) {
$base_ref->{pl} = 1;
}

doesen't, if $base_ref->{pl} is defined.
 
I

it_says_BALLS_on_your_forehead

Anno said:
<Tim Kazner> wrote in comp.lang.perl.misc:
Randal L. Schwartz schreef:

defined $_ or $_ = 1 for $base_ref->{pl};
[...]

use strict;
use warnings;

my ($base_ref,$v) = ({});

$$v=1 if ($v=\$base_ref->{pl} and !defined $$v);
print "$$v\n";

($base_ref,$v) = ({pl=>8});

$$v=1 if ($v=\$base_ref->{pl} and !defined $$v);
print "$$v\n";

__END__
1
8

What is the advantage over

defined or $_ = 1 for $base_ref->{pl};

Especially considering that $v is undeclared. That would also have
to be done somewhere.
Not really sure why this won't work:

$$v=1 if (!defined $($v=\$base_ref->{pl}));

That fails because de-referencing doesn't take an arbitrary expression.
You must give it a simple variable or a block:

$$v=1 unless defined ${ $v=\$base_ref->{pl} };

Again, what is the possible advantage?

might take you one step closer to winning an obfuscation award?
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top