user1289
user1289

Reputation: 1321

Can't use tzset with "use strict"

I need to get current time with my specified timezone, so I'm using tzset. But when I add use strict, I get the following error

use strict;

use POSIX qw(tzset);

......................

sub is_active
{
    tzset;
    $ENV{TZ} = 'America/New_York';

    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
    if (($hour > 9 && $min > 30) || ($hour < 14))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

Bareword "tzset" not allowed while "strict subs" in use at .......

Is this known issue? Is there any alternative I can use?

Upvotes: 1

Views: 223

Answers (1)

amon
amon

Reputation: 57640

When you import subroutines from a module like use POSIX qw(tzset), the imported subroutines are imported into the current package. If you do not declare a package, that is the main package.

To fix your problem, first declare a package, then use any modules you need:

use strict;

package MyModule;
use POSIX qw(tzset);  # now tzset is available within MyModule

...

For pragmas like use strict and use warnings that change how your code behaves but don't import any subroutines, it is not important whether they come before or after a package declaration. Their effect is not limited to a package but to a lexical scope (delimited by curly braces).

Upvotes: 3

Related Questions