Re: strict or not strict?
by Tanalis (Curate) on Aug 19, 2005 at 14:01 UTC
|
no strict 'vars';
.. but I'm not 100% sure what you're trying to do. Typically, if you need to turn off strictness, something needs rethinking, in my experience.
| [reply] [Watch: Dir/Any] [d/l] |
|
Typically, if you need to turn off strictness, something needs rethinking, in my experience.
That, I disagree with. Whenever I turn off strictness, I do so *after* thinking. Your statement suggests that turning off strictness is something bad, and ought to be avoided at all costs. Sometimes there are alternatives for turning off strictness, but they are not always better.
I think it's a sign of poor programming if your code bends backwards to satisfy strict, when the code could have been shorter, easier to understand and maintain and less error prone with a strategic 'no strict'.
| [reply] [Watch: Dir/Any] |
|
I think it's a sign of poor programming if your code bends backwards to satisfy strict, when the code could have been shorter, easier to understand and maintain and less error prone with a strategic 'no strict'.
I buy that, when applied to short scripts, one-liners and throw-away code. For anything longer, or anything where a large number of people are going to be working on the code, it's nearly always easier to adhere to strict than to take the shortcuts and risk having to explain to colleagues and maintainers why you've made the decision you made.
In the context of the OP (which is the context in which my comment should be read), where the snippet seems to be a cut-down excerpt from something bigger, I think that turning off strictness to kludge something in is a bad idea. In this context, I stand by what I said: something needs rethinking.
Update: What ++Transient said :)
| [reply] [Watch: Dir/Any] |
|
'Typically' ne 'Always'
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
Re: strict or not strict?
by Fletch (Bishop) on Aug 19, 2005 at 14:03 UTC
|
To start with my $$_ = $_; isn't even valid syntax. You're trying to declare a lexical with a dynamically generated name. Aside from the fact that the lexical would go out of scope when the block was exited (i.e. immediately in this case), it's a syntax error. A more recent perl (5.8.6) would tell you:
Can't use global $_ in "my" at spoo line 11, near "$$_ "
The real question is what do you think you're trying to accomplish with this. You have $data{blah} already, just use the hash you've got.
--
We're looking for people in ATL
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
*grin*
Interestingly, in the two versions of Perl that I have access to here, 5.004_04 and 5.6.1, this snippet
$_ = "aa";
$$_ = "bb";
print $aa;
outputs bb.
Notice the lack of my, and the lack of use strict; - it only works without strictness, which I guess was the point of the OP's question about turning off strict vars.
It's not a good thing to do, admittedly - though it would seem to be valid syntax, at least historically.
Updates to add a few words so that the post makes a little more sense.
Update 2: Just noticed that my $$_ appears in both the OP and in Fletch's reply. Doh. Fletch is right, of course, my $$_ = $_ isn't valid syntax. | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: strict or not strict?
by ikegami (Patriarch) on Aug 19, 2005 at 14:08 UTC
|
There's a way of doing it that doesn't cause use strict 'refs' to complain, but that doesn't change the fact that what you are doing is exactly what use strict 'refs' is meant to stop you from doing. Why are you creating variable names? Why isn't a hash good enough?
Either use no strict 'refs' to get rid of the error or don't use symbolic refs. Using the workaround (symbol table manipulation) just hides the problem, while no strict 'refs' documents that you are using this dangerous feature.
Oh and get rid of the my. You can't my a dereferencing.
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: strict or not strict?
by xdg (Monsignor) on Aug 19, 2005 at 14:24 UTC
|
Here's a way of doing it -- but it uses package variable (i.e. globals) and, of course, you have to fully qualify them. (Or else declare them with "our").
#!/usr/bin/perl
use strict;
use warnings;
my %data = ( "blah"=>1, "boo"=>2, "hoo"=>3 );
# assign each key to a package variable of the same name
foreach (keys %data) {
no strict 'refs';
${"main::$_"} = $data{$_};
} # end-foreach
# note, this causes a "used only once" warning as the symbolic referen
+ce
# above doesn't count. Irrelevant for this example.
print $main::blah;
exit;
It's still unclear what this gains you. Anytime you're resorting to symbolic references or package variables, it should be a warning sign that something isn't well thought out. There are relatively few areas where it makes sense (e.g. writing modules which manipulate the symbol table of the package that uses them.)
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
| [reply] [Watch: Dir/Any] [d/l] |
Re: strict or not strict?
by Roger (Parson) on Aug 19, 2005 at 14:20 UTC
|
#!/usr/bin/perl
use strict;
use warnings;
use base 'Class::Data::Inheritable';
my %data = ( "blah"=>1, "boo"=>2, "hoo"=>3 );
# assign each key to a variable of the same name
foreach (keys %data) {
# no strict;
__PACKAGE__->mk_classdata($_, $data{$_});
} # end-foreach
print blah();
exit;
| [reply] [Watch: Dir/Any] [d/l] |
Re: strict or not strict?
by Tanalis (Curate) on Aug 19, 2005 at 14:15 UTC
|
I'm trying to plug a hash into an old piece of code (for a once off) and I was hoping not to have to change every instance of i.e $blah to $hash{'blah'}. Just being lazy...
Replace All is your friend, surely? That or write a one-liner to replace it for you ...
| [reply] [Watch: Dir/Any] |
|
Of course 'replace all' was my first thought, but then I did think of the above method and just wanted to investigate further down this road.
Hence my question...
Thanks for your help.
| [reply] [Watch: Dir/Any] |
|
#!/usr/bin/perl
use strict;
use warnings;
my ($blah, $boo, $hoo); # DECLARED HERE
my %data = ( "blah"=>1, "boo"=>2, "hoo"=>3 );
# assign each key to a variable of the same name
foreach (keys %data) {
eval " \$$_ = \$data{$_} ";
} # end-foreach
print $blah;
exit;
Replace is still probably a better long run strategy.
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: strict or not strict?
by chromatic (Archbishop) on Aug 19, 2005 at 17:21 UTC
|
You can make a hash of references:
my %hash_refs = ( foo => \$foo, bar => \$bar );
Then look up the reference by name use it where you'd use the hash.
| [reply] [Watch: Dir/Any] [d/l] |