develooper Front page | perl.perl5.porters | Postings from December 2007

Re: "strict refs" not strict enough

Thread Previous | Thread Next
From:
Michael G Schwern
Date:
December 29, 2007 12:35
Subject:
Re: "strict refs" not strict enough
Message ID:
4776AF8B.5090306@pobox.com
Mark Jason Dominus wrote:
> I have been thinking about this for a long time, and it has often
> seemed to me that "strict refs" was not as helpful as it could be.

I agree with the problem you've outlined, in theory, but I worry about one
thing...


> #1 and #3 could be solved by providing a trivial function,
> "ref2string", which explicitly converts a reference to a string
> without raising a fatal error.  So instead of this:
> 
>         my $obj = Doggie->new(...);
>         warn "Manufactured object '$obj'";
> 
> one would have this:
> 
>         my $obj = Doggie->new(...);
>         warn "Manufactured object '" . ref2string($obj) . "'";

I'm not sure I want to worry about mere interpolation throwing a run-time
error.  Maybe a warning.  Right now you have to work hard to get string
interpolation to blow up at run time.

I'm pretty sure I can come up with some legit cases where
$i_really_dont_care_if_this_is_a_reference_or_not is dropped into a string.
As I've learned, one easy way is to implement the stricture/warning and then
run it on some existing code. :)

Now instead of:

	print "Blah blah $foo blah blah";

I need:

	printf "Blah blah %s blah blah",
		ref $foo ? ref2string($foo) : $foo;

Or I risk a run-time error.

And what if $obj has string overloading?  Presumably that's safe, but now I
need more than a simple ref $obj check if I want to use J Random Scalar in a
string.  I guess overload::StrVal() does what I mean.

	use overload;
	print "Blah blah @{[ overload::StrVal($foo) ]} blah blah";

Either way, its sand in the gears of Perl's implicit type conversions with
implications well beyond the simple foo("$ref") mistake.


-- 
Reality is that which, when you stop believing in it, doesn't go away.
    -- Phillip K. Dick

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About