Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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 ( [id://977043]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^3: using a string as a SCALAR ref while "strict refs" in use
by dave_the_m (Monsignor) on Jun 19, 2012 at 12:05 UTC
    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.

Re^3: using a string as a SCALAR ref while "strict refs" in use
by Anonymous Monk on Jun 19, 2012 at 11:59 UTC

    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 "; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-03-29 10:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found