Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Variable Names and References

by Joost (Canon)
on Mar 09, 2007 at 15:59 UTC ( [id://603998]=note: print w/replies, xml ) Need Help??


in reply to Variable Names and References

${"str$_"} is not a "hard" reference, it's a symbolic reference. i.e. it just refers to the name of a variable. You can't use symbolic refences while you have "strict refs" in use (as the error correctly states).

You can use symbolic references if you switch off "strict refs":

for (1..3) { no strict 'refs'; my $ref = \${'str'.$_}; # this creates a hard reference VIA a symb +olic reference # or: my $ref = "str$_"; # this is a purely symbolic reference, +access is the same as above print "The string is $$ref"; }
update: you can only refer to GLOBALs with symbolic references. (update2: here's the relevant documentation in perlref) That's why that code still won't work. Here's a working version:
#!/usr/bin/perl -l use warnings; use strict; our $str1 = qq[A-M o'Foo']; our $str2 = qq[.,rtyu_'']; our $str3 = qq[$<%^]; our $str4 = qq[this works]; my $ref4 = \$str4; print $$ref4; # works as expected for (1..3) { no strict 'refs'; my $ref = \${'str'.$_}; print "The string is $$ref"; }

Note that in your example using a simple array would be much easier, less error-prone and more efficient. Symbolic references are almost only useful these days for creating subroutines / packages at runtime (basically, wherever the only alternative is to use eval STRING).

updated again, added some comments

Replies are listed 'Best First'.
Re^2: Variable Names and References
by chakram88 (Pilgrim) on Mar 09, 2007 at 16:13 UTC
    huh. Thanks. I understand what you mean about the symbolic reference. I just was not seeing it that way. (hence, while I understood the error message, I thought my problem was in the syntax, when all along it was improper referencing.)

    Thanks. And re your suggestion, I was going to go the route of an array -- but got hung up on why I couldn't make this work.

    Thank you, I can now stop floundering and get back to being productive.

Re^2: Variable Names and References
by rvosa (Curate) on Mar 12, 2007 at 00:52 UTC
    # or: my $ref = "str$_"; # this is a purely symbolic reference, +access is the same as above
    I don't see how that is a symbolic reference? I think this is (had to think a while to come up with something contrived, here):
    my $class = 'CGI'; my $version; { no strict 'refs'; $version = ${"${class}::VERSION"}; use strict; } print $version # e.g. 3.05 on my machine
    In other words, we construct a variable's name ($CGI::VERSION) out of expanded variables ($class), and expand (it's almost like a string eval()) the result in turn to return the value stored under the constructed variable's name.

    ...mmmm still not really put elegantly, sorry.
      Symbolic references are just names of global variables. That means "str2" is a symolic reference, just like "CGI::VERSION" is.

      In other words; "${class}::VERSION"; is a symbolic reference, and ${"${class}::VERSION"}; derefences the reference into the value of $CGI::VERSION. Note that "hard" references use exactly the same syntax to dereference.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-24 20:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found