Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Variable declaration on one line

by Anonymous Monk
on Jan 28, 2004 at 15:15 UTC ( [id://324682]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Sorry but have to ask a beginner question.

These declarations:
my $varOne; my $varTwo; my @varArray; my $varThree;
Want on one line, can it be done??
my $varOne, $varTwo, @varArray, $varThree;
It gives me declaration errors when I try the above one liner.

update (broquaint): title change (was Varirable declaration on one line)

Replies are listed 'Best First'.
Re: Variable declaration on one line
by monktim (Friar) on Jan 28, 2004 at 15:19 UTC
    my ($varOne, $varTwo, @varArray, $varThree);
Re: Variable declaration on one line
by duff (Parson) on Jan 28, 2004 at 15:20 UTC

    Sure it can be done, but you need to enclose them in parentheses. When you don't put the parens, you get the equivalent of

    my ($varOne), $varTwo, @varArray, $varThree;

    Again, the proper way is:

    my ($varOne, $varTwo, @varArray, $varThree);
        my $var = ''; initialises the var with the actual value - empty string.

        my $var; leaves the variable undefined (undef).

        I'm assuming you mean: my $var = ''; and my $var; In the first example you declare the scalar variable and initialize it to the empty string. In the second example you only declare the scalar variable.

        There is not one specific type set to the scalar (i.e. integer, character, etc.). Perl figures out what you want based on the context of use.

        use strict; use warnings; my ($i, $j) = (1, 2); my $k = ''; print $i + $j ."\n"; print $i + $k ."\n";
        The first print statement adds the two values and it is easy to see this because they are both integers at that point. The second print statement will convert $k to a number and then add $i. Since you are using $k in an arithmetic operation, Perl knows that $k should be a number. Perl is very context sensitive. It is very important to learn how things behave in different contexts.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2024-04-18 10:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found