Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
"be consistent"
 
PerlMonks  

unitialized value in subroutine entry

by Anonymous Monk
on Jun 27, 2006 at 02:42 UTC ( [id://557714]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

in a module, i have a routine. it has a "private function" (an anonymous routine).

if i print the anonymous routine as $<>; perl says it is a CODE ref; so that's good.

when the routine is invoked with &$<>; i get the following :

Use of uninitialized value in subroutine entry at ... and Can't use string ("") as a subroutine ref while "strict refs" in u +se at ...

i could not find a good description of what these messages mean.

thanks

SIMPLE CASE :

sub A { if ( exists $OBJ->{$p} ) { my $q = $OBJ->{$p}; $q = &$<package_name>::formatThis($q); print FH "$p=$q "; } my $formatThis = sub { my $x = @_; my $y = sprintf ( "%.2f", $x); return $y; }; }

Formatting fixed by GrandFather

Replies are listed 'Best First'.
Re: unitialized value in subroutine entry
by madtoperl (Hermit) on Jun 27, 2006 at 03:03 UTC
    Hi Ananymous Monk,

    These like issues i faced previously when you give use strict and some initialization issues,So you remove the use Strict; statement in the code and run it.Othersiwe do the proper initialization there.Hope this will solve your issue.

    Thanks and Regards,
    madtoperl.
      yes, i am trying to identify exactly what are the initialization issues. the anonymous routine is code; and the first invocation has a valid parameter. in addition; "no strict" did not hide these messages. thanks
      So you remove the use Strict; statement in the code and run it.

      Ack! Tppppbbbbttt! If code won't run under strict, then turning strict off is not the way to fix it. If you do that, the real, underlying problem will still be there, you just won't get told about it - until the code blows up in your face.

Re: unitialized value in subroutine entry
by reneeb (Chaplain) on Jun 27, 2006 at 03:06 UTC
    The variable $formatThis is not known as a "package-variable", because it just a variable for the subroutine. You can do it this way:
    #!/usr/bin/perl use strict; use warnings; my $OBJ = {test => 3}; my $p = 'test'; A(); sub A { my $formatThis = sub { my ($x) = @_; my $y = sprintf ( "%.2f", $x); return $y; }; if ( exists $OBJ->{$p} ) { my $q = $OBJ->{$p}; $q = $formatThis->($q); print "$p=$q "; } $formatThis; }

      i'm sorry, but i don't follow much of your comments.

      • variable
        my simple code example with the <package_name> string was to show that the subroutine variable still needs the package namespce. i find this odd; the subroutine was declared "my" all scalar/array/hash variables don't need a package name prepended to them when used in a function. to my simple mind, the code variable shouldn't need one.
      • code snippet
        assuming that the second invocation of formatThis is a typo, i still get both error messages.
      • error messages
        if someone could point me to a URL that defines these two messages; that would be a huge help. as i said earilier; i have both the input parameters and the code variable "provably" defined; so i'm curious what perl thinks is unitialized.

      Formatting fixed by GrandFather

        Back to your original example, with comments:
        # $formatThis is not known here sub A { # begin of subroutine # what is $OBJ ? is it a global variable? or is it passed into thi +s sub? # what is $p ? is it a global variable? or is it passed into thi +s sub? if ( exists $OBJ->{$p} ) { my $q = $OBJ->{$p}; $q = &$<package_name>::formatThis($q); # wrong! <package_name> + means: # read one record from filehandle 'pack +age_name' print FH "$p=$q "; } my $formatThis = sub { # $formatThis declared as my(), lexically +scoped my $x = @_; # wrong! evaluates @_ in scalar context! my $y = sprintf ( "%.2f", $x); return $y; }; } # end of subroutine # from here on $formatThis is not known
        I guess you want to say something like this, but I can't know, because you don't tell us about the context of your code:
        sub A { my ($OBJ,$p) = @_; if ( exists $OBJ->{$p} && exists $OBJ->{'sub'}) { my $q = $OBJ->{$p}; $q = $OBJ->{'sub'}->($q); # execute the sub stored in $OBJ print STDOUT "$p=$q\n"; } my $formatThis = sub { my ($x) = @_; my $y = sprintf ( "%.2f", $x); return $y; }; $OBJ->{'sub'} = $formatThis; } # test it my $ref = {}; $ref->{'float'} = 3.1415926; A($ref); # get a sub A($ref,'float'); # now doit
        --shmem
        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: unitialized value in subroutine entry
by Anonymous Monk on Jun 27, 2006 at 03:06 UTC
    Hi,
    hmm....I removed that one and that issue solved.some other issue i will check that.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://557714]
Approved by davido
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.