in reply to declaration of variables

I think davido said it all in Re: declaration of variables, but he failed to comment on the last possibility that you mentioned.
m ($var) = "";
I have no idea where you got that from. The simplest explanation would be that it is a typo and that you in fact meant:
my ($var) = "";
If that is what you meant, read no further.















You're reading further. Ok, what you wrote almost has meaning. But should probably only be used in obfuscations, never in real code.
m ($var) = "";
is basically equivalent with:
$_ =~ m/$var/ = "";
which of course is an error, namely:
Can't modify pattern match (m//) in scalar assignment
But had your example shown just:
m ($var);
that code would have compiled to:
$_ =~ m/$var/;
if you don't use use strict; (which you don't in your example). A scary thought.

Therefore, once again, always use strict; (and use warnings; if you're using Perl 5.6.0 or later).

Liz

Update:
Made a little clearer with what I meant with "It".

Replies are listed 'Best First'.
Re: Re: declaration of variables
by tsee (Curate) on Sep 02, 2003 at 09:23 UTC
    (Update: Sorry for the confusion. Until Liz' update about the fact that she was talking about "m ($v) =..." in the second half of her reply, I mistook her reply to be talking about the ordinary "my ($v) = 'a';".)

    Hi Liz,
    I don't think "my ($v) = '';" is equivalent to "$_ =~ m/$var/ = '';" and throwing a warning.
    (cmd.exe has different shell quoting!) %perl -w -Mstrict -e "my ($v) = '';" (no warnings)
    And thus:
    %perl -w -Mstrict -e "my ($v) = 'a'; print $v;" a
    which parses in my head as 'string is assigned to first element of the left hand side list'. I can't argue with the exact concepts applied by the interpreter because I forgot most about the weirder rules of context including lvalue stuff. But this seems to support the above:
    %perl -w -Mstrict -e "my ($v,$s) = 'a'; print $v, $s;" aUse of uninitialized value in print at -e line 1.
    Still, string being assigned to first element of the left-hand side list. Same for lists on the right-hand side. (Which would be common usage in contrast to the above weirdness.)
    %perl -w -Mstrict -e "my ($v,$s) = ('a'); print $v, $s;" aUse of uninitialized value in print at -e line 1.
    Good. As expected. Same goes for two-element lists on the rhs.
    %perl -w -Mstrict -e "my ($v,$s) = ('a', 'b'); print $v, $s;" ab
    And the ',' operator has lower precedence than '=', so we get a 'useless use of a constant' error.
    %perl -w -Mstrict -e "my ($v,$s) = 'a', 'b'; print $v, $s;" Useless use of a constant in void context at -e line 1. aUse of uninitialized value in print at -e line 1.
    Hope this clarified things a little.