Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

From your various posts in this thread:

I'm dealing with large hash structures and big input data ... [validation] is highly inefficient

Please clarify. How big and deep are your data structures? Did you try running the code I posted here on your data, and how long did it take?

Also, how are you accessing your data structure? Directly, as in printf "%s", $data->{foo}{bar}{quz}{baz}, do you copy subsections into temporary variables, do you walk the structure recursively, etc.? Again, please see SSCCE, if you could show us something that's actually representative of your code, we can give better advice.

I don't want to add any kind of extra checking code to avoid the error ... I just want to know if it's possible (without writing any specific checking code) to know the name of the variable that produces the error

I believe that Rolf showed (Update: now even including a post by the P5Porter who implemented it himself!) conclusively that in many cases, this isn't built into Perl, so the answer to your question is no. You'll have to add the checks yourself, even if you "don't want to" :-P

You could check the arguments:

use Carp qw/cluck/; sub myprintf { cluck "myprintf: undef argument(s)" if grep {!defined} @_; printf @_ } myprintf "%s-%s-%s-%s\n", 'quz', undef, 'baz', undef; __END__ myprintf: undef argument(s) at - line 4. main::myprintf("%s-%s-%s-%s\x{a}", "quz", undef, "baz", undef) cal +led at - line 7 Use of uninitialized value in printf at - line 5. Use of uninitialized value in printf at - line 5. quz--baz-

Or you could check for undefs when accessing the data:

use Carp; sub dive { my ($ref,@path) = @_; for my $i (0..$#path) { $ref = $path[$i]=~/^\[(\d+)\]$/ ? $ref->[$1] : $ref->{$path[$i]}; if (!defined $ref) { carp "undef at: ".join('', map {/^\[\d+\]$/?$_:"{$_}"} @path[0..$i]); return undef; } } return $ref; } my $data = { this => { is => { a => { deep => { structure => [ 'abc', 'def', undef ] } } } } }; printf "<%s>\n", dive($data, qw/ this is a deep structure [0] /); printf "<%s>\n", dive($data, qw/ this is a deep structure [3] /); printf "<%s>\n", dive($data, qw/ this is an example /); __END__ <abc> undef at: {this}{is}{a}{deep}{structure}[3] at - line 10. main::dive(HASH(0x9e47a4), "this", "is", "a", "deep", "structure", + "[3]") called at - line 22 Use of uninitialized value in printf at - line 22. <> undef at: {this}{is}{an} at - line 10. main::dive(HASH(0x9e47a4), "this", "is", "an", "example") called a +t - line 23 Use of uninitialized value in printf at - line 23. <>

In reply to Re: determine the variable causing the error: Use of uninitialized value by haukex
in thread determine the variable causing the error: Use of uninitialized value by ruqui

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2022-05-16 04:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (62 votes). Check out past polls.

    Notices?