sulfericacid has asked for the wisdom of the Perl Monks concerning the following question:

I'm still not well versed in perl but I know I've never come across a script which my'd an array (ie. my @email = "";). But under strict you MUST declare each and every scalar you use. Why wouldn't you have to do the same with arrays?

"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: mying arrays?
by Tomte (Priest) on Feb 16, 2003 at 18:28 UTC

    Under strict you must declare every variable you use, scalar, array, hash doesn't matter.
    my @email = ""; is plain wrong, because @mail is an array and "" is a scalar.

    I suspect one of your misunderstandings is what 'value' a not explicitly defined variable has.
    Scalars are simply undefined; arrays and hashes are just empty.

    my @array; # emtpy array my @array = (); # empty array my %hash; # empty hash my %hash = (); # empty hash (not sure about this *blush*) my $scalar; # undefined scalar my $scalar = ""; # scalar with empty string as value
    I found any of the following
    perldoc perldata
    perldoc perlref
    perldoc perlreftut
    to be very helpfull readings, for starters especially perlreftut - Mark's very short tutorial about references

    regards,
    tomte


Re: mying arrays?
by pfaut (Priest) on Feb 16, 2003 at 18:18 UTC

    You do have to do the same with arrays. Hashes, too.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: mying arrays?
by Limbic~Region (Chancellor) on Feb 16, 2003 at 18:40 UTC
    As I was typing my reply - my box crashed, so you undoubtedly already have your answer.

    I am very suprised that you have never seen a script that uses my to scope an array. It is used to scope a variable to enclosing brackets (or Main if there are no enclosing brackets).

    Consider the following VERY contrived example:

    #!/usr/bin/perl -w use strict; my @array; # no enclosing braces - scoped to Main my $var1 = 3; my $var2 = 4; foreach (1..10) { push @array , $_; } if ($var2 > $var1) { my @array = ($var1 , $var2); print "$_\n" foreach (@array); } print "Other array\n"; print "$_\n" foreach (@array);

    This allows you to use the same variable name numerous times within the same program without stepping on each other. It should not be confused with local, which I will leave you to read the Perl docs to grok.

    Cheers - L~R

Re: mying arrays?
by Ryszard (Priest) on Feb 16, 2003 at 18:29 UTC
    In addition you dont have to do the assigning,  my (@array, %hash, $scalar); works quite well.

    Using my you're scoping your variables.

Re: mying arrays?
by sulfericacid (Deacon) on Feb 16, 2003 at 21:17 UTC
    Ok, sorry for the confusion everyone and I probably should have known that. But I've never seen code which my'd any array which got me wondering why that might have been. I rarely work with scalars or hashes (most of my scripts are simple enough to use scalars).

    Thanks for your help everyone!

    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid