Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: perl doesn't like variable

by jbware (Chaplain)
on Oct 03, 2004 at 13:47 UTC ( [id://396004]=note: print w/replies, xml ) Need Help??


in reply to perl doesn't like variable

You need to declare the variable before usage. Try:
my $counter = 0; # (comment-out added): use vars qw($counter);
or (From davido)
use vars qw($counter); $counter = 0;
-jbWare

Update: Per davido's comment, I've modified my answer.

Replies are listed 'Best First'.
Re^2: perl doesn't like variable
by davido (Cardinal) on Oct 03, 2004 at 15:05 UTC

    You need to declare the variable. Try:

    my $counter = 0;

    Why? He is properly declaring his variable to be a package global, and that is perfectly legit under strictures. It would be a mistake to try to also declare it to be a lexical variable, as that would create two separate variables.

    His problem is that he initialized $counter before declaring it. Here is his code:

    $counter = 0; use vars qw($counter);

    The proper sequence of events is to declare the variable, and then start using it (or initialize it), like this:

    use vars qw($counter); $counter = 0;

    HTH.


    Dave

      Good catch. Chalk that up to tunnel vision on my part. I saw the usage w/o declaration and didn't even look any further.

      -jbWare
Re^2: perl doesn't like variable
by jamaas (Novice) on Oct 03, 2004 at 14:03 UTC
    Thanks!


    I did that and now it does not increment within the loop.
    Do I need to declare it somewhere within the block?
    Thanks
    Jim

    jamaas btinternet com

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-18 09:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found