http://qs321.pair.com?node_id=734531


in reply to Re^2: Perl cheat sheet
in thread Perl cheat sheet

Globals are an important exception here. If you log($!), you may easily have a clobbered $! by the time you use it. log("$!") gets a snapshot of the variable at the time of the call.

Replies are listed 'Best First'.
Re^4: Perl cheat sheet
by ikegami (Patriarch) on Jan 07, 2009 at 01:24 UTC

    That's something most people will never need to know.

    The problem is that new programmers love to put *everything* in quotes. While it's a bad idea to pass global (lexical and package) variables as arguments, that is probably not a problem you'll ever run into. Just about every sub already creates a copy of the value before it can change.

    sub logger { my ($arg) = @_; # The copying happens here, so ... # no need to do it in the caller. }

    One could even argue it's the sub's responsibility to protect global variables it changes if it uses @_ at any other point.

      Sometimes I forget that "never" means "you probably don't want to". ;-) Next time I'm in that code, I'll make it the responsibility of the called function. Thanks.
Re^4: Perl cheat sheet (dualvars)
by lodin (Hermit) on Jan 07, 2009 at 08:07 UTC

    $! was an unfortunate choice of variable. :-) It's a dualvar so it has different values if treated as a string or as a numerical.

    open my $fh, '<', 'this does not exist'; printf "%s (%d)\n", $!, $!; __END__ No such file or directory (2)
    It's better to just create a copy and pass that, if the subroutine doesn't copy the argument.

    lodin

      The example was fine. It doesn't make sense for logger to need the dualvar.

      • If you wanted to pass the error string to the sub, the caller would use "$!".
      • If you wanted to pass the error number to sub, the caller would use 0+$!.
      • If the sub needs both an error string and an error number, it would take two arguments.
      • If the sub wants to look at $!, it wouldn't take it as an argument.