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

Re^4: unitialized value in subroutine entry

by Anonymous Monk
on Jun 27, 2006 at 05:08 UTC ( [id://557744]=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^3: unitialized value in subroutine entry
in thread unitialized value in subroutine entry

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

Replies are listed 'Best First'.
Re^5: unitialized value in subroutine entry
by ikegami (Patriarch) on Jun 27, 2006 at 11:52 UTC

    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://557744]
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.