Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: To initialise or not to initialise?

by Fletch (Chancellor)
on Jun 24, 2004 at 11:25 UTC ( #369287=note: print w/replies, xml ) Need Help??


in reply to Re: To initialise or not to initialise?
in thread To initialise or not to initialise?

my ($title, $page, $chapter) = "somethingglobal";

You've only initialized $title. You probably mean ... = ("somethingglobal") x 3;</pedant>

Replies are listed 'Best First'.
Re^3: To initialise or not to initialise?
by kiat (Vicar) on Jun 24, 2004 at 13:15 UTC
    I learn something new today, thanks Fletch :)

    Sometimes I wanted to do that but I didn't know what the syntax is.

      There are several ways to do that. Here are two more.
      my ($foo, $bar, $baz) = map "default", 1..3; $_="default" for my ($foo, $bar, $baz);
      As obscure as the last one is, I'd prefer it over the others if you think that the variable list will expand since you don't have to synchronize the number of variables with a hard-coded constant.

      That is, if I wanted to achieve this effect. Which I never have needed to.

        $_="default" for my ($foo, $bar, $baz);
        That one surprises me that it works. I thought that the "for" statement modifier was mere syntactic sugar, since that deparses as:
        u2-4.14 $ perl -MO=Deparse ./tst foreach $_ (my($foo, $bar, $baz)) { $_ = 'default'; } ./tst syntax OK
        But if written as a straight for loop (as it deparses to), then the variables are local to the loop. And I could've sworn I tried that once upon a time, and it didn't work...probably won't be the last time I make that mistake :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2021-10-27 23:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (95 votes). Check out past polls.

    Notices?