Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Here is a code snippet from a tutorial i was looking at on the web

package TestSite::Login; $VERSION = v0.0.1; use v5.10.0; use warnings; use strict; use Carp; use Digest::MD5 'md5_hex'; open(my $fh, '<', 'passwords') or die "cannot open passwords file $! +"; my %passwords = map({chomp; split(/:/, $_, 2)} <$fh>); sub check_password { my ($user, $pass) = @_; return( $passwords{$user} and md5_hex($pass) eq $passwords{$user} ); } 1;
This code has a use strict directive in it so I don't understand how it can call the md5_hex function without qualifying it with the full package name. I thought 'use strict' forces you declare lexical variables using 'my' and to reference to package variables and functions with their full names.

many thanks

Replies are listed 'Best First'.
Re: beginner - understanding use strict
by kennethk (Abbot) on Oct 16, 2010 at 16:23 UTC
    Note the line use Digest::MD5 'md5_hex';. The use directive is a mechanism for importing functions into your local package. Specifically, it functions as a BEGIN block (see BEGIN, UNITCHECK, CHECK, INIT and END in perlmod) wrapping a require (checking that a module exists and compiles) and an import. If the use statement is unqualified, as with use Carp; in your example, the default import list will be brought into your local namespace. This means carp will provide you with carp, croak and confess with no extra effort on your part. In the case of Digest::MD5, the string 'md5_hex' overrides the default import list, specifying you want the function in question and only that function imported.
Re: beginner - understanding use strict
by Anonymous Monk on Oct 16, 2010 at 16:27 UTC