perl coding style question

2011-08-16 Thread Alex Brelsfoard
Hi All,

I just came across some code that used some old-style coding practice, and I
wanted to know if there was still any GOOD reason for using it.

The code I saw was referencing variables using the $:: pragma:

if ($::var) {
   ...
}

To my understanding this is a bit messy, and not to mention potentially
dangerous as it kinda goes against the use strict; methodology, does it
not?

I haven't seen this style code in a long time so I wanted to get other
feedback on its potential benefits before I suggest updating it.
Thanks in advance to all that can help.

--Alex


Re: perl coding style question

2011-08-16 Thread marcos rebelo
the is the case of the given/when, the code blocks and $_, for example:

use strict;
use warnings;
use v5.10;

use List::Util;

given (0) {
when (0) {
say List::Util::first { $::_  3 } (1..10)
}
}


prints 4





use strict;
use warnings;
use v5.10;

use List::Util;

given (0) {
when (0) {
say List::Util::first { $_  3 } (1..10)
}
}

first return undef




Crazy result ;)

Best Regards
Marcos

On Tue, Aug 16, 2011 at 12:47, Alex Brelsfoard
alex.brelsfo...@gmail.com wrote:
 Hi All,

 I just came across some code that used some old-style coding practice, and I
 wanted to know if there was still any GOOD reason for using it.

 The code I saw was referencing variables using the $:: pragma:

 if ($::var) {
   ...
 }

 To my understanding this is a bit messy, and not to mention potentially
 dangerous as it kinda goes against the use strict; methodology, does it
 not?

 I haven't seen this style code in a long time so I wanted to get other
 feedback on its potential benefits before I suggest updating it.
 Thanks in advance to all that can help.

 --Alex




-- 
Marcos Rebelo
http://www.oleber.com/
Webmaster of http://perl5notebook.oleber.com



Re: perl coding style question

2011-08-16 Thread Randy J. Ray

Alex Brelsfoard wrote:

Hi All,

I just came across some code that used some old-style coding practice, and I
wanted to know if there was still any GOOD reason for using it.

The code I saw was referencing variables using the $:: pragma:

if ($::var) {
   ...
}

To my understanding this is a bit messy, and not to mention potentially
dangerous as it kinda goes against the use strict; methodology, does it
not?


This isn't a pragma, this is a short-hand way of referring to the main 
namespace. $::var is the same as $main::var, and is sometimes necessary 
when you need to refer to a value in main from within a module. Generally, 
having to refer to a different namespace at all breaks encapsulation, so it 
should only be done if absolutely necessary.


Randy
--

Randy J. Ray  Sunnyvale, CA  http://www.rjray.org   rj...@blackperl.com

Silicon Valley Scale Modelers: http://www.svsm.org


Re: perl coding style question

2011-08-16 Thread Mark Fowler
On Tuesday, August 16, 2011, Alex Brelsfoard alex.brelsfo...@gmail.com
wrote:

 The code I saw was referencing variables using the $:: pragma:

I sometimes use this in combination with the -s switch.

#!/opt/perl/bin/perl -s

use 5.14;
use warnings;

say STDERR 'reticulating splines'
 unless $::q;

In other words it's a way of me saying, yes, I really want the global var $q
in the main namespace.

(some people might argue I should use 'our' here. They might be right -
especially in the case where I use a command lone switch only once, in which
case warnings will ommit a warning)

Mark

Typed on mobile Internet thingy, expect typos, autocorrect, etc.