Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: unitialized value in subroutine entry

by reneeb (Chaplain)
on Jun 27, 2006 at 07:06 UTC ( #557719=note: print w/replies, xml ) Need Help??

in reply to unitialized value in subroutine entry

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; }

Replies are listed 'Best First'.
Re^2: unitialized value in subroutine entry
by Anonymous Monk on Jun 27, 2006 at 07:29 UTC

    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
      _($_=" "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}

        thanks for taking the time to answer this.

        my basic problem is that i was NOT declaring the code variable BEFORE invoking it.

        i was getting "faked out" by adding the package name to the code variable to silence the "requires explicit package name" message. for some reason; perl doesn't complain about &$<package>::variable when this variable has not yet been declared!

        the lesson i learned is that private functions need to be declared identically to lexically scoped variables; BEFORE usage.

        here's the final code snippet :

        my problem was using the anonymous routine "formatThis1" which was declared at the end of the routine. my solution was to move the anonymous routine to the front of the routine ("formatThis0")

        note that the variable $somepackage::formatThis1 is supposed to be the code at the end of someroutine. however; it is not and that's what causes the unitialized variable at subroutine entry message (i think!)

        package somepackage; sub someroutine { my ($FH,$OBJ,$OPT) = @_; my $formatThis0 = sub { my ($x) = @_; my $y = sprintf ( "%.2f", $x); return $y; }; print FH "M", $OBJ->{"someKey1"}, " "; if ( exists $OPT->{"someKey0"} ) { foreach my $p ( @{ &someotherPackage::var0() } ) { if ( $OPT->{"someKey0"} eq "all" ) { if ( exists $OBJ->{$p} ) { my $q = $OBJ->{$p}; $q = &$formatThis0($q); ## $q = &$somepackage::formatThis1($q); print FH "$p=$q "; } } } } print FH "\n"; my $formatThis1 = sub { my ($x) = @_; my $y = sprintf ( "%.2f", $x); return $y; }; }

        Formatting fixed by GrandFather

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://557719]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2018-05-25 09:50 GMT
Find Nodes?
    Voting Booth?