Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Merge log files causing Out of Memory (just a note on ampersand)

by Discipulus (Canon)
on Aug 25, 2016 at 10:26 UTC ( [id://1170402] : note . print w/replies, xml ) Need Help??


in reply to Re: Merge log files causing Out of Memory
in thread Merge log files causing Out of Memory

as always you are very complete and competent, but (as other times) i must dissent on:

&date_calc; and &get_dirs; is Perl4-style. Avoid the ampersand, it usually does not do what you intent to do.

is not Perl4, is perfectly valid Perl5, and is reffered by official docs as optional in modern perl

It is still not optional in three cases:

# while naming a sub like in: defined &my_sub_name; # doing indirect sub call (but $subref->() is another valid option) &$subref(); # making a reference to a sub $coderef = \&handler;

..it usually does not do what you intent to do.
just means that &get_dirs receive the current @_ even if no args are specified. The programmer must be aware of this and the feature can be also used in a profitable way.

The example in perlsub is exahustive:

&foo(1,2,3); # pass three arguments foo(1,2,3); # the same foo(); # pass a null list &foo(); # the same &foo; # foo() get current args, like foo(@_) !! foo; # like foo() IFF sub foo predeclared, else "foo"

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^3: Merge log files causing Out of Memory (just a note on ampersand)
by hippo (Bishop) on Aug 25, 2016 at 10:57 UTC
    is not Perl4, is perfectly valid Perl5

    Yes, it is perfectly valid Perl5. However it is also perfectly valid Perl4 and as afoken points out this chimes with all the rest of the script looking like Perl4 too. I don't have a perl4 installation to hand to test this on but would not be surprised if that's what the script is (or at least how it started out).

    Ampersand notation without good reason (such as the exceptions you have correctly quoted from the docs) is very much Perl4-style.

    Edited for typo and clarity

      Probably is only one of my vicious behaviours but im used to use &function($arg); to call function loacally defined in the current script (file) to distinguish them from methods-functions imported by modules i use.

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re^3: Merge log files causing Out of Memory (just a note on ampersand)
by afoken (Chancellor) on Aug 26, 2016 at 06:20 UTC

    I've updated my posting to limit the "avoid the ampersand" rule to function calls.

    just means that &get_dirs receive the current @_ even if no args are specified

    Exactly for this reason. &function; has this surprising behaviour.

    The programmer must be aware of this

    Correct. The programmer must be aware of this, because it is nastily surprising behaviour due to backwards compatibility with Perl 4. In other words: Don't expect a novice to understand what happens here.

    Compare to what we tell our little children: "Don't touch the knife." ("Don't use ampersands in function calls.") You don't explain an average three-year old the physics of cutting, the medical problems of infections and loss of large amounts of blood, and so on. Later in the life, you or someone else explains the child (or the novice) why knifes (or ampersands in front of function calls) are dangerous, and how knifes (or ampersands in front of function calls) can be useful.

    i must dissent on:
    &date_calc; and &get_dirs; is Perl4-style. Avoid the ampersand, it usually does not do what you intent to do.
    is not Perl4, is perfectly valid Perl5

    I never said that &get_dirs; was Perl 4, and I never said it was illegal in Perl 5. I named it Perl4-style, because that's how functions had to be called in Perl 4.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re^3: Merge log files causing Out of Memory (just a note on ampersand)
by choroba (Cardinal) on Aug 25, 2016 at 20:39 UTC
    There's one more context where & isn't optional:
    goto &func;

    See goto.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,