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

Thanks Ken, that was helpful!

Could you please explain the purpose of blocking the statements following `use`?

  • Comment on Re^2: Creating a Moose object and serializing it

Replies are listed 'Best First'.
Re^3: Creating a Moose object and serializing it
by kcott (Archbishop) on Oct 23, 2010 at 17:55 UTC

    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

      Thank you!