Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
XP is just a number
 
PerlMonks  

Re^3: unitialized value in subroutine entry

by shmem (Chancellor)
on Jun 27, 2006 at 04:00 UTC ( [id://557729]=note: 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.


in reply to Re^2: unitialized value in subroutine entry
in thread unitialized value in subroutine entry

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}

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

    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

      for some reason; perl doesn't complain about &$package::variable when this variable has not yet been declared!

      Indeed

      { $var = 1; # ok. Strict off. } { use strict; $var = 1; # Error! } { use strict; $main::var = 1; # ok. Strict allows fully qualified var names. }

      i was getting "faked out" by adding the package name to the code variable to silence the "requires explicit package name" message.

      That message means your variable hasn't been declared. The text assumes most variables are package variables, but that's untrue. Lexical (my) variables should be used whenever possible.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://557729]
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.