|
List Info
Thread: defined("foo") eq "foo"
|
|
| defined("foo") eq
"foo" |

|
2007-09-06 21:33:50 |
ISTM that this would be a handy shortcut :
if (defined("foo") eq "foo") {print
"DWIM"}
it wouldnt work as expected for ugly code like
if (defined 2 == 1) {},
but thats too ugly to be defensible coding style.
So, I tried patching pp_defined(),
in this (non-functional) patch.
what did I do wrong ?
|
|
|
| Re: defined("foo") eq
"foo" |

|
2007-09-06 21:42:31 |
On Sep 6, 2007, at 9:33 PM, Jim Cromie wrote:
> ISTM that this would be a handy shortcut :
>
> if (defined("foo") eq "foo")
{print "DWIM"}
Why would you actually want to do this?
And what about
if (defined("0"))
?
xoxo,
Andy
--
Andy Lester => andy petdance.com => www.petdance.com =>
AIM:petdance
|
|
| Re: defined("foo") eq
"foo" |

|
2007-09-06 22:10:14 |
On Sep 6, 2007, at 9:42 PM, Andy Lester wrote:
>
> On Sep 6, 2007, at 9:33 PM, Jim Cromie wrote:
>
>> ISTM that this would be a handy shortcut :
>>
>> if (defined("foo") eq "foo")
{print "DWIM"}
>
> Why would you actually want to do this?
Probably to do something like
if (defined($x) and $x eq 'foo') { print
"DWIM"}
which with // can be written
if (($x // '') eq 'foo') { print "DWIM"}
> And what about
>
> if (defined("0"))
or
if (defined(''))
both would be a problem and is a very good read why not to
change.
Graham.
|
|
| Re: defined("foo") eq
"foo" |

|
2007-09-06 23:27:01 |
Jim Cromie wrote:
>
> ISTM that this would be a handy shortcut :
>
> if (defined("foo") eq "foo")
{print "DWIM"}
Uhh, WDYM? (What Do You Mean)
--
There will be snacks.
|
|
| Re: defined("foo") eq
"foo" |

|
2007-09-07 00:27:01 |
Graham Barr wrote:
> On Sep 6, 2007, at 9:42 PM, Andy Lester wrote:
>>
>> On Sep 6, 2007, at 9:33 PM, Jim Cromie wrote:
>>
>>> ISTM that this would be a handy shortcut :
>>>
>>> if (defined("foo") eq
"foo") {print "DWIM"}
>>
>> Why would you actually want to do this?
>
> Probably to do something like
>
> if (defined($x) and $x eq 'foo') { print
"DWIM"}
>
this is precisely what prompted the experiment.
> which with // can be written
>
> if (($x // '') eq 'foo') { print "DWIM"}
>
>> And what about
>>
>> if (defined("0"))
>
> or
>
> if (defined(''))
>
> both would be a problem and is a very good read why not
to change.
>
> Graham.
>
>
SvTRUE takes care of it - from perlapi,
SvTRUE Returns a boolean indicating whether Perl would
evaluate the SV
as true or false, defined or undefined. Does not handle
’get’ magic.
BTW- that wording seems somewhat ambiguous.
so in the patch, 'true' vals are returned as is (ex:
"foo")
and false but defined are PL_sv_yes.
The patch passes most tests, failing on these:
Failed 30 tests out of 1426, 97.90% okay.
../ext/Devel/PPPort/t/call.t
../ext/Encode/t/Encode.t
../ext/Hash/Util/FieldHash/t/01_load.t
../ext/IO/t/io_const.t
../ext/List/Util/t/first.t
../ext/List/Util/t/max.t
../ext/List/Util/t/maxstr.t
../ext/List/Util/t/min.t
../ext/List/Util/t/minstr.t
../ext/List/Util/t/openhan.t
../ext/List/Util/t/p_first.t
../ext/List/Util/t/p_max.t
../ext/List/Util/t/p_maxstr.t
../ext/List/Util/t/p_min.t
../ext/List/Util/t/p_minstr.t
../ext/List/Util/t/p_openhan.t
../ext/threads/shared/t/wait.t
../lib/Archive/Tar/t/02_methods.t
../lib/DBM_Filter/t/01error.t
../lib/DBM_Filter/t/02core.t
../lib/DBM_Filter/t/encode.t
../lib/Math/BigInt/t/bare_mbi.t
../lib/Math/BigInt/t/bigintpm.t
../lib/Math/BigInt/t/sub_mbi.t
../lib/Math/BigRat/t/bigrat.t
../lib/Math/BigRat/t/bigratpm.t
../lib/Math/BigRat/t/trap.t
../lib/Pod/Simple/t/search05.t
../lib/Test/Harness/t/assert.t
../lib/Test/Simple/t/use_ok.t
### Since not all tests were successful, you may want to run
some of ...
The 1st 2 failures are expected, given the tests -
ok( defined"foo", 1, ".......")
and your acid tests above also work:
[jimc harpo defret]$ ./perl -Ilib -e 'print defined 0,
defined "",
defined "0", "n"'
111
All that said, I suppose its not strictly backwards
compatible,
so I'll drop it. Im satisfied that it 'works'.
heres the patch anyway.
|
|
|
| Re: defined("foo") eq
"foo" |

|
2007-09-07 00:52:51 |
aww crap - that last patch had superflous stuff in it.
here it is again, de-cluttered.
[jimc harpo defret]$ ./perl -Ilib -e
'$_="junkn"; print if defined "foo"'
junk
BTW, I see nothing in `perldoc -f defined` that specifies
the return value.
quoting:
So you should use "defined" only when you’re
questioning the integrity
of what you’re trying to do. At other times, a simple
comparison to 0 or
"" is what
you want.
|
|
|
| Re: defined("foo") eq
"foo" |

|
2007-09-07 01:33:22 |
Jim Cromie wrote:
> Failed 30 tests out of 1426, 97.90% okay.
> ../ext/Devel/PPPort/t/call.t
> ../ext/Encode/t/Encode.t
> ../ext/Hash/Util/FieldHash/t/01_load.t
> ../ext/IO/t/io_const.t
> ../ext/List/Util/t/first.t
> ../ext/List/Util/t/max.t
> ../ext/List/Util/t/maxstr.t
> ../ext/List/Util/t/min.t
> ../ext/List/Util/t/minstr.t
> ../ext/List/Util/t/openhan.t
> ../ext/List/Util/t/p_first.t
> ../ext/List/Util/t/p_max.t
> ../ext/List/Util/t/p_maxstr.t
> ../ext/List/Util/t/p_min.t
> ../ext/List/Util/t/p_minstr.t
> ../ext/List/Util/t/p_openhan.t
> ../ext/threads/shared/t/wait.t
> ../lib/Archive/Tar/t/02_methods.t
> ../lib/DBM_Filter/t/01error.t
> ../lib/DBM_Filter/t/02core.t
> ../lib/DBM_Filter/t/encode.t
> ../lib/Math/BigInt/t/bare_mbi.t
> ../lib/Math/BigInt/t/bigintpm.t
> ../lib/Math/BigInt/t/sub_mbi.t
> ../lib/Math/BigRat/t/bigrat.t
> ../lib/Math/BigRat/t/bigratpm.t
> ../lib/Math/BigRat/t/trap.t
> ../lib/Pod/Simple/t/search05.t
> ../lib/Test/Harness/t/assert.t
> ../lib/Test/Simple/t/use_ok.t
> ### Since not all tests were successful, you may want
to run some of ...
>
>
> The 1st 2 failures are expected, given the tests -
>
> ok( defined"foo", 1, ".......")
I'd like to see specifically what failed, that would give an
idea of just what
the consequences of this patch are. I'm surprised at the
Test::Simple
failure. That test only checks for whether defined &sub
is true or false.
--
Robrt: People can't win
Schwern: No, but they can riot after the game.
|
|
| Re: defined("foo") eq
"foo" |

|
2007-09-07 02:47:05 |
Michael G Schwern wrote:
> Jim Cromie wrote:
>
>> Failed 30 tests out of 1426, 97.90% okay.
>> ../ext/Devel/PPPort/t/call.t
>> ../ext/Encode/t/Encode.t
>> ../ext/Hash/Util/FieldHash/t/01_load.t
>> ../ext/IO/t/io_const.t
>> ../ext/List/Util/t/first.t
>> ../ext/List/Util/t/max.t
>> ../ext/List/Util/t/maxstr.t
>> ../ext/List/Util/t/min.t
>> ../ext/List/Util/t/minstr.t
>> ../ext/List/Util/t/openhan.t
>> ../ext/List/Util/t/p_first.t
>> ../ext/List/Util/t/p_max.t
>> ../ext/List/Util/t/p_maxstr.t
>> ../ext/List/Util/t/p_min.t
>> ../ext/List/Util/t/p_minstr.t
>> ../ext/List/Util/t/p_openhan.t
>> ../ext/threads/shared/t/wait.t
>> ../lib/Archive/Tar/t/02_methods.t
>> ../lib/DBM_Filter/t/01error.t
>> ../lib/DBM_Filter/t/02core.t
>> ../lib/DBM_Filter/t/encode.t
>> ../lib/Math/BigInt/t/bare_mbi.t
>> ../lib/Math/BigInt/t/bigintpm.t
>> ../lib/Math/BigInt/t/sub_mbi.t
>> ../lib/Math/BigRat/t/bigrat.t
>> ../lib/Math/BigRat/t/bigratpm.t
>> ../lib/Math/BigRat/t/trap.t
>> ../lib/Pod/Simple/t/search05.t
>> ../lib/Test/Harness/t/assert.t
>> ../lib/Test/Simple/t/use_ok.t
>> ### Since not all tests were successful, you may
want to run some of ...
>>
>>
>> The 1st 2 failures are expected, given the tests -
>>
>> ok( defined"foo", 1,
".......")
>>
>
> I'd like to see specifically what failed, that would
give an idea of just what
> the consequences of this patch are. I'
btw - I trust the WDYM is clear at this point ..
[jimc harpo defret]$ ./perl -Ilib
lib/Test/Simple/t/use_ok.t
1..13
ok 1 - use Symbol;
ok 2 - use_ok() no args exports defaults
ok 3 - use Symbol;
ok 4 - one arg, defaults overriden
Bizarre copy of CODE in aassign at lib/Test/More.pm line
260.
# Looks like you planned 13 tests but only ran 4.
# Looks like your test died just after 4.
so, adding -d, setting breakpoint,
Foo::two::(lib/Test/Simple/t/use_ok.t:26):
26: ::ok( defined &qualify, ' right
function exported' );
DB<5> s
Test::More::ok(lib/Test/More.pm:260):
260: my($test, $name) = _;
DB<5> n
Bizarre copy of CODE in aassign at lib/Test/More.pm line
260.
# Looks like you planned 13 tests but only ran 4.
# Looks like your test died just after 4.
R-repeating,
Foo::two::(lib/Test/Simple/t/use_ok.t:26):
26: ::ok( defined &qualify, ' right
function exported' );
DB<5> s
Test::More::ok(lib/Test/More.pm:260):
260: my($test, $name) = _;
DB<5> s
Test::Builder::CODE(0x87693fc)(lib/Test/Builder.pm:1679):
1679: my $in_eval = 0;
DB<5> T
$ =
Test::Builder::__ANON__[lib/Test/Builder.pm:1684]('Bizarre
copy of
CODE in aassign at lib/Test/More.pm line 260.^J') called
from file
`lib/Test/More.pm' line 260
. = Test::More::ok('$;$', ' right function exported')
called from file
`lib/Test/Simple/t/use_ok.t' line 26
one more time..
DB<4> l
26==>b ::ok( defined &qualify, ' right
function exported' );
27 }
DB<4> s
Test::More::ok(lib/Test/More.pm:260):
260: my($test, $name) = _;
DB<4> x _
Bizarre copy of CODE in leaveeval at lib/perl5db.pl line
638.
This looks quite internal ..
z <;> nextstate(Foo::two 782 use_ok.t:26) v:{
10 <0> pushmark s
11 <#> gv[*Foo::two::qualify] s
12 <1> rv2cv[t15] sK*/AMPER,32
13 <1> defined sKM/1
14 <$> const[PV " right function exported"]
sM
15 <#> gv[*ok] s
16 <1> entersub[t16] vKS/TARG,1
17 <2> leaveloop vK/2
does anyone see a problem with any of the flags on these ops
?
something to do with refcounting ?
|
|
| Re: defined("foo") eq
"foo" |

|
2007-09-07 06:24:53 |
On Sep 7, 2007, at 12:27 AM, Jim Cromie wrote:
>> which with // can be written
>>
>> if (($x // '') eq 'foo') { print "DWIM"}
>>
>>> And what about
>>>
>>> if (defined("0"))
>>
>> or
>>
>> if (defined(''))
>>
>> both would be a problem and is a very good read why
not to change.
>>
>> Graham.
>>
>>
>
> SvTRUE takes care of it - from perlapi,
No it does not.
You cannot make if(defined($x) eq $x) and if(defined($x))
both
always have the same result for every value of $x
> SvTRUE Returns a boolean indicating whether Perl would
evaluate the
> SV as true or false, defined or undefined. Does not
handle ’get’
> magic.
Which means it does not handle overloaded objects.
Graham.
|
|
| Re: defined("foo") eq
"foo" |

|
2007-09-10 12:14:31 |
the "it" proposal was to have a new LNV (such as
$^it) which would be a
non-magical copy of whatever was the argument of the most
recent call
to exists or defined. (exactly how non-magical would it be
would have to be
talked out.)
Implementing it involves adding a couple lines to pp_exists
and pp_defined
which don't do anything but slow down the general case.
$^it would work here:
if (defined($x) and $x eq 'foo') { print
"DWIM"}
could become
if (defined($x) and $^it eq 'foo') { print
"DWIM"}
which becomes advantageous when $x is something expensive
to look up, like a tied database fetch.
Of course one can get the same result, without using any new
syntax,
with
if (defined(my $_tmpx=$x) and $_tmpx eq 'foo') { print
"DWIM"}
so I'm going to stop now and doubt the wisdom of having
started. ;)
|
|
[1-10]
|
|