or what is the best way to declare a variable?

I'll respond to that question first. The best way depends on what you're trying to accomplish, and the 'scope' you're trying to achieve.

my declares variables to exist within the innermost block. In this sense, I mean block as a literal { } pair, or as a subroutine, or as a file, or even as an eval. What this means is that when you declare a variable with my, it lives within the block in which it was declared, and falls out of scope when the block itself passes from scope. It probably gets a little more complicated than that sometimes (if you make it), but generally just count on variables declared with 'my' as existing within the block in which they were declared. But that probably wasn't really your question.

You were asking what is the difference between some of the commonly seen usages of 'my'. I'll try to respond accurately. I'm sure if I miss something we'll hear about it. ;)

my $var; # Declares a single variable.

The following example assigns the variable a value:

my $var = "Hello world!\n"; my $var = 10; my $var = 0; my $var = ""; my $var = \@some_array; my $var = \%some_hash; my $var = \$some_scalar; my $var = \&some_function;

In the preceeding examples, you're assigning $var some value. It is not always necessary to assign $var the value of "" (the null string). Unlike C, variables in Perl are already empty when you create them. But empty in this case means undefined. In the context of "What is false" and "What is true", undef, "" (null string), and 0, are all "false". But if you are counting on a null string, assign it as in your examples. Redundancy isn't always bad either; in some cases you'll want to assign something, even if it's nothing, to add clarity.

What if you want to declare multiple variables? my binds more tightly than the comma, so you cannot just say, my $this, $that;. You must use paranthesis:

my ( $this, $that );

The parenthesis are also serving the dual function of (1) Specifying order of precedence so that the comma binds more closely than the my. And (2) the parenthesis have also created a list. That means you can legitimately do this:

my ( $this, $that ) = ( "the", "other" );

It is not wrong to say,

my ($var);

or

my ($var) = "This";

It's just not *necessary* in this situation to use the parens. ...but it doesn't hurt anything.

Some additional things you can do:

my ( $this, $that ) = "The other";

Now $this has been assigned a value, and $that has not. ...and you can do this:

my @array = ("This", "That", "and", "The", "Other"); my %hash = ( "This" => "That", "The" => "Other" ); my $var = ("This", "That"); # $var now contains "That". my ( $this, $that ) = \( $the, $other ); # The reference # constructer is distributive and # applies to both items inside the rvalue # list.

And there are plenty of other things you can do. Just remember, if there is more than one variable on the left, use parenthesis so that 'my' binds to both. The rest you'll find behaves pretty much how you expect it to when you need it.

Dave

"If I had my life to do over again, I'd be a plumber." -- Albert Einstein


In reply to Re: declaration of variables by davido
in thread declaration of variables by Murcia

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.