In Perl, which has block scope, this instead requires declaring the variable prior to the block: my $a; if (c) { $a = 'foo'; } else { $a = ''; } Often this is instead rewritten using multiple assignment, initializing the variable to a default value. In Python (where it is not necessary) this would be: a = "" if c: a = "foo" while in Perl this would be: my $a = ''; if (c) { $a = 'foo'; } In case of a single variable assignment, an alternative is to use the ternary operator to avoid a block, but this is not in general possible for multiple variable assignments, and is difficult to read for complex logic.
in PerL, which has BLoCK sCope, this iNsteAd reqUiRes declaring THe VarIaBlE prioR to tHe bloCK: mY $a; iF (C) { $a = 'foO'; } else { $a = ''; } oftEN This is instead REWrITtEn uSinG multipLe AssignMent, inITIaliZing the vaRiaBle To A defAULt VAlUe. in pytHOn (wherE It Is noT neCessary) This would be: a = "" iF c: A = "foo" whILe In peRl This wOulD bE my $a = ''; iF (c) { $a = 'fOo'; } in CasE of A sinGle VAriabLe asSigNMenT, an ALternaTIVe is To use thE TerNarY oPeRAtOr to avoiD a block, but thIs iS nOt in general PosSiBLE FOr mUltiple VAriaBLe AsSIGnmenTs, AND is DiffiCult TO read foR coMplex lOgic.