Ing. Branislav Gerzo wrote:
JupiterHost.Net [JN], on Wednesday, April 27, 2005 at 10:23 (-0500)
thinks about:

JN> No its not, you can if you want but it pointless:

I read it somewhere (it was perl cookbook/learning perl from o'reilly
maybe). Always declare

my @a = ( );

And here is why, if I remember correctly - if you declare
my @a; you should think you declare empty array. After that, in script
you should use @a; which do nothing. So my @a = ( ); is good practise
I think. It is the same for hashes.

"my @a;" creates the lexical variable @a at compile time and since it has just been created it will be empty. "my @a = ();" creates the variable during compilation but the assignment (IIRC) has to be done at run time. The end result is the same but the assignment makes it a tiny bit slower (not enough to really worry about.)


$ perl -e'
use Benchmark qw(cmpthese);
cmpthese( -10, {
assign => sub { my @a = (); my @b = (); my @c = (); my @d = (); return @a . @b . @c . @d },
noassign => sub { my @a; my @b; my @c; my @d; return @a . @b . @c . @d }
} );
'
Rate assign noassign
assign 112694/s -- -2%
noassign 115567/s 3% --



 >> $|++;, and I use $| = 1;
JN> That two ways to assign 1 to $|, its not similar to the array thing at all.

I meant good practise in scripts. What about when somewhere in script
will be $| = -1; pr $|-- ?
$| = 1 will work always.

Any true (numeric) value assigned to $| will set it to 1 and any false (numeric) value will set it to 0.


$ perl -e'
for ( -876868, -1, 1, 84859, "0", "abcdef", undef ) {
    $| = $_;
    print "\$_ = $_";
    print "  and  \$| = ", $|;
    print "  and  ++\$| = ", ++$|;
    print "\n";
    }
'
$_ = -876868  and  $| = 1  and  ++$| = 1
$_ = -1  and  $| = 1  and  ++$| = 1
$_ = 1  and  $| = 1  and  ++$| = 1
$_ = 84859  and  $| = 1  and  ++$| = 1
$_ = 0  and  $| = 0  and  ++$| = 1
$_ = abcdef  and  $| = 0  and  ++$| = 1
$_ =   and  $| = 0  and  ++$| = 1


--$| and $|-- have the affect of toggling the value of $| between 0 and 1.

$ perl -e'
for ( -876868, -1, 1, 84859, "0", "abcdef", undef ) {
    $| = $_;
    print "\$_ = $_";
    print "  and  \$| = $|";
    print "  and  --\$| = ", --$|;
    print "  and  --\$| = ", --$|;
    print "\n";
    }
'
$_ = -876868  and  $| = 1  and  --$| = 0  and  --$| = 1
$_ = -1  and  $| = 1  and  --$| = 0  and  --$| = 1
$_ = 1  and  $| = 1  and  --$| = 0  and  --$| = 1
$_ = 84859  and  $| = 1  and  --$| = 0  and  --$| = 1
$_ = 0  and  $| = 0  and  --$| = 1  and  --$| = 0
$_ = abcdef  and  $| = 0  and  --$| = 1  and  --$| = 0
$_ =   and  $| = 0  and  --$| = 1  and  --$| = 0



John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to