Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Mistaken notions of $a/$b strict

by clintp (Curate)
on Feb 28, 2002 at 15:43 UTC ( [id://148248]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Re: (re:x5 use strict....)$a and $b should be in perlvar
in thread use strict won't require explicit name for all variables?

My answer was tongue firmly in cheek. Note the tone and the :)'s scattered about. But please, submit a patch.

To clear up a misconception though, your "action at a distance" horror scenario there won't happen. No sir. $a and $b are (apparently) local'd to the block:

@list=qw( one two three four five ); $a="Hello"; $b="World"; sort { $a <=> $b } @list; print "$a $b"; # Hello World
I knew this. Several others in channel this morning knew this. But in an ironic twist of events, I can't find where that localization is documented. I think I see it in the Perl source...but not in the docs.

Replies are listed 'Best First'.
Re: Mistaken notions of $a/$b strict
by dragonchild (Archbishop) on Feb 28, 2002 at 16:02 UTC
    Ah, but the "action at a distance" most certainly does occur. If you create a sorting sub in one module and use it in another ... Look at the following code:
    my $a = 5; my @b = reverse (0 .. 9); print "@b\n"; sub boo { $a <=> $b } @b = sort boo @b; print "@b\n"; print $a,$/; ---- 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 9 8 5 6 7 5
    Note the lack of sorting after 4. :-)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      While I don't have time to fully explain it, you can actually use prototypes(bleck) to avoid the weirdness in $a and $b (ughhh) for 5.6 and above....
      sub boo ($$) { my ($a,$b) = @_; $a <=> $b }

      -Blake

        Actually, just scoping $a and $b to boo() is enough. I didn't realize they were passed in through @_. :-)

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://148248]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-20 01:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found