Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

My command in perl?

tammo21tammo21 Member Posts: 1
I can't for the life of me figure out what this command means:

my $variable

What does the "my" thing do? I'm new at this shiz, and I can't find out why or what the "my" thingy is for. Can anyone tell me?

Comments

  • JonathanJonathan Member Posts: 2,914
    Hi,

    my is kinda like a two-in-one tool, allowing you to create and scope a variable at the same time. Imagine I did this:-

    [code]

    for (1..5) {
    my $variable = "hello
    "
    print $variable;
    }

    print $variable;

    [/code]

    Now, that would print "hello" 5 times (e.g. because the loop goes from 1 to 5). However, the print statement outside the loop would not print anything as the variable only exists within the loop - it's scope.

    That's my understanding of it anyway! It's kinda like a local variable in a procedure, but it seems that you can make it local to a loop or if structure (or others I guess) too.

    I'll stand corrected if anyone knows anything that I don't about "my" though. :)

    Jonathan




    ------------------------------------------
    Count downloads from your site for free!
    http://www.downloadcounter.com/

  • heeroyuyheeroyuy Member Posts: 125
    : Hi,
    :
    : my is kinda like a two-in-one tool, allowing you to create and scope a variable at the same time. Imagine I did this:-
    :
    : [code]
    :
    : for (1..5) {
    : my $variable = "hello
    "
    : print $variable;
    : }
    :
    : print $variable;
    :
    : [/code]
    :
    : Now, that would print "hello" 5 times (e.g. because the loop goes from 1 to 5). However, the print statement outside the loop would not print anything as the variable only exists within the loop - it's scope.
    :
    : That's my understanding of it anyway! It's kinda like a local variable in a procedure, but it seems that you can make it local to a loop or if structure (or others I guess) too.
    :
    : I'll stand corrected if anyone knows anything that I don't about "my" though. :)
    :
    : Jonathan
    :
    :
    :
    :
    : ------------------------------------------
    : Count downloads from your site for free!
    : http://www.downloadcounter.com/
    :
    :

    Thats pretty much correct :) there is another command in perl, i don't use it often so I don't remember it, but it allows for basicly the same type of thing as "my" variables.

    -----------------------
    [RED]Heero Yuy
    Perl Board Moderator
    Perl Semi-Expert[/RED]

  • JonathanJonathan Member Posts: 2,914
    :
    : Thats pretty much correct :) there is another command in perl, i don't use it often so I don't remember it, but it allows for basicly the same type of thing as "my" variables.
    :
    : -----------------------
    : [RED]Heero Yuy
    : Perl Board Moderator
    : Perl Semi-Expert[/RED]
    :
    :

    Knowing Perl there'll be about 5 ways of doing it... ;) There always seems to be for everything else!

    Jonathan


    -------------------------------------------
    Count downloads from your site for free!
    http://www.downloadcounter.com/
    Oh yeah, I run a web hosting thing now too.
    http://www.incrahost.com/

  • bgibbybgibby Member Posts: 251
    : I can't for the life of me figure out what this command means:
    :
    : my $variable
    :
    : What does the "my" thing do? I'm new at this shiz, and I can't find out why or what the "my" thingy is for. Can anyone tell me?
    :

    Hi there,

    Exact quote from http://www.perldoc.com...

    [code]
    my EXPR

    A my declares the listed variables to be local (lexically) to the enclosing block, file, or eval. If more than one value is listed, the list must be placed in parentheses.
    [/code]

    If you don't understand SCOPE then this will mean nothing to you.

    MY allows you to make a variable that can only be seen by the current scope and it's child scopes.

    Cya
    Bradley q:)
  • Justin BibJustin Bib USAMember Posts: 0

    < http://forcoder.org /> free video tutorials and ebooks about || JavaScript, Delphi, Perl, Scratch, C++, Objective-C, Visual Basic .NET, Ruby, C#, Java, MATLAB, PL/SQL, Go, Swift, Visual Basic, Assembly, Python, R, C, PHP Dart, D, Awk, Alice, F#, LabVIEW, Kotlin, Logo, Transact-SQL, SAS, ML, Lisp, Apex, Hack, FoxPro, Clojure, ABAP, Scala, Lua, Ada, Rust, Julia, VBScript, Bash, Fortran, Crystal, COBOL, Scheme, Erlang, Prolog || _________

Sign In or Register to comment.