in reply to my $x = <expr>; vs my $x; $x = <expr>;

No, they are equivalent as you have written them. I prefer to initialize at the same time I declare lexical, as in your first example. That helps enforce locality of the variable. I generally split them only when $x is set within a smaller scope, but $x is needed in a wider one. An example,

my $x; { open my $fh, '<', '/path/to/foo.file' or die $! $x = <$fh>; close $fh; }
$x is available outside the braces, but the apparatus needed to set it is hidden from the rest of the program.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: my $x = <expr>; vs my $x; $x = <expr>;
by Aristotle (Chancellor) on Jun 05, 2004 at 21:32 UTC

    I'd write that as

    my $x = do { open my $fh, '<', '/path/to/foo.file' or die $! <$fh>; };

    Note that $fh is auto-closed due to falling out of scope.

    Update: the last line was not meant to be $x = <$fh> of course.

    Makeshifts last the longest.

      You can't use $x in the same statement as you declare it. Your second use of $x is global. The last line in the block should be just scalar <$fh>

      The PerlMonk tr/// Advocate
        That was just a mistake. Thanks for catching it. scalar is not necessary here, btw, because my $x = already provides scalar context.

        Makeshifts last the longest.