Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Use variable outside if statement

by bl0rf (Pilgrim)
on Dec 17, 2003 at 23:45 UTC ( [id://315418]=note: print w/replies, xml ) Need Help??


in reply to Use variable outside if statement

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: (2) Use variable outside if statement
by diotalevi (Canon) on Dec 18, 2003 at 00:00 UTC
    That's a poor idea. The right way to solve it is to expand the scope of the variable a notch to be just outside the if-block. You solved it by ratchetting the scope up to global which is far higher than the problem required.
Re: Re: Use variable outside if statement
by SavannahLion (Pilgrim) on Dec 18, 2003 at 06:56 UTC

    Make it a global variable, don't use "my"

    Ummm... Just a thought, aside from what diotalevi said, I don't think Perl would be happy with that solution since the original poster has Use Strict; in his code.
    It's poor coding practice to unnecessarily declare such broad scopes when smaller scopes would be just as effective. I can't tell you how often I reuse variable names, making it even more important to limit their scope. Can you imagine the kind of bugs that would crop up if you use a global variable called $count in all your While loops?

    ----
    Is it fair to stick a link to my site here?

    Thanks for you patience.

      Ummm... Just a thought, aside from what diotalevi said, I don't think Perl would be happy with that solution since the original poster has Use Strict; in his code.

      What's wrong with our?

Re: Re: Use variable outside if statement
by podian (Scribe) on Dec 18, 2003 at 20:28 UTC
    use strict; if ($line =~ /TC/g) { my $lookfor = "TC"; } else { my $lookfor = "26 "; } print $lookfor;

    I have not seen a simple answer to this question. A simple answer could be,

    declare the variable outside the IF statement like this:


    use strict; my $lookfor; if ($line =~ /TC/g) { $lookfor = "TC"; } else { $lookfor = "26 "; } print $lookfor;

    Because he is not seeing the result when he prints the variable because it's scope is inside the IF

    In fact, shouldn't he get a compile error when he tries to run this? (unless he already has the same variable somewhere else)

Re: Re: Use variable outside if statement
by Juerd (Abbot) on Dec 23, 2003 at 23:01 UTC

    Make it a global variable, don't use "my"

    Heh, I had a good laugh. That single ++ was mine :)

    rotflol.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-04-17 15:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found