Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Global Variables

by Cubes (Pilgrim)
on Nov 27, 2007 at 22:13 UTC ( [id://653393]=note: print w/replies, xml ) Need Help??


in reply to Global Variables

Put a my $shipment_request_id before the if block, in the same block where you later use the variable (and remove the my inside the if).

Replies are listed 'Best First'.
Re^2: Global Variables
by kmullin (Acolyte) on Nov 27, 2007 at 22:22 UTC
    Wonderful, that did it. So, you need to define variables before getting into a block, because if you declare them in a block, they are local to the block?
      Yes. Each block is a scope. Even bare curlies (without any flow control statements) make a block.
      use strict; { my $foo = 2; { my $foo = 1; # masks outer $foo my $bar = $foo; print "foo is '$foo'\n"; # foo is '1' } print "foo is '$foo'\n"; # foo is '2' print $bar; # # error, $bar not visible here my $foo; # warning: $foo already declared # as lexical in this scope. } print "foo = $foo\n"; # error, $foo not visible here

      --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}
      Exactly.
      Well...close, but not exactly. If you are using "use strict", which you evidently are, this is 100% correct. But you should know that without the strict option, a variable can be declared anywhere simply by using it. When not using the strict constraints, the best way I know to describe scoping is "global once declared".
        But you should know that without the strict option, a variable can be declared anywhere simply by using it.

        IMHO, since kmullin is a self-confessed mewbie, you should add a strong health warning to that statement, perhaps something along these lines. Don't be tempted to turn off strictures to silence intractable compilation errors; always use strict;.

        Cheers,

        JohnGG

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-09-15 14:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (21 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.