Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Referencing the locals

by Chuma (Scribe)
on Apr 29, 2021 at 00:30 UTC ( [id://11131835]=note: print w/replies, xml ) Need Help??


in reply to Referencing the locals

Thank you all for your answers!

It's fair to say this is all firmly in the chaotic evil school of programming, so it's no wonder "strict" doesn't like it.

Using explicit references would work in this example, but then I can't do anything else with the string. A hash would allow that, although I probably don't want to refer to the array as @{$animals{'cat'}} for the rest of the program, so I guess I could add

my @cat = @{$animals{'cat'}}; my @dog = @{$animals{'dog'}}; my @pig = @{$animals{'pig'}};

There's also the option of just repeating the whole code block three times over instead of looping. I suppose that's alright, but doesn't really strike me as beautiful code.

The text about "why it's stupid to use a variable as a variable name" makes a good point, generally. Symbolic references can lead to uncontrolled global variables – if $x turns out to be something unexpected, there's no telling what $$x might destroy. But that's not really applicable here, firstly because the variable takes on exactly these three values, and secondly because I'm trying to make the variables not global. It seems like an odd design choice that that's apparently impossible, but maybe it's just an implementation artefact.

Replies are listed 'Best First'.
Re^2: Referencing the locals
by haukex (Archbishop) on Apr 29, 2021 at 07:10 UTC
    although I probably don't want to refer to the array as @{$animals{'cat'}} for the rest of the program, so I guess I could add
    my @cat = @{$animals{'cat'}}; my @dog = @{$animals{'dog'}}; my @pig = @{$animals{'pig'}};

    Note that makes a shallow copy of the array - personally I'd write my $cat = $animals{cat};, and if it's just a simple array and not a nested structure I'd write $$cat[$i] to access it (though many people also prefer the $cat->[$i] syntax).

    Note that as of Perl 5.24, you can write $animals{cat}->@* instead of @{$animals{cat}} (in 5.20 and 5.22 this needed a use feature 'postderef';). Also, as of 5.22 there's the still experimental Assigning to References, where you can say \my @cat = $animals{cat}; and have @cat be an alias to the arrayref, which IMHO is really nice, but the feature is still experimental so it is subject to change!

    Update: A few more comments:

    There's also the option of just repeating the whole code block three times over instead of looping. I suppose that's alright, but doesn't really strike me as beautiful code.

    I agree, DRY applies.

    The text about "why it's stupid to use a variable as a variable name" makes a good point, generally. ... But that's not really applicable here

    I think the point is more to make a case for why strict is a good thing.

      Huh, I didn't know that, about the shallow copy. But in this case, since I'm just trying to "rename" the array, shallow copy seems like the right thing.

      There are of course situations where you can't avoid repetition, but I always figured this was a neat shortcut in Perl (I can't think of any other language that would allow it). I often find that I want to, for example, open cats.txt, @cats=<$fh>, $type{$_}='cats', print "there were $cats cats", $max{'cats'}=max(@cats), or whatever the case may be – and then do the same thing for 'dogs'. I'm not aware of any other way that's as succinct and simple as looping over the strings.

      There are a lot of people who argue that you should always use "strict", basically changing the language into a stricter version of itself. There are also those who argue that you should use an even more strict version of Perl, and that it's called Python. Different preferences, I guess.

        Huh, I didn't know that, about the shallow copy. But in this case, since I'm just trying to "rename" the array, shallow copy seems like the right thing.

        The shallow copy would be a problem if your arrays are large, or if you expect changes to the array to be reflected in the original data structure. (Because it's only a shallow copy, changes to nested arrays or hashes would be reflected in the original data.)

        There are of course situations where you can't avoid repetition, but I always figured this was a neat shortcut in Perl (I can't think of any other language that would allow it). ... I'm not aware of any other way that's as succinct and simple as looping over the strings.

        If by this you mean using symbolic references, then as I said, the IMHO better appraoch is hashes. They also have the advantage that instead of for('cat','dog','pig') or for(\@cat,\@dog,\@pig) you can just write for (keys %animals), allowing your code to be more generic.

        There are a lot of people who argue that you should always use "strict", basically changing the language into a stricter version of itself. ... Different preferences, I guess.

        If you look, you'll find lots of information on why strict is a best practice. I recently wrote a bit about it here, and I encourage you to read it since I think it applies here as well.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-25 21:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found