I never quite understood why this is considered dangerous style. This is the old style of calling subroutines. If you do not predeclare you subs then you either have to &sub or sub() to not get a bareword error with use strict

When you call a subroutine like &sub I understand that you are passing the value of @_ to the sub or the equivalent of sub(@_). If you call sub(), you instead are passing a null list. But exactly how is passing @_ dangerous if the sub is not using any passed variables?

I think not knowing the following is more dangerous.

#!/usr/bin/perl -w use strict; my @test = ("one","two","three"); my $x = "one"; my $y = "two"; my $z = "three"; test1($x,$y,$z); print "$x\n$y\n$z\n\n"; test2($x,$y,$z); print "$x\n$y\n$z\n"; test1(@test); print join "\n", @test, "\n"; test2(@test); print join "\n", @test, "\n"; sub test1 { my $x1 = shift; my $y1 = shift; my $z1 = shift; $_[0] = "test1: 1"; $_[1] = "test1: 2"; $_[2] = "test1: 3"; } sub test2 { my ($x2,$y2,$z2) = @_; $_[0] = "test2: 1"; $_[1] = "test2: 2"; $_[2] = "test2: 3"; }
Output:
one two three test2: 1 test2: 2 test2: 3 one two three test2: 1 test2: 2 test2: 3

Someone mind explaining this? In more detail than " '@_' is a local array, but its elements are aliases for the actual scalar paramters." What really gets me is how useing shift changes the behavior! Why?

zzSPECTREz


In reply to Re: Re (tilly) 2: Calling subroutine in a package from other perl sub. by zzspectrez
in thread Calling subroutine in a package from other perl sub. by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.