Naked words to learn in Perl

Source: Internet
Author: User

The use strict consists of 3 parts. One of them (use strict "subs") is responsible for prohibiting the indiscriminate of the naked word .

What does that mean?

Without this restriction, the following code can also print "Hello".

My $x = hello;print "$x \ n";    # Hello

This use does not conform to the usual habit of putting strings in quotation marks, but Perl defaults to allowing (using) the bare word --the word without quotation marks--as a string.

The above code outputs "Hello".

Of course, at least before the "Hello" function is added by default at the top of the script (this is the case):

Sub Hello {  return "zzz";} My $x = hello;print "$x \ n";    # ZZZ

In the new version, Perl sees the Hello () function, calls it (function), and assigns the result to $x.

Then, if someone puts this function at the end of the file (after assignment), Perl does not see the function at the time of assignment, and it returns to the old: "Hello" is assigned to $x.

Yes, you certainly don't want to get yourself into trouble. So avoid confusion by using use strict in your code to prevent the naked word hello from appearing in your code.

Use strict;my $x = hello;print "$x \ n";

The following error is given:

Bareword "Hello" not allowed while "strict subs" in use with script.pl line 3.Execution of script.pl aborted due to Compilat Ion errors.
Correct use of the bare word

Even if the use strict "subs" is turned on, there are some places where you can have a bare word.

First, the user-defined function name is the bare word.

Similarly, in the Extract hash Table element curly braces also use the naked word, as well as the fat arrow = left can also be without quotation marks.

Use strict;use warnings;my%h = (name = ' Foo ');p rint $h {name}, "\ n";

The "name" in the code above is a bare word, which is also valid when use strict.

Naked words to learn in Perl

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.