Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: using a string as a SCALAR ref while "strict refs" in use

by Anonymous Monk
on Jun 19, 2012 at 11:27 UTC ( [id://977032]=note: print w/replies, xml ) Need Help??


in reply to using a string as a SCALAR ref while "strict refs" in use

If strict is bothering you :) turn it off :)  no strict 'refs';   ## no critic; because  strict itself confers no benefits; The benefits come from avoidance of the bad practices forbidden by  strict :)

Its time to read a classic :)

Here is explanation of how to do this (  no strict 'refs';   ## no critic;), why not to do this, and the better approach -- it is FAQ :)

  1. Why it's stupid to `use a variable as a variable name'
  2. A More Direct Explanation of the Problem
  3. What if I'm Really Careful?

Replies are listed 'Best First'.
Re^2: using a string as a SCALAR ref while "strict refs" in use
by xorl (Deacon) on Jun 19, 2012 at 11:42 UTC
    Well because I really don't want to turn strict off since as you point out using a variable as a variable name isn't the best idea. As I mentioned in my post, I'm willing to consider alternatives. I'm just not seeing how to implement the alternatives in my specific case.
      I'm just not seeing how to implement the alternatives in my specific case
      Well, its difficult to know, since you don't show why you need a bunch of $newfoo variables, but the approach with a hash might be to have two hashes, one for the original vars, and one for the new vars; so something like
      use strict; my %vars = ("foo" => 1, "bar" => 2, "baz" =>3); my %newvars = ("newfoo" => "A"); foreach my $var (keys %vars) { my $newvarname = "new" . $var; if (defined $newvars{$newvarname}) { $vars{$var} = $newvars{$newvarname}; } }

      Dave.

      Well because I really don't want to turn strict off... strict is lexically scoped, you don't have to turn it off for the whole program

      no strict; if (defined $$newvarname) { $$var = $$newvarname; }
      but that will only work of the vars are our vars

      I'm just not seeing how to implement the alternatives in my specific case.

      You could improve upon your description ( post a more complete question), I don't see why a hash couldn't work

      my %hash = ( qw/ foo 1 bar 2 baz 3 / ); $array[$user_section] =~ s{ ^ username => (.*) $ }{ Dance( \%hash, $1 ); }xemg; sub Dance { my( $vars, $one ) = @_; my $name = $vars->{$one}; return "username => $name" if defined $name; die "no name for $one "; }
Re^2: using a string as a SCALAR ref while "strict refs" in use
by Anonymous Monk on Jun 19, 2012 at 11:31 UTC

    But that would only work with GLOBALS (our/vars ) not lexicals (my)

    If you want to get at lexicals (my vars) you should use PadWalker

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (8)
As of 2024-04-16 08:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found