Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

$a and strict

by mce (Curate)
on Jul 18, 2001 at 17:56 UTC ( #97651=perlquestion: print w/replies, xml ) Need Help??
mce has asked for the wisdom of the Perl Monks concerning the following question:

while giving a perl initiation to some coworkers I came up with this strange problem
(it happened on perl 5.005 to 5.6.1 )

use strict ; $a="hallo";

This runs OK??
also when I specify $b.
But from $c on it fails etc...

Why is this?

I searched perldoc , deja, O'Reilly books, but no luck.
Can anybody enlighten me please?
Thanks in advance,

Dr. Mark Ceulemans
Senior Consultant
IT Masters, Belgium

Replies are listed 'Best First'.
Re (tilly) 1: $a and strict
by tilly (Archbishop) on Jul 18, 2001 at 18:02 UTC
    $a and $b are special because they are used by sort, so there is an exception for them.

    You should never use those as variable names. Among other problems, if you declare them with my, you will be unable to write custom sort functions afterwards. Oops.

      Only within the scope of the my, or anywhere after?

      What about having the sort function qualify the names, eg. $main::a, to avoid that issue?

        There is only an issue with sort if you are not using the fully qualified package name and you are within the scope of a my declaration. (Or an our declaration from a different package.)

        Of course your co-workers may still want a clearer variable name...

Re: $a and strict
by Masem (Monsignor) on Jul 18, 2001 at 18:00 UTC
    $a and $b are special in that they are automagically defined in the main:: scope due to their use in the sort function. In other words, with regards to strictness, they're considered in the same class as @_, $_, etc.

    Dr. Michael K. Neylon - || "You've left the lens cap of your mind on again, Pinky" - The Brain
      Not true.

      Unlike *_, *ENV, etc, $a and $b are in the current package. They are just, like @INC, only exempt from strict.

      Beware! Should you wish to write a sort subroutine that will be called from another package, you must go out of your way to use $a and $b from *their* package, and not your own. (Similarly if they try to pass a sort subroutine into your function, it won't work as expected.)

      Thanks tye for spotting my obvious thinko.

Re: $a and strict
by davorg (Chancellor) on Jul 18, 2001 at 18:02 UTC
(kudra: $a used in sorting) Re: $a and strict
by kudra (Vicar) on Jul 18, 2001 at 18:04 UTC
    When you use strict, you need to explicitly declare your variables. That is why $c doesn't work. $a and $b are used for sorting and already belong to a package, so they do not require declaration.
Re: $a and strict
by Hofmator (Curate) on Jul 19, 2001 at 14:40 UTC

    I searched perldoc , deja, O'Reilly books, but no luck.
    You got your answer, but just a small comment - I don't think this information was so well hidden.
    • From perldoc strict
      Because of their special use by sort(), the variables $a and $b are exempted from this check.
    • The camel (3rd edition) has it in the index and this points to the section about the special variables. Furthermore you can find it in 'Pragmatic Modules'->use strict 'vars'

    -- Hofmator

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://97651]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2018-12-16 17:47 GMT
Find Nodes?
    Voting Booth?
    How many stories does it take before you've heard them all?

    Results (71 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!