develooper Front page | perl.perl5.porters | Postings from September 2000

Re: [ID 20000926.002] substr with undef values is not consistant

Thread Previous | Thread Next
From:
Clinton A. Pierce
Date:
September 26, 2000 20:14
Subject:
Re: [ID 20000926.002] substr with undef values is not consistant
Message ID:
39D16592.8433332B@geeksalad.org
Rick Delaney wrote:
> This is exactly what you get for substr("", 0) and substr("", 1), which
> makes sense since that's what the "Use of uninitialized value" warning
> that you've turned off is telling you.  You also get the same for
> substr("x",1) and substr("x",2) so this seems like a logical
> progression.

Hmm...doesn't seem logical to me.  I wasn't interested in the warning at
all. I knew very well I could be playing with data that wasn't
initialized.  In fact, I was counting on it in some cases.

I wanted the behavior of substr to be consistant with undef values.  If
something is undef, its undef at the beginning and everywhere else
(undefined has an anywhere else?!  It has a beginning?).  It's just
undefined, period.

What I did was something like [grueling description follows]:

* In an XML document 'element/birthDate' could represent an element
which may or may not exist.  In this case birthDate is simply a mapping
to a C structure which has the date stored as YYYYMMDD.

*  The XML document was handed off to a templating system where the
element extracted and (if necessary) was split up into component fields
and inserted into a  form:

	print fill_tags(<<EOF, $xmldoc);
	Year: <#get id="element/birthDate,0/4">
	Month: <#get id="element/birthDate,4/2">
	Day: <#get id="element/birthDate,6/2">
	EOF

* The templating system would
	1 extract the element specified from the XML object or
          yeild undef if the element does not exist.
	2 take the substring of the element (if needed).  [If the element
          was undef, then the substring would be undef as well...it
seemed]
	3 pass this to the insertion routine which must fill in fields
          with values, if the value is undef then simply use a null
 	  string as undef is unacceptable.  :)

I was generalizing substeps (1,3) to work for all elements, not just
those that needed to be split up.  I broke (2) out as a special case
midway and just let the undef-or-result fall through the rest of the
code.
         
 
> But a question remains.  Why is
>     no warnings qw(uninitialized);
> 
> suppressing the warning,
>     substr outside of string

I guess some things are more uninitialized than others.  And there's no
bug here you say?

-- 
    Clinton A. Pierce              Teach Yourself Perl in 24 Hours! 
  clintp@geeksalad.org         for details see http://www.geeksalad.org
"If you rush a Miracle Man, 
	you get rotten Miracles." --Miracle Max, The Princess Bride

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