Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Wierd error message

by herby1620 (Monk)
on Aug 16, 2006 at 01:57 UTC ( [id://567576]=perlquestion: print w/replies, xml ) Need Help??

herby1620 has asked for the wisdom of the Perl Monks concerning the following question:

In diagnosing some error problems, I made up a TIE module for printing. It works quite well, but does "wierd things" in one instance. The routine that is called says:

Use of uninitialized value in split at <routine/line> during global destruction.

The code is pretty simple (reconstructed a bit):

sub fool { my @vals; my $n; my $i; while (@_) { @vals = split /(\n)/, (shift); # Error happens here $n = @vals; for ($i = 0; $i < $n; $i++) { print "The value is: $vals[$i] \n"; # Or similar action } } }
My question: how do I detect that this error is GOING to happen with the called arguments (in this case to the 'fool' routine) so I can print out a more intelligent error message?

Is there any way I can "produce" this same error message using the 'fool' routine as shown above. It would "enlighten" me quite a bit.

Replies are listed 'Best First'.
Re: Wierd error message
by graff (Chancellor) on Aug 16, 2006 at 03:00 UTC
    Here is one way to generate that sort of warning message (there are many other ways):
    my @array=qw/foo bar/; $array[3] = "baz"; foo1( @array ); # $array[2] is undefined

    One way to avoid the warning would be:

    for my $param (grep {defined} @_) { for my $val ( split /\n/, $param ) { print "The value is: $val\n"; # or whatever you want to do } }
    (I took the liberty of eliminating unnecessary variables, and localizing the scope of needed variables a little more tightly.)

    The other methods suggested above would also work, I expect. There might be reasons for preferring one approach over the other, depending on the rest of your application.

      Reply most helpful. I was able to make the proper tests, and incorporate the detection of "undefined" stuff in my code.

      Sorry if the example isn't that good. I tried to extract the pertinant stuff for the example. The intended application is a "tie" method for output (prints). It replaces the output method to stdout, and adds the time, and module/line as a prefix. It seems to work quite well, as we've put it in a bunch of scripts here. Efficient? I don't know, it does the job, and really isn't called that much. I suspect that being my first module, it refelcts my background ("...you can program in Fortran in any language.").

Re: Wierd error message
by Zaxo (Archbishop) on Aug 16, 2006 at 02:19 UTC

    Your line eight should be something like,

    @vals = split /(\n)/, $_;

    I don't understand what the "fool" routine is. You have a very straightforward loop, done in C-style. Perhaps if you included some of the Tie code?

    After Compline,
    Zaxo

      This use of while doesn't seem to set $_ for me. Which version of Perl are you using?

        I was thinking of while (<$fh>) { }, which does.

        After Compline,
        Zaxo

Re: Wierd error message
by roboticus (Chancellor) on Aug 16, 2006 at 02:18 UTC
    herby1620--

    I'd be similarly "enlightened" if I could see a real example--a small complete example--that I could just run to see the error message. So please bear with me, as my ESP::Clairvoyance module doesn't compile at the moment.

    If I had to guess, (and I do must!), I'd suspect that shift is returning an undef when it runs out of items, and that is the source of your error...

    UPDATE: perhaps changing

    @vals = split /(\n)/, (shift); # Error happens here
    to something like:
    @vals = split /(\n)/, (shift)||'';
    might help. (I don't know if it'll even compile, but the intent is to provide split *something* to work with if shift returns undef...)

    UPDATE: As ikegami states below, my attempted "fix" is quite bogus. Luckily, he also had a better suggestion. (Had I refreshed before posting my prior update, I'd've probably been spared the embarassment! Thanks Icky! 8^)

    --roboticus

Re: Wierd error message
by ikegami (Patriarch) on Aug 16, 2006 at 02:21 UTC
    Perhaps adding next if not defined $_[0]; as the first line of the while loop body? It's hard to tell if there's a better approach or not without more complete (working?) example.
Re: Wierd error message
by jdporter (Paladin) on Aug 16, 2006 at 04:25 UTC

    If you have 'some weird error', the problem is probably with your frobnitzer. — #11938

    :-)

    We're building the house of the future together.
Re: Wierd error message
by ysth (Canon) on Aug 16, 2006 at 15:22 UTC
    In general, you can avoid that kind of error message by not putting objects into global variables (or tieing globals, which is a sideways way of putting in an object), at least without manually clearing/untieing them at some point.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://567576]
Approved by friedo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-19 01:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found