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

Re^3: RFC: Tutorial: use strict; now what!?

by tangent (Parson)
on Feb 09, 2012 at 14:44 UTC ( [id://952737]=note: print w/replies, xml ) Need Help??


in reply to Re^2: RFC: Tutorial: use strict; now what!?
in thread RFC: Tutorial: use strict; now what!?

I am trying to follow this as I use hashes all over the place. What exactly is a soft reference. Perldoc says this:
Symbolic references are names of variables or other objects, just as a symbolic link in a Unix filesystem contains merely the name of a file. The *glob notation is something of a symbolic reference. (Symbolic references are sometimes called "soft references", but please don't call them that; references are confusing enough without useless synonyms.)
I understand the difference between hard and soft links in a filesystem, but in Perl?

Replies are listed 'Best First'.
Re^4: RFC: Tutorial: use strict; now what!?
by educated_foo (Vicar) on Feb 09, 2012 at 15:06 UTC
    $foo = 23; $soft = 'foo'; # soft reference $hard = \$foo; # hard reference print "soft=$$soft, hard=$$hard\n";
    A soft reference holds a variable's name, while a hard reference holds its memory address. When you dereference a soft reference, you search for a name in the symbol table. When you dereference a hard reference, you fetch the variable at that address.
      Thanks for explaining, I didn't know you could do that. BTW when I run that I get "soft=, hard=23"
        Are you running exactly that program? What version of Perl? This is what I get:
        $ cat foo2.pl $foo = 23; $soft = 'foo'; $hard = \$foo; print "soft=$$soft, hard=$$hard\n"; $ perl foo2.pl soft=23, hard=23 $ perl -v This is perl, v5.10.1 (*) built for i686-cygwin-thread-multi-64int
Re^4: RFC: Tutorial: use strict; now what!?
by Xiong (Hermit) on Feb 17, 2012 at 15:44 UTC
    I am trying to follow this as I use hashes all over the place. What exactly is a soft reference.

    tangent, you'll get plenty of explanations of symbolic references. But even after you understand them well it may not be clear how hashes come into this, in almost the same breath.

    To recap, moritz offered hashes as an alternative to hard references. There was some discussion of this and JavaFan noted hashes can invite some of the same issues strict 'refs' is meant to avoid. You may not see the connection; it took me a moment to pick it up.

    Seen one way, use strict; is a typo-catcher. In the simple case:

    our $x13 = 'foo'; print $x31;

    ... may not work as you expect because you really meant to type $x13 both times. But use strict; will complain that you did not declare $x31 and so the typo is caught.

    If you attempt to take a hard reference and make a typo, you'll get an error, too. But if you take a symbolic reference and don't use strict; then you will not get early notice of your typo.

    Where hashes begin to resemble (in a certain mental state) symbolic references is when you start stuffing a lot of unrelated data into a hash with literal keys; and then later you want to take something out:

    my %hash; $hash{boy} = 'Arnold'; $hash{pension_balance} = 172.50; $hash{filehandle} = $fh; print $hash{pensionbalance};

    The typo will not be caught, with or without use strict; instead, perl will happily autovivify $hash{pensionbalance} for you and immediately print it... although it's undefined. This can be an annoying thing to track down.

    For more on this, see Re: Accessing hash from within module, where I demonstrate exactly this kind of risky approach... and note that it's almost as evil as global variables. I would not even have mentioned it if the OP didn't seem solidly committed to globals in the first place. As questionable as it is, I still think it's better than no strict 'refs';

    I'm not the guy you kill, I'm the guy you buy. —Michael Clayton
      Thanks for taking the time to explain Xiong, and I get the connection now, even though I had to read through everything a few times. Being an obedient student, I always use warnings as well as strict, and note that I get a warning if I try to print $hash{pensionbalance} and also if I say something like:
      my $debts = 100; my $money_left = $debts - $hash{pensionbalance}; # Use of uninitialized value in subtraction (-)
      Am I safe to assume then that use warnings will always keep me safe?
        In this case it will. Or if you set all warnings to be fatal then you're extra "safe". Depending on how you use the value though, you may or may not get a warning. If you just use it as a boolean you do not get a warning:
        if ($hash{pensionbalance}) { print "Got a pension balance!\n"; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-03-19 10:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found