in reply to Re^2: Creating a Moose object and serializing it
in thread Creating a Moose object and serializing it

Near the beginning of the script I have all warnings switched on:

use warnings;

The warnings pragma is lexically scoped and so affects the entire script.

When I first started to test Storable with coderefs, my code looked like this:

use Storable qw{nstore retrieve}; $Storable::Deparse = 1; $Storable::Eval = 1;

Perl emitted a warning that each of those variables had only been used once and this possibly indicated an error in my code. I knew there wasn't an error and I didn't want this warning to be constantly displayed. I could have switched the warning off like this

use Storable qw{nstore retrieve}; no warnings q{once}; $Storable::Deparse = 1; $Storable::Eval = 1;

but that would have turned off the warning for the entire script: maybe later in the script there was a real error which now wouldn't be reported.

By using a block I constrain the scope of the warning suppression to just that block. Furthermore, it also highlights what has been done. The following is equivalent but uses more code and is less obvious:

use Storable qw{nstore retrieve}; no warnings q{once}; $Storable::Deparse = 1; $Storable::Eval = 1; use warnings q{once};

Compare that with the block form:

use Storable qw{nstore retrieve}; { no warnings qw{once}; $Storable::Deparse = 1; $Storable::Eval = 1; }

-- Ken

Replies are listed 'Best First'.
Re^4: Creating a Moose object and serializing it
by daverave (Scribe) on Oct 23, 2010 at 18:07 UTC
    Thank you!