Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: style guidance

by 7stud (Deacon)
on Nov 09, 2009 at 00:02 UTC ( [id://805824]=note: print w/replies, xml ) Need Help??


in reply to Re: style guidance
in thread style guidance

Thanks for the guidance. I'm trying to digest it all.

Don't assign to a global variable without localising it. You just clobbered your parent's data.

I guess you mean like this:

use strict; use warnings; use 5.010; sub do_stuff { foreach (1..5) { &dangerous($_); say "calculation in do_stuff(): ", $_ * 10; } } sub dangerous { $_ = shift; $_ *= 100; say "calculation in dangerous(): $_" } &do_stuff(); --output:-- calculation in dangerous(): 100 calculation in do_stuff(): 1000 calculation in dangerous(): 200 calculation in do_stuff(): 2000 calculation in dangerous(): 300 calculation in do_stuff(): 3000 calculation in dangerous(): 400 calculation in do_stuff(): 4000 calculation in dangerous(): 500 calculation in do_stuff(): 5000

By the way, I'm calling all user defined functions using the syntax &func() because the llama 5th ed. merely says that the & prevents name clashes with perl functions, therefore beginners should use the & until they become familiar with the names of perl functions. However, from the comments I gather there is more to it than that.

sub do_stuff1 { local $_ = shift; chomp; say; }
I don't understand what local() does differently than my(). I read the docs, and I don't understand the difference.
or even safer:

sub do_stuff1 {
    ${ local *_ } = shift;
    chomp;
    say;
}

The latter handles the case where $_ is tied or otherwise magical.

What does that * notation do, and could you give some more details why that is safer than local $_? Maybe an example?

Thanks.

Replies are listed 'Best First'.
Re^3: style guidance
by ikegami (Patriarch) on Nov 09, 2009 at 19:57 UTC

    the & prevents name clashes with perl function

    & is not a no-op. It causes prototypes to be ignored.

    I don't understand what local() does differently than my().

    my should be used whenever possible, but it's not possible to use my on package variables.

    >perl -e"my $_" Can't use global $_ in "my" at -e line 1, at end of line Execution of -e aborted due to compilation errors.

    $_ was made a legal name for a lexical in 5.10, and builtins will use the lexical $_ instead of the global $_ if there is one in scope, but not everyone is using 5.10 yet.

    What does that * notation do

    One reason my is preferred over local is that my creates a new variable, whereas local simply saves the variables current value (and restores it on scope exit). Any magic associated with the variable is unaffected by local.

    You can localise the variable rather than its value by localising the glob that contains the variable. A glob is an associative array (like a hash) that contains the different type of variables ($name, @name, %name) associated with each name in the symbol table.

    local *_ localises the whole glob, which means a new $_ will be created if one is needed.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2025-11-13 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (68 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.